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?
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);
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?
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.