0

I'm working on a low power device that implements an ESP8266 MCU and uses it's deep sleep feature. The firmware uses the Arduino C++ library.

Part of the functionality requires knowing the time. To do this, I am using configTime to get the time (i.e. NTP). But, to reduce power consumption, I only connect to WiFi every 24 hours. The ESP8266 wakes every hour (to decide if it should do something or go back to sleep). To keep time, I record the last known unix timestamp to RTC memory and upon waking add the amount of time that the device was told to deep sleep (i.e. 1 hour).

The flaw in this design: Part of the requirement is that the user needs to be able to wake the device manually with a button press and interrupt the normal sleep pattern, but if the device is woken with the reset button, it adds an hour to the time and the time becomes completely inaccurate (because an hour was added, even though it could be seconds since deep sleep started).

Is there a way, using the ESP8266 API, to tell how long the device was actually in deep sleep? That way, on wake, instead of blindly adding 1 hour to the RTC-saved unix timestamp, I can add the amount of time that the MCU was in deep sleep. The datasheet seems to only mention the RTC a few times in not much detail.

If no firmware solution exists, what hardware solution could I use?

Thinking out loud: I think a compromise could be to detect if the reset button was pressed (latch/flip flop/register, maybe?). In this case I still wouldn't be able to tell how long the MCU was asleep, but at least I'd be able to tell if I should add time. I found that system_get_rst_info is based on how the device went to sleep (i.e. if woken from deep sleep by pressing the reset button, the reason is REANSON_DEEP_SLEEP_AWAKE), so I couldn't see how to make use of that in this case. I suppose I could add an external RTC, negating the need to calculate how long the MCU was in deep sleep, and just use that external IC for time keeping.

Edit: Apparently, rtctime.dsleep() provides time keeping across deep sleep, but it's LUA only so not useful to me. The source is written in C, but I can't see anything that would help.

Time is kept across the deep sleep. I.e. rtctime.get() will keep working (provided time was available before the sleep).

2
  • 1
    The hardware solution could be a flip/flop or scr or latching bjt thingie that's clamped by a physical button press that also uses a fet to pull down rst. You can then quickly query that one bit of state on boot to determine if a button did it. You then reset the latch/JK/SCR with another GPIO. So, it burns two GPIOs but works well.
    – dandavis
    Jan 12, 2023 at 22:35
  • @dandavis Good idea, I'll give that a try. Jan 17, 2023 at 14:10

2 Answers 2

0

I believe the answer is no (you cannot tell exactly how long an ESP8266 was asleep), given these constraints:

  1. If an external RTC cannot be used.
  2. If the MCU does not connect to Wi-Fi every time it wakes.
  3. If the MCU can be woken arbitrarily by the user pressing the reset button.

In other words, if the ESP8266 does not connect to Wi-Fi every time it wakes, you can only estimate the time based on how long it should have been in deep sleep, but you can't know for sure without either going online or using an external RTC.

-1

save timestamp in NVRAM, the time before sleep. once wakeup, simply subtract the the data saved in NVRAM from the current time.

2
  • What if I sleep for 1 hour, but then press the reset button 1 second later? The time would be 59 mins and 59 seconds out, as I mentioned in my question. Jan 16, 2023 at 20:43
  • I always use unix timestamp in millisecond ie... gettimeofday(&tv,NULL); time_stamp[ptr] = (int64_t)(tv.tv_sec*1000LL + tv.tv_usec /(1000LL)); Jan 17, 2023 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.