# ESP8266 does not write to RTC memory correctly

**URL:** <https://forum.arduino.cc/t/esp8266-does-not-write-to-rtc-memory-correctly/548172>\
**Category:** 3rd Party Boards\
**Created:** [September 27, 2018, 9:07am UTC](https://forum.arduino.cc/t/esp8266-does-not-write-to-rtc-memory-correctly/548172 "2018-09-27T09:07:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![itwasntme967](https://avatars.discourse-cdn.com/v4/letter/i/ce73a5/32.png) [@itwasntme967](https://forum.arduino.cc/u/itwasntme967)\
**Post date:** [September 27, 2018, 9:07am UTC](https://forum.arduino.cc/t/esp8266-does-not-write-to-rtc-memory-correctly/548172/1 "2018-09-27T09:07:57Z")

</div>

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.

```cpp
#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

```cpp
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

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [September 28, 2018, 4:09am UTC](https://forum.arduino.cc/t/esp8266-does-not-write-to-rtc-memory-correctly/548172/2 "2018-09-28T04:09:19Z")

</div>

Try removing the &:

sizeof(&RTCdata)

---

<div class="post-metadata">

**Author:** ![wondward](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wondward/32/274076_2.png) [@wondward](https://forum.arduino.cc/u/wondward)\
**Post date:** [July 1, 2020, 8:55am UTC](https://forum.arduino.cc/t/esp8266-does-not-write-to-rtc-memory-correctly/548172/3 "2020-07-01T08:55:22Z")

</div>

That's because the area 65 is probably used by the system,you should choose a vacant address,like 6400(the total space is 8K).

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 5:14pm UTC](https://forum.arduino.cc/t/esp8266-does-not-write-to-rtc-memory-correctly/548172/4 "2021-05-06T17:14:38Z")

</div>


