reading JSON from serial into a char array
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void setup()
{
float carbonIntensity=0.0f; // <<< define variable to hole carbonIntensity value
Serial.begin(115200);
Serial.setTimeout(100000ul);
char str[500]={0};
Serial.readBytesUntil('\n', str, 500); // read JSON from Serial into str
char * pch, *lastToken;
Serial.print ("Splitting string into tokens: ");
Serial.println(str);
pch = strtok (str,"\":,{}");
while (pch != NULL)
{
Serial.print ("token found ");
Serial.println(pch);
// check if last token is carbonIntensity and current token starts with a digit
if(strcmp(lastToken,"carbonIntensity") == 0 && isdigit(pch[0])) {
carbonIntensity=atof(pch); // <<<< set up value of carbonIntensity
Serial.print (" found "); Serial.print(lastToken);
Serial.print(" value "); Serial.println( carbonIntensity);
}
lastToken= pch;
pch = strtok (NULL, "\":,{}");
}
Serial.print("\nresult carbonIntensity = "); Serial.println( carbonIntensity);
}
void loop() {}
I send it the file
{"_disclaimer":"This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data, please contact hello@tmrow.com","status":"ok","countryCode":"DE","data":{"carbonIntensity":212.1861638551479,"fossilFuelPercentage":24.364762395715616},"units":{"carbonIntensity":"gCO2eq/kWh"}}
and the result is
Splitting string into tokens: {"_disclaimer":"This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data, please contact hello@tmrow.com","status":"ok","countryCode":"DE","data":{"carbonIntensity":212.1861638551479,"fossilFuelPercentage":24.364762395715616},"units":{"carbonIntensity":"gCO2eq/kWh"}}
token found _disclaimer
token found This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data
token found please contact hello@tmrow.com
token found status
token found ok
token found countryCode
token found DE
token found data
token found carbonIntensity
token found 212.1861638551479
found carbonIntensity value 212.19
token found fossilFuelPercentage
token found 24.364762395715616
token found units
token found carbonIntensity
token found gCO2eq/kWh
token found
result carbonIntensity = 212.19
therefore in your code after you have skipped the headers you read the incomming JSON into a char array and then tokenise it, e.g. something along the lines of
// Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n";
if (!client.find(endOfHeaders))
{
Serial.println(F("Invalid response"));
return;
}
float carbonIntensity=0.0f; // <<< define variable to hole carbonIntensity value
char str[500]={0};
int index=0;
// read each byte, send to the serial monitor and store in str[] until end of line
while (client.available()) {
char c = 0;
client.readBytes(&c, 1);
Serial.print(c);
if(c != '\n') str[index++]=c;
else break;
}
char * pch, *lastToken;
Serial.print ("Splitting string into tokens: ");
Serial.println(str);
pch = strtok (str,"\":,{}");
while (pch != NULL)
{
Serial.print ("token found ");
Serial.println(pch);
// check if last token is carbonIntensity and currect token starts with a digit
if(strcmp(lastToken,"carbonIntensity") == 0 && isdigit(pch[0])) {
carbonIntensity=atof(pch); // <<<< set up value of carbonIntensity
Serial.print (" found "); Serial.print(lastToken);
Serial.print(" value "); Serial.println( carbonIntensity);
}
lastToken= pch;
pch = strtok (NULL, "\":,{}");
}
Serial.print("\nresult carbonIntensity = "); Serial.println( carbonIntensity);
hope that makes sense