PaulStoffregen/Time

Is this library compatible with any deep-sleep mode

Jorfuenst opened this issue · 3 comments

For example, ESP32 deep-sleep.

Thanks a lot.

kolen commented

It relies on millis()

Time/Time.cpp

Lines 250 to 253 in 6d0fc5e

time_t now() {
// calculate number of seconds passed since last call to now()
while (millis() - prevMillis >= 1000) {
// millis() and prevMillis are both unsigned ints thus the subtraction will always be the absolute value of the difference

which in turn uses:

Both functions return time that is reset on deep sleep. So no, it's incompatible, you can use it, but it resets after deep sleep.

As "rtc clock", which continues to work in deep sleep mode, is too imprecise and unreliable (quartz oscillator is disabled in deep sleep), it requires sophisticated handling. Nodemcu has implementation for it, and it's quite complex.

I am having great luck using this time library with pwrDownMode() from Sleep_n0m1 library. I go into pwrDownMode ( "deep sleep" ) for 10 seconds and then I ADVANCE the millis() timer directly to account for the milliseconds lost during the sleep. Some forum posts say it's bad to do that, but it works in practice for me. YMMV

  extern volatile unsigned long timer0_millis; //<<this line before setup() !
  #include <Sleep_n0m1.h> //<<this line before setup() !
  Sleep cpu_sleep; //<<this line before setup() !
//following in setup/loop:
  cpu_sleep.pwrDownMode(); //set sleep mode
  cpu_sleep.sleepDelay(10000); //actual sleep
  noInterrupts ();
  timer0_millis = millis()+9988L; //long int 9988
  interrupts ();