Manuel Alejandro Iglesias Abbatemarco
Published © LGPL

MultiMedia IoT with Artik Cloud - First MM-IOT Idea Concept

Introducing MultiMedia in the IoT Artik cloud for Smart Cities or Smart Homes, using low cost hardware we can bring MM-IOT to every corner.

AdvancedFull instructions provided3 days1,022
MultiMedia IoT with Artik Cloud - First MM-IOT Idea Concept

Things used in this project

Story

Read more

Schematics

WINC1500 shield

This is the Flexduino WiFi Shield
File missing, please reupload.

simple esp breakout

Code

Basic Upload Image function

C/C++
void parseImgurUpload(char *json){
	char *ptr1, *ptr2;
	int len;

	if(!(ptr1 = strstr(json, "id\":")))
		return;
	ptr1 += strlen("id\":") + 1;
	ptr2 = strstr(ptr1, "\",\"");
	len = ptr2 - ptr1;
	memset(ID, 0, sizeof(ID));
	strncpy(ID, ptr1, len);
	Serial.print("Picture ID = ");
	Serial.println(ID);
}

void sendImgurImageCb( void ){
	boolean currentLineIsBlank = true;
	boolean chase4JsonStart = false;
	boolean chase4JsonEnd = false;
	boolean postSuccess = false;

	char rxBuf[5000];
	char *rxBufPtr = &rxBuf[0];
	memset(rxBuf, 0, sizeof(rxBuf));
	char *jsonPtr = (char*)malloc(MAX_UPLOAD_RESPONSE_SIZE);
	char *jsonAdderPtr = jsonPtr;
	uint16_t idx = 0;
	uint8_t nested;
	Serial.println("trying to connect to server");

	memset(jsonPtr, 0, MAX_UPLOAD_RESPONSE_SIZE);
	if(client.connect(imgur_url, 443)){
		Serial.println(" connected to Imgur - Send image");
		// client POST to Imgur API version 3
		client.println("POST /3/image.json HTTP/1.1");
		client.println("Host: api.imgur.com");
		client.println("Accept: */*");
		client.println("Content-Type: application/json");
		client.println("Connection: close");
		client.println(AuthorizationDataImgur);
		// Automated POST data section
		client.print("Content-Length: ");
		// add the length
		int n = loadImageInJson();
		client.println(n);
		client.println();
		char *ptr = (char*)&tmpBuf[0];
		// transmit without overflowing the tx buffer
		int k;
		while(n){
			if(n < 500)
				k = client.write(ptr, n);
			else
				k = client.write(ptr, 500);
			if(k == 0){
				delay(1);
				continue;
			}else{
				n  = n - k;
				ptr += k;
			}
		}
		client.println();
	}
	Serial.println("Send Image done");
	long start = millis();
	for(;;){							// this timeout is needed
		if(millis() - start >= 5000)
			break;
		while (1) {
			int rdy = client.available();
			if (!rdy)
				break;
			start = millis();
			client.read((uint8_t*)rxBufPtr, rdy);
			rxBufPtr += rdy;
		}
	}

	char *idxPtr = &rxBuf[0];
	uint16_t len = rxBufPtr - idxPtr;
	for(int j=0; j<len; j++){
		char c = *idxPtr++;
		Serial.write(c);
		if(idx >= 100){
			Serial.println();
			idx = 0;
		}
		if(c == '{' && chase4JsonStart){
			idx++;
			*jsonAdderPtr++ = c;
			chase4JsonStart = false;
			chase4JsonEnd = true;
			nested = 1;
			continue;
		}
		if(chase4JsonEnd){
			if(c == '}'){
				nested--;
				if(nested == 0){
					chase4JsonEnd = false;
					postSuccess = true;
				}
			}else if(c == '{'){
				nested++;
			}
			idx++;
			*jsonAdderPtr++ = c;
		}
		if (c == '\n' && currentLineIsBlank){
			chase4JsonStart = true;
			currentLineIsBlank = false;
			continue;
		}
		if (c == '\n') {
		  currentLineIsBlank = true;
		}else if (c != '\r') {
		  currentLineIsBlank = false;
		}

	}
	parseImgurUpload(jsonPtr);
    if (client.connected()) {
      Serial.println("disconnecting from server");
      client.stop();
    }
    free(jsonPtr);
    uploadImgurImageDoneCb();
}

Flexduino Artik branch

The Fkexduino Artik implementation for Multimedia IoT

Credits

Manuel Alejandro Iglesias Abbatemarco

Manuel Alejandro Iglesias Abbatemarco

17 projects • 77 followers
Embedded Firmware Engineer

Comments