Photon no Cloud reset routine?

I’ve adapted @rickkas7 electron code for the Photon.

Basically I have a Photon as an MQTT client which for some reason doesn’t recover its Cloud connection if the WiFi ever drops out.

Would kind of be amazed if this didn’t already exist in some other form…? Posting here in case it’s useful to someone. Also in case anyone wishes to make “numfailures” actually DO something. At the moment it doesn’t. It does in @rickkas7 original code for the Electron.

uint32_t numFailures = 0;
unsigned long cloudCheckStart = 0;
unsigned long cloudWaitForReboot = 180000; // milliseconds
bool temp = false;
bool isCloudConnected = false;


void setup() {
          //Serial.begin(115200);


}

void loop() {
        temp = Particle.connected();
        if (temp != isCloudConnected) {
                        //Serial.println("Cloud connection state changed");

                // Cloud connection state changed
                isCloudConnected = temp;

                if (isCloudConnected) {
                        //Serial.println("Upon succesful connection, clear the failure count");
                        // Upon succesful connection, clear the failure count
                        numFailures = 0;
                } else {
                        //Serial.println("Cloud just disconnected, start measuring how long we've been disconnected");
                        // Cloud just disconnected, start measuring how long we've been disconnected
                        cloudCheckStart = millis();
                }
        }

        if (!isCloudConnected) {
                //Serial.println("Not connected to the cloud - check to see if we've spent long enough in this state to reboot");
                // Not connected to the cloud - check to see if we've spent long enough in this state to reboot
                if (cloudWaitForReboot != 0 && millis() - cloudCheckStart >= cloudWaitForReboot) {
                        //Serial.println("// The time to wait to connect to the cloud has expired, reboot");
                        // The time to wait to connect to the cloud has expired, reboot
                        numFailures++;
                        System.reset();
                }
        }
}
1 Like