Page 1 of 1

ESP8266 RTC memory ?

Posted: Wed Mar 15, 2017 9:07 pm
by Niallp
Is there any MicroPython mechanism for using the RTC memory on the ESP8266 ? Or is it already in use internally ? I'd like to retain some values between deep sleeps. (didn't find anything in the documentation).

thx.

Re: ESP8266 RTC memory ?

Posted: Wed Jul 05, 2017 11:52 am
by gdr
Same question. rtc.memory() does not save anything.

>>> import machine
>>> rtc = machine.RTC()
>>> rtc.memory(b'hello')
>>> rtc.memory()
b'hello'

restart board

>>> import machine
>>> rtc = machine.RTC()
>>> rtc.memory()
b''

Re: ESP8266 RTC memory ?

Posted: Thu Jul 06, 2017 4:07 am
by Damien
@gdr what do you do when you "restart board"? I've tested your code and it works (the memory is retained) when the esp8266 is hard-reset (using the RST button on the Adafruit Feather Huzzah board). The memory is also retained after waking from deepsleep.

Re: ESP8266 RTC memory ?

Posted: Mon Jul 10, 2017 3:05 pm
by g-sam
I believe I have encountered the same problem as @gdr, but things are a little more complicated: sometimes, rtc.memory does succeed, but not always...Try the following:

==================================
import machine
import esp

INIT_SECONDS = 23

rtc = machine.RTC()

print('woken with task', rtc.memory())

if rtc.memory() == b'init':
rtc.memory(b'read')
print('going to sleep, will wake to ', rtc.memory())
esp.deepsleep(INIT_SECONDS * 1000000)

rtc.memory(b'init')
=================================

This produces an infinite loop like:
=======================
woken with task b'init'
going to sleep, will wake to b'read'
woken with task b'init'
going to sleep, will wake to b'read'
=======================

Note, before the board enters deepsleep, the memory appears to have been set to 'read', but upon waking it is always found to be 'init'. Very frustrating!

I have only encountered this when going to sleep immediately after writing, but it does seem to be a bug. Ideas, anyone?

Re: ESP8266 RTC memory ?

Posted: Mon Jul 10, 2017 3:55 pm
by g-sam
I have figured out that the problem with the code above has nothing to do with RTC memory, but rather with esp.deepsleep. The code continues to run after calling the latter, so the memory is getting set back to 'init'. I could fix it by adding time.sleep(1) after the call to deepsleep. This seems counterintuitive, though -- and is that behaviour documented anywhere?

Re: ESP8266 RTC memory ?

Posted: Fri Jul 14, 2017 10:30 am
by deshipu
Nothing gets executed after the deepsleep. The only way to exit deepsleep is to reset the board, and of course the code runs from the beginning then.

Re: ESP8266 RTC memory ?

Posted: Sat Jul 15, 2017 8:47 pm
by g-sam
So I would have expected. But then what is wrong with the code above, and why does adding a time.sleep after the deepsleep fix the problem?

Re: ESP8266 RTC memory ?

Posted: Sun Jul 16, 2017 4:11 pm
by cefn
Anecdotally I seem to have seen code running after machine.reset()

See
Interestingly the text "Visiting box 3" is actually from a few lines later than the machine.reset() request, suggesting that things are running on for some reason.
from viewtopic.php?f=16&t=3582&p=20784&hilit=visiting#p20784

Is reset() or deepsleep() perhaps something which is scheduled to run, rather than run immediately?