Electron Power Save Question

Good day:
I desire to have our Electron go into deep power save mode for 6 hours and then wake/report/sleep.
I have used the following code:

System.sleep(SLEEP_MODE_DEEP,21600);

It works, but the interval timing goes off after the first sleep, and the wakeup time gets pushed further ahead each period. Can you suggest the proper way to code this for someone who doesn’t know any better?

Thanks so much!

You need to consider the time the device needs to reconnect, do its job and then go back to sleep. With a constant sleep period this extra time will always push the wake time on further and further.

You need to calculate the time till your next scheduled wake and use that instead.
something like

 int secToSleep = timeToWake - Time.local();
 System.sleep(SLEEP_MODE_DEEP, secToSleep);
1 Like

Thank you for your insight. This of course makes sense. I will study your reply, still learning…

1 Like

I put at the beginning of my code: int secToSleep = 21600 - Time.local();
And at the end: System.sleep(SLEEP_MODE_DEEP, secToSleep);
It compiled OK, but 6hrs later, no wake up?

Sorry to be so stupid about this…

timeToWake should be a UNIX epoch timestamp just as Time.local() is.

If you want a timestamp without date you can use the modulo function to only keep the time portion of the timestamp.

uint32_t currSecOfDay = Time.local() % 86400;
uint32_t timeToWake = 12*3600 + 00*60 + 00; // 12 hours 0 minutes 0 seconds
System.sleep(SLEEP_MODE_DEEP, timeToWake - currSecOfDay);

Alternatively you could just subtract the number of seconds you already are onto the current 6 hour block of the day, which would result in a wake at 0:00, 6:00, 12:00 and 18:00.

System.sleep(SLEEP_MODE_DEEP, 21600 - Time.local() % 21600);

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.