Hi all,
I am tinkering to build a thermometer with the ESP8266.
Since the goal is to run it on a battery and the measurements do not have to be in realtime I want to let the controller sleep between measurements.
For this reason I need to store a few values in the RTC memory, but out of the four variables I want to save only two are stored.
#include <Wire.h>
#include <Adafruit_SHT31.h>
Adafruit_SHT31 sht31 = Adafruit_SHT31();
typedef struct data { //stting up the variables to be stored
float x;
float y;
int i = 0;
unsigned int magic = 0;
};
data RTCdata;
int counter;
void setup() {
Serial.begin(115200);
Serial.println(RTCdata.magic, HEX); //debugging Statement to check if the magic bytes were written correctly
ESP.rtcUserMemoryRead(65, (uint32_t*) &RTCdata, (sizeof(data)));
if(RTCdata.magic == 1) { //if the magic bytes are set, do a few things differently
(ESP.rtcUserMemoryRead(65, (uint32_t*) &RTCdata, (sizeof(data)));
} else {
ESP.rtcUserMemoryRead(65, (uint32_t*) &RTCdata, (sizeof(data)));
RTCdata.magic = 1;
}
Serial.print("i: ");
Serial.println(RTCdata.i); //another debugging output
Serial.println();
if (RTCdata.i >= 5) { //if the code was executed 5 times or more reset the display fully
Serial.println("Updating display...");
display.fillRect(0, 0, GxEPD_HEIGHT, GxEPD_WIDTH, GxEPD_WHITE);
display.update();
RTCdata.i = 0;
Serial.println("done");
}
//SDA,SCL
Wire.begin(5, 12);
sht31.begin(0x44);
float t = sht31.readTemperature();
float phi = sht31.readHumidity(); //reading the temperature and humidity
if (RTCdata.x != t) {
UpdateDisplayTemp(t);
counter = 1;
RTCdata.x = t;
}
if (RTCdata.y != phi); {
UpdateDisplayHydro(phi); //if any of the values differ from the last measurement write the new
counter = 1; //value to the display and set a flag
RTCdata.y = phi;
}
if(counter == 1){ //if any part of the display was updated increase the loop count
RTCdata.i++;
}
ESP.rtcUserMemoryWrite(65, (uint32_t*) &RTCdata, sizeof(&RTCdata));
delay(100);
Serial.println();
Serial.print("Temp = ");
Serial.println(RTCdata.x);
Serial.print("Hydro = ");
Serial.println(RTCdata.y);
Serial.print("Loop Nr = "); //outputting all the variables supposed to be stored in the RTC memory
Serial.println(RTCdata.i);
Serial.print("Magic = ");
Serial.println(RTCdata.magic, HEX);
Serial.println();
ESP.deepSleep(5e6, WAKE_RF_DISABLED);
}
So as you can see, I have quite a few debugging outputs, and while I would expect something like
0 if it's the first run, otherwise 1
i: (value of i)
Temp = 23.97
Hydro = 38.40
Loop Nr = (value of i++)
Magic = 1
I instead get
0
i: -262042446
Temp = 23.97
Hydro = 38.40
Loop Nr = -262042445
Magic = A588B3B1
So it seems like RTCdata.magic and RTCdata.i don't get written properly to memory.
Does anybody know, what causes this to happen?
Also in the first if-statement
if(RTCdata.magic == 1) {
ESP.rtcUserMemoryRead(65, (uint32_t*) &RTCdata, (sizeof(data)));
} else {
ESP.rtcUserMemoryRead(65, (uint32_t*) &RTCdata, (sizeof(data)));
RTCdata.magic = 1;
}
the program seems to ignore the statement and the value of the variable completely and just defaults into the else-state no matter the state of the variable.
There is another bit of code in the else-statement through which I can tell, but which I don't want to post here because it is not relevant to any of the other problems.
Does anybody know what is happening here?
Thanks in advance