
Timezone: Europe/London
Deepsleep puts the ESP into ultra low power mode, allowing it to be maintained on a battery for quite a long time.
In deepsleep, all that is kept powered is the RTC, so all volatile memory is lost. Emerging from deepsleep is pretty much a power on reset.
So the downside of this is if you need to preserve some essential variables, you would have to write them to a file to be read on waking.
A useful function of the RTC is that you can store a limited amount of data in spare bytes of its memory block.
This enables you to keep essential data up to date
which does not get lost when the ESP enters deepsleep.
All that is required to do this is to define the amount of memory to allocate and write a function to read and write to it.
If you only want to store a few small numerical variables, here is one method, using two bytes per variable.
For 16 bit integers, whole numbers between 0 to 65,535, (or if using minus numbers -32768 to + 32768).
A byte stores a number from 0 to 255. If you use two bytes in pairs, the most significant bit [MSB] stores the integer divided by 256, and the least significant bit [LSB] stores the remainder.
To convert back to the original integer, read the MSB and multiply it by 256, then add the LSB. Therefore, if both were 255, the integer would be the maximum 65,535.
For numbers with decimal points involved, I have cheated a little to keep it fairly simple.
I use the MSB as the whole part
of the number to the left of the decimal point, and the LSB for the decimal part of the number, subtract the whole part from the number,
leaving the decimal and multiply by 100 to make it a whole number. Use the round() function to ensure it is an integer and store in the LSB.
Reading the number is just the opposite of this. Eg. 116.9569 would be: MSB = int(number) , [116].
LSB = int(round(number - MSB,2)*100) [95.69 rounded = 96]
The drawback here is you can only store fractions less than 255.9999
You could store proper floating point numbers, but this would take 5 bytes per number, and complicate the code somewhat too.
Here is an example.
If you need to save larger floating point numbers, strings and other data types, I have written a library to do just that.
RMM.py consists of a class library and a few associated functions to put in your code.
It handles several data types, and you can save several different types at the same time without having to decode them.
The ESP32 has 2047 bytes of useable RTC memory, the ESP8266 only 490. This library works for both, and also enables you to breaks this size limit.
You can view and download the library and its associated demo files from my github repository.
or view the file from this link. View raw code
IDE editors for programming ESP boards.
Micropython editors
Micropython functionality and libraries.
Micropython.org
Find the uPython firmware for your board.
uPython firmware for development boards.
ESP32 uPython modules and function examples.
Quick reference for the ESP32
Scanning and connecting to multiple SSIDs
Using an SD card reader breakout board
Storing variables in the RTC
OTA Updating running software without interuption
Future articles
Communication between ESPs
Using an Adafruit GPS module
Hassle free web communication with urequests.py.