For preserving variables during the deepsleep it is recomended to save them in RTC memory.
64 integer variables are available (indexed 0~63) and one string variable with max length of 2047 bytes.
CRC16 is used to guarantee the integrity of the saved values.
The string variable can, for example, contain json string with current working state which has to be restored after deepsleep wake-up.
Code: Select all
>>> import machine
>>> rtc=machine.RTC()
>>> rtc.read_string()
>>> rtc.write_string('This string is preserved after deepsleep wake-up')
True
>>> rtc.write(1,123456)
True
>>> rtc.write(10,654321)
True
>>> machine.deepsleep(10000)
ESP32: DEEP SLEEP
...
...
FreeRTOS running on BOTH CORES, MicroPython task started on App Core.
uPY stack size = 19456 bytes
uPY heap size = 81920 bytes
D (275) intr_alloc: Connected src 34 to int 3 (cpu 1)
Reset reason: Deepsleep wake-up
Wakeup source: RTC wake-up
D (285) intr_alloc: Connected src 22 to int 4 (cpu 1)
MicroPython ESP32_LoBo_v2.0.6 - 2017-10-24 on ESP32 board with ESP32
Type "help()" for more information.
>>> import machine
>>> rtc=machine.RTC()
>>> rtc.read_string()
'This string is preserved after deepsleep wake-up'
>>> rtc.read(1)
123456
>>> rtc.read(10)
654321
>>>