Auto Safe-Mode after repeated SOS (bad code saviour)

Hi all,

Unsure where this code came from. Feels like I’ve been using it for years but I was especially thankful this morning when a coding mistake caused an Argon to SOS which isn’t easily accessible.

Hopefully it’s helpful to someone else too.

https://go.particle.io/shared_apps/5ee4aba1fbd3d5000775e479

retained uint32_t lastHardResetTime;
retained int resetCount;

// Stores the last boot time. Not used for
// auto safe-mode but useful to have available. 
unsigned long resetTime;


SYSTEM_THREAD(ENABLED);
STARTUP(System.enableFeature(FEATURE_RESET_INFO));

void setup() {

    waitFor(Particle.connected, 30000);
    
    do {
        resetTime = Time.now();
        Particle.process();
    } while (resetTime < 1550000000 || millis() < 30000);
    
    if (System.resetReason() == RESET_REASON_PANIC) {
        // We're only concerned about hard resets happening in sub-2 minutes.
        // Anything greater and we should be albe to flash new code normally.
        if ((Time.now() - lastHardResetTime) < 120) {
            resetCount++;
        } else {
            resetCount = 1;
        }

        lastHardResetTime = Time.now();

        if (resetCount > 3) {
            System.enterSafeMode();
        }
    } else {
        resetCount = 0;
    }

    Particle.publish("LOG", String::format("I'm awake! - %d", resetCount), PRIVATE);
}

void loop() {
    // Uncomment to cause a crash
    abort();
}
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.