JSON parsing and deep sleep ESP8266

I have been working on a sensor system (ESP8266) that collects data for five minutes and then sends it over a wireless link. The ESP8266 goes into deep sleep, waking every minute to take readings. After five reading cycles, it establishes a WiFi link and transmits the data.

My plan was to use the JSON library and add each set of readings to a string stored within the SPIFFS. On waking from deep sleep, the string would be retrieved, parsed, new data added and then written back to the SPIFFS.

I have written a small program to test the concept but I am having some issues. The retrieving, parsing, data addition and storing all work well if the ESP8266 does not sleep. If the ESP8266 deep sleeps, the file is retrived and parses, but adding new readings seem to reset the nested arrays. Here is the test program:

#include <FS.h>
#include <ArduinoJson.h>

float   t = 0 ;
float   h = 0 ;

DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonArray& timestamp = root.createNestedArray("timestamp");
JsonArray& temp = root.createNestedArray("t");
JsonArray& humid = root.createNestedArray("h");

void loadfile() {
  File file = SPIFFS.open(" / temp.txt", "r");
  if (!file) {
    Serial.println("No Log File Exists");
  } else {
    size_t size = file.size();
    if ( size == 0 ) {
      Serial.println("Log File is Empty");
    } else {
      JsonObject& root = jsonBuffer.parseObject(file);
      if (!root.success()) {
        Serial.println("Error reading Log File");
      } else {
        Serial.println("Log File Loaded");
        root.printTo(Serial);
        Serial.println();
      }
    }
    file.close();
  }
}

void savefile() {
  Serial.println();
  Serial.println("Saving Log File");
  Serial.println();
  File file = SPIFFS.open(" / temp.txt", "w");
  root.printTo(file);
  file.close();
}

void setup() {

  Serial.begin ( 74880 );

  Serial.println("Starting SPIFFS");
  Serial.println();

  if (!SPIFFS.begin()) {
    Serial.println("SPIFFS Mount failed");
  } else {
    Serial.println("SPIFFS Mount succesfull");
    loadfile();
  }
  delay(100);
}

void loop() {

  for (int i = 0; i < 5; i++) {
    t = 1;
    h = 2;
    addPtToLog();
  }
  ESP.deepSleep(10e6);
}

void addPtToLog() {

  unsigned long currentMillis = millis();
  long int tps = currentMillis;
  timestamp.add(tps);
  temp.add(t);
  humid.add(h);
  savefile();
  root.printTo(Serial);
  Serial.println();
  delay(2000);
}

Here is the Serial Monitor output:

Starting SPIFFS

SPIFFS Mount succesfull
No Log File Exists

Saving Log File

{"timestamp":[70375],"t":[1],"h":[2]}

Saving Log File

{"timestamp":[70375,72423],"t":[1,1],"h":[2,2]}

Saving Log File

{"timestamp":[70375,72423,74428],"t":[1,1,1],"h":[2,2,2]}

Saving Log File

{"timestamp":[70375,72423,74428,76434],"t":[1,1,1,1],"h":[2,2,2,2]}

Saving Log File

{"timestamp":[70375,72423,74428,76434,78439],"t":[1,1,1,1,1],"h":[2,2,2,2,2]}

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v0fd86a07
~ld
Starting SPIFFS

SPIFFS Mount succesfull
Log File Loaded
{"timestamp":[70375,72423,74428,76434,78439],"t":[1,1,1,1,1],"h":[2,2,2,2,2]}

Saving Log File

{"timestamp":[332],"t":[1],"h":[2]}

Saving Log File

{"timestamp":[332,2337],"t":[1,1],"h":[2,2]}

Saving Log File

{"timestamp":[332,2337,4342],"t":[1,1,1],"h":[2,2,2]}

Saving Log File

{"timestamp":[332,2337,4342,6348],"t":[1,1,1,1],"h":[2,2,2,2]}

Saving Log File

{"timestamp":[332,2337,4342,6348,8353],"t":[1,1,1,1,1],"h":[2,2,2,2,2]}

I am at a loss on how to resolve this particular issue. Any assistance would be greatly appreciated.

cheers,

Adrian

In the log(Serial output),

there are two strings of "Starting SPIFFS".
So the program started twice,in the first time and the second time when the program resume from the deep sleep.

Do you know what we maintain the value in the deep sleep.

Parhaps,I think we don't maitain many RAM value.

Best regards,

When the ESP8266 returns from deep sleep the entire program restarts. You can use memory associated with the RTC to store values during sleep.

Here the issue seems to revolve around the parsing of the stored JSON string and why new values are not added to the parsed nested arrays. I am hoping it is just my lack of understanding of the parsing process and that there is a way to make this work.

Cheers,

Adrian