ESP8266-Webserver-Tutorials
ESP8266-Webserver-Tutorials copied to clipboard
save & load json
Hello, thanks for your great tutorial. It's really good. I have an issue with the json load. It loads the json fine, but in another root... so, the rest of the app can't use it. is it a mistake ? if no, did I miss something ? if yes, how to fix it ? ;) Thanks !! Francois (greetings from france)
https://github.com/projetsdiy/ESP8266-Webserver-Tutorials/blob/master/Part5_DHT22WebserverESP8266_SPIFFS/Part5_DHT22WebserverESP8266_SPIFFS.ino first root at L60 second root at L197
clearly, the root scope isn't the same, and the json data is lost after exiting the function.
To solve this problem, I propose to replace the subprogram of the same name with the one below:
void loadHistory(){
DynamicJsonBuffer json_Buffer(5000);
File file = SPIFFS.open(HISTORY_FILE, "r");
if (!file){
Serial.println("No History Exist");
} else {
size_t size = file.size();
if ( size == 0 ) {
Serial.println("History file empty !");
} else {
std::unique_ptr<char[]> buf (new char[size]);
file.readBytes(buf.get(), size);
JsonObject &temp_root = json_Buffer.parseObject(buf.get()); //область видимости в этой подпрограмме только
if (!temp_root.success()) {
Serial.println("Impossible to read JSON file");
} else {
Serial.println("History loaded");
temp_root.prettyPrintTo(Serial);
Serial.println (" ");
//---- заполнение рабочего JsonObject
for (int i=0; i< temp_root["timestamp"].size(); i++){
timestamp.add( long(temp_root["timestamp"][i]));
hist_t.add(double(temp_root["t"][i]));
hist_h.add(double(temp_root["h"][i]));
hist_pa.add(double(temp_root["pa"][i]));
}
if (temp_root["bart"].size() > 0 ) {
for ( int k = 0 ; k < 7 ; k++ ) {
bart.add(double(temp_root["bart"][k]));
barh.add(double(temp_root["barh"][k]));
}
}
}
}
file.close();
}
}