Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By Andrew Grande
#40801 Hi Guys,

Ran into an issue (of course!) with the deep sleep not accepting a number large enough (would like to wake up once or twice a day only). Need to get creative :)

So the approach I was considering was to sleep for as long as it can and check and increment a persistent counter first-thing on wake up. It will serve as a multiplier to get us to a desired sleep duration without wasting energy on wifi connection and server comms.

Now, how to make this counter survive the reset. I found 2 options:
  • Low-level EEPROM
  • Write a small file on SPIFFS

With EEPROM it's nice and small, although a little low-level. I'd like to understand if I should avoid any specific memory segments on purpose? E.g. with the anticipation that I will also have a generic config file there in the future too, so they don't clash.

With SPIFFS it's operating with a higher level API and easy to use. Maybe an overkill (if EEPROM path is safe enough?)

An important consideration - need a way to reset a counter for the first-time deployment. Thought of:
  • Wire a button (long-hold), or a (dip) switch which would be read and EEPROM/SPIFFS file erased if a condition detected.
  • Any other nice trick maybe?

Someone did mention the max sleep they could get out of esp8266 was around half an hour only, suggesting it was a 31 bit integer or something only? Can someone share their experience with that?
User avatar
By lethe
#40811 The recommended way would be to use the RTC memory. There are up to 512 bytes available in RTC RAM, which will survive deep sleep and not wear down your flash memory. See system_rtc_mem_write/ system_rtc_mem_read in the API doc.
For maximum power saving you should also set system_deep_sleep_set_option to disable wifi on all wake-ups but the last one, where you actually want to transmit data.
You should also read section 9.5 of the API document.
User avatar
By Barnabybear
#40821 Hi, i'm sure you'll get this working internaly, but if not you could consider using a 74HC393 dual 4-bit counter. It only draws 4uA, if you connected the GPIO 16 -> reset link to the 'count in' this would increment as soon as the ESP woke. You could check which ever counter output is best for your application and chose wether to go back to sleep or not. This would give you the options to divide by 2, 4, 8, 16, 32, 64 or 128. In theory something like 5 days, without having to anything other than wake check to see if a GPIO is high and goback to sleep, you should be able to do that in under a second. There is a way to reset the counter but it's not easy to explain in text - PM me if you want a drawing.