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:
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.
int secToSleep = timeToWake - Time.local();
Thank you for your insight. This of course makes sense. I will study your reply, still learning…
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
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);