Hey folks,
I’m working on getting my reconnect loop to work after a long sleep interval (48 minutes).
When resuming from a long sleep I attempt a Publish, if that fails I enter a reconnect loop.
3 attempts to reconnect failing triggers a system reset (which always works).
I’d really like to avoid the system reset (losing SRAM state and other retained data), however so far I cannot get this reconnect loop to work consistently.
Running in SYSTEM_MODE set to SEMI_AUTOMATIC, with SYSTEM_THREAD set to ENABLED.
The timeout for reconnect is 300000ms.
bool ready = false;
Particle.disconnect();
#if (PLATFORM_ID == 10)
Cellular.disconnect();
// 16:MT silent reset (with detach from network and saving of NVM parameters), with reset of the SIM card
Cellular.command(30000, "AT+CFUN=16\r\n");
Cellular.off();
delay(500);
Cellular.on();
Cellular.connect();
delay(500);
Particle.process();
waitFor(Cellular.ready, CLOUD_RECONNECTION_MODEM_TIMEOUT_MS);
ready = Cellular.ready();
#elif (PLATFORM_ID == 6)
WiFi.disconnect();
WiFi.off();
delay(500);
WiFi.on();
WiFi.connect();
delay(500);
Particle.process();
waitFor(WiFi.ready, CLOUD_RECONNECTION_MODEM_TIMEOUT_MS);
ready = WiFi.ready();
#endif
// We should be ready to Particle.publish here
if (!ready)
{
System.reset(); // Scorched earth, I want this to never be called
}
It might be worth noting that the failure rate on the Photon is MUCH higher than the Electron.
I’m wondering if I should also be calling and waiting for Particle.connect here as well?
We retain timers and some state, but we have a series of startup routines that essentially reset all of our timing (protecting against power loss and getting initial config from our cloud). For this reason it would be great to avoid the reset call.
I guess the big question is what is the System.reset() doing to the modem and CPU that the reconnect logic can’t replicate? A reset always triggers a successful reconnect, but the reconnect logic often fails (forcing a System.reset).
We could check state at startup or try to record a power-loss reason (we use coin-cells for RTC backup) to mitigate the issues, but I’d really like to understand what the flaws are in this code, and improve my understanding of what these things do in the context of the network and cloud connection status.
I’ve had some success using waitFor and Particle.connect() on wake.
It works for electrons reasonably well. Seeing fewer system resets.
When I do need a full modem reset I’ve been using this code, but I don’t think it’s been hit yet:
#if (PLATFORM_ID == 10)
Serial.println("Resetting Cellular Modem");
Cellular.command(30000, "AT+CFUN=16\r\n");
// Give the modem time to respond
delay(1000);
Cellular.off();
delay(1000);
Cellular.on();
ready = waitFor(Cellular.ready, CLOUD_RECONNECTION_MODEM_TIMEOUT_MS);
#endif
// Note: Photons (platform id 6) seem to always need a system reset
// with system firmware 0.5.3
if (!ready)
{
System.reset();
}