I am new to photon,
I realize photon can be put on sleep mode using System.sleep function,
How do I wake it up from deep sleep mode at specific time of day and do tasks
e.g.
I want to wake it up at 8am 1pm 5pm 10pm daily and do certain action
Both - stop mode sleep and standby (aka deep sleep) - allow for a timed wake by providing a sleep duration parameter (in seconds).
If you want your device to wake at a specific time, you need to calculate the difference between your targeted wake time and the current time and pass that to the System.sleep() function.
There are several threads in this forum that show ways to do that.
My commom approach is using Time.local() and a % 86400 (modulo) calculation.
Building on what ScruffR has said. If your requirement is to wake at very specific times (i.e accuracy is important) then another approach is to using an external RTC (Real time clock) with an alarm function. When the time arrives the RTC will throw an interrupt (typically pull a pin low) - the Photon can be setup to use the WKP pin - this is the only one which will wake/restart it when in deep sleep. Having said this, the RTC on the Photon pretty good and therefore you can use System.sleep(SLEEP_MODE_DEEP, seconds_to_your_next_wake_time); Where seconds_to_your_next_wake_time has been calculated as indicated. Remember that after a wake from deep sleep the system will restart, so you may want to store some context in EEPROM before going to sleep.
Not sure why you declare your hours array with 23 entries when you only use 4.
I’d also stay clear of String and rather use snprintf() to convert numerics into strings.
This way you can also unify your three events into one single one.
When you detect a negative difference a simple addition of 86400 (24 not 23 times 3600) should do the trick.
Or alternatively you can just unify the whole calculation into one single statement.
Finally, in getNextHours()sizeof(hours) would only return the correct value if you declared your array like this
uint8_t hours[] = { 8, 13, 17, 22 };
sizeof() returns the number of bytes (one int is four bytes), not the number of elements and as said above you’d have 23 elements with only 4 populated.
As you have got it your for() would run up to 92 times only rendering valid entries on the first four iterations, then 19 times zero and after that some random values (i.e. after 22:00).