Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save & load json #6

Open
Koxx3 opened this issue Nov 3, 2018 · 2 comments
Open

save & load json #6

Koxx3 opened this issue Nov 3, 2018 · 2 comments

Comments

@Koxx3
Copy link

Koxx3 commented Nov 3, 2018

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
Copy link
Author

Koxx3 commented Nov 3, 2018

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.

@Igor-from-moscow
Copy link

Igor-from-moscow commented Nov 17, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants