Using the ESP8266 Real Time Clock memory to manage device reboots
/The ESP8266 device has a built in Real Time Clock (RTC) that can work as, well, a real time clock. This is very useful if you want to put the processor to sleep for a while in low power mode and have it wake up later. The RTC also contains a small amount of memory which is maintained in low power mode. You would put values into this that you wanted to remember when the device wakes up again.
You can also use RTC memory to store values that will be retained when the device is rebooted. After a reboot you can’t make any assumptions about values that might or might not be in memory, but you can assume that the RTC memory values are intact.
I’m using this ability to allow an ESP8266 to reboot in different “modes”. The problem that I am solving is that there is not enough memory in an ESP8266 to allow it to run the Connected Little Boxes software and a web server at the same time. My program sets the type of device it needs into RTC memory and then resets the device. When it starts running it reads the value back and then starts in the requested mode.
int getInternalBootCode()
{
uint32_t result;
ESP.rtcUserMemoryRead(0, &result, 1);
return (int)result;
}
void setInternalBootCode(int value)
{
uint32_t storeValue = value;
ESP.rtcUserMemoryWrite(0, &storeValue, 1);
}
The getInternalBootCode function returns the contents of the value at the start of the RTC user memory. the setInternalBootCode function sets it. The functions use the ESP.rtcUserMemoryRead and ESP.rtcUserMemoryWrite functions which transfer a block of data between the program and the RTC memory. The first parameter is the offset into the RTC memory (I’m using 0 to indicate the start of the memory). The second parameter is a pointer to a 32 bit data value. The third parameter is the number of 32 bit values to transfer. I only want the one value for my boot code. Now I can store it and fetch it back.