I’m trying to make it so if my electron doesn’t connect to the cloud after 5 minutes, but keeps trying, it will go back to deep sleep mode for another hour and then try again.
Here is the important part of the code that I’m using.
> ApplicationWatchdog wd(WATCHDOG_TIME, sleepHour); // WATCHDOG_TIME is defined as 300000
> void sleepHour() {
> System.sleep(SLEEP_MODE_SOFTPOWEROFF, WATCHDOG_SLEEP_TIME); //WATCHDOG_SLEEP_TIME is defined as 3600 seconds
> }
It seems after the prescribed 5 minutes it doesn’t do the sleepHour() function, but rather the LED flashes red (an SOS pattern that doesn’t match the shown SOS patterns in the particle documentation.
Any ideas on what’s going on, and what I need to change or another way to do what I’m attempting to do?
Cellular.connect(); // This command turns on the Cellular Modem and tells it to connect to the cellular network.
if (!waitFor(Cellular.ready, 600000)) { //If the cellular modem does not successfuly connect to the cellular network in 10 mins then go back to sleep via the sleep command below. After 5 mins of not successfuly connecting the modem will reset.
System.sleep(D0, RISING,sleepInterval * 2, SLEEP_NETWORK_STANDBY); //Put the Electron into Sleep Mode for 2 Mins + leave the Modem in Sleep Standby mode so when you wake up the modem is ready to send data vs a full reconnection process.
}
I have used the Application Watchdog like this in the same sketch:
ApplicationWatchdog wd(660000, System.reset); //This Watchdog code will reset the processor if the dog is not kicked every 11 mins which gives time for 2 modem reset's.
I believe the problem is that you can’t call System.sleep from an application watchdog callback. It’s either because the stack is too small, or you can’t call System.sleep from a non-loop thread, or maybe both, but in any case I don’t think it works.
Pretty much the only thing you can safely do is call System.reset.
In order to stop connecting to the cloud you should instead use system threading mode and monitor the connection state from loop. This is a code example that does that:
Thank you @rickkas7 and @RWB I’ll try those out. That seems like a great way to do it, and I was wondering if that was an issue with the ApplicationWatchdog.
@rickkas7 I got things working with the system threading enabled, but it made it so my OTA updates don’t trigger before my device goes to sleep. System.updatesPending() == TRUE never happens with System threading mode. Any suggestions to get both of these needed features (OTA updates, and system thread enabled)? I can’t afford the loss of battery life by keeping my device on for a longer period of time.