Bulk-Update Using ESP8266

4 views (last 30 days)
Matt Hosini
Matt Hosini on 18 Feb 2022
Hi guys,
One way of posting multiple data at once is bulk-write JSON Data. There is an example re this here, which shows how to update the data on ESP8266 frequently. I can't work out step 8, which expalins how to update the JSON file.
Could you please provide me a reference/tutorial/manual so that I can upskill myself to work this out?
Also, I need to send the temperature readings (variables). Do I need to define the temp data as object or array on JSON?
Below provides the code for step 8.
// Updates the josnBuffer with data
void updatesJson(char* jsonBuffer){
/* JSON format for updates paramter in the API
* This examples uses the relative timestamp as it uses the "delta_t". You can also provide the absolute timestamp using the "created_at" parameter
* instead of "delta_t".
* "[{\"delta_t\":0,\"field1\":-70},{\"delta_t\":3,\"field1\":-66}]"
*/
// Format the jsonBuffer as noted above
strcat(jsonBuffer,"{\"delta_t\":");
unsigned long deltaT = (millis() - lastUpdateTime)/1000;
size_t lengthT = String(deltaT).length();
char temp[4];
String(deltaT).toCharArray(temp,lengthT+1);
strcat(jsonBuffer,temp);
strcat(jsonBuffer,",");
long rssi = WiFi.RSSI();
strcat(jsonBuffer, "\"field1\":");
lengthT = String(rssi).length();
String(rssi).toCharArray(temp,lengthT+1);
strcat(jsonBuffer,temp);
strcat(jsonBuffer,"},");
// If posting interval time has reached 2 minutes, update the ThingSpeak channel with your data
if (millis() - lastConnectionTime >= postingInterval) {
size_t len = strlen(jsonBuffer);
jsonBuffer[len-1] = ']';
httpRequest(jsonBuffer);
}
lastUpdateTime = millis(); // Update the last update time
}

Answers (1)

Christopher Stapels
Christopher Stapels on 18 Feb 2022
You can just define the temperature readings as variables, but making them in an array will make it easier to write them to the JSON object in a loop. In the above example the data are originally stored in the variables temp and rssi before they become part of the JSON string in step 8.
In step 10 of this example, you can see how to build the posting string from a data array. This string is for the single update not bulk, but it shows the basic idea.
As far as a reference manual, I would suggest you make sure you understand JSON format, but then just study the format specified in the documentation page that you linked for bulk update JSON. Its a bit of a custom thing that we created, so Im not sure there is a standard that covers it exactly.

Communities

More Answers in the  ThingSpeak Community

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by