ESP8266-Webserver-Tutorials icon indicating copy to clipboard operation
ESP8266-Webserver-Tutorials copied to clipboard

save & load json

Open Koxx3 opened this issue 7 years ago • 2 comments

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)

Koxx3 avatar Nov 03 '18 18:11 Koxx3

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.

Koxx3 avatar Nov 03 '18 18:11 Koxx3

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();
  }
}

Igor-from-moscow avatar Nov 17 '19 00:11 Igor-from-moscow