diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 32989fd2..9422ba1d 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -82,11 +82,15 @@ #endif //------------------------------------------------------------------------------ -// CRASH +// SYSTEM CHECK //------------------------------------------------------------------------------ -#define CRASH_SAFE_TIME 60000 // The system is considered stable after these many millis -#define CRASH_COUNT_MAX 5 // After this many crashes on boot +#ifndef SYSTEM_CHECK_ENABLED +#define SYSTEM_CHECK_ENABLED 1 // Enable crash check by default +#endif + +#define SYSTEM_CHECK_TIME 60000 // The system is considered stable after these many millis +#define SYSTEM_CHECK_MAX 5 // After this many crashes on boot // the system is flagged as unstable //------------------------------------------------------------------------------ diff --git a/code/espurna/debug.ino b/code/espurna/debug.ino index 0e2c90ef..0bdea8bd 100644 --- a/code/espurna/debug.ino +++ b/code/espurna/debug.ino @@ -33,7 +33,9 @@ void debugSend(const char * format, ...) { #endif #if DEBUG_UDP_SUPPORT + #if SYSTEM_CHECK_ENABLED if (systemCheck()) { + #endif udpDebug.beginPacket(DEBUG_UDP_IP, DEBUG_UDP_PORT); udpDebug.write(buffer); if (len > DEBUG_MESSAGE_MAX_LENGTH) { @@ -41,7 +43,9 @@ void debugSend(const char * format, ...) { } udpDebug.endPacket(); delay(1); + #if SYSTEM_CHECK_ENABLED } + #endif #endif #if DEBUG_TELNET_SUPPORT @@ -70,7 +74,9 @@ void debugSend_P(PGM_P format, ...) { #endif #if DEBUG_UDP_SUPPORT + #if SYSTEM_CHECK_ENABLED if (systemCheck()) { + #endif udpDebug.beginPacket(DEBUG_UDP_IP, DEBUG_UDP_PORT); udpDebug.write(buffer); if (len > DEBUG_MESSAGE_MAX_LENGTH) { @@ -78,7 +84,9 @@ void debugSend_P(PGM_P format, ...) { } udpDebug.endPacket(); delay(1); + #if SYSTEM_CHECK_ENABLED } + #endif #endif #if DEBUG_TELNET_SUPPORT diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 7b62fa97..1d55c611 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -53,14 +53,6 @@ void hardwareSetup() { void hardwareLoop() { - // System check - static bool checked = false; - if (!checked && (millis() > CRASH_SAFE_TIME)) { - // Check system as stable - systemCheck(true); - checked = true; - } - // Heartbeat static unsigned long last_uptime = 0; if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) { @@ -225,7 +217,9 @@ void setup() { hardwareSetup(); // Question system stability - systemCheck(false); + #if SYSTEM_CHECK_ENABLED + systemCheck(false); + #endif // Show welcome message and system configuration welcome(); @@ -243,7 +237,9 @@ void setup() { #endif // Do not run the next services if system is flagged stable - if (!systemCheck()) return; + #if SYSTEM_CHECK_ENABLED + if (!systemCheck()) return; + #endif #if WEB_SUPPORT webSetup(); @@ -316,7 +312,10 @@ void loop() { otaLoop(); // Do not run the next services if system is flagged stable - if (!systemCheck()) return; + #if SYSTEM_CHECK_ENABLED + systemCheckLoop(); + if (!systemCheck()) return; + #endif #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE lightLoop(); diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index b4c1ca2e..c0d8417a 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -141,9 +141,11 @@ unsigned char customReset() { // ----------------------------------------------------------------------------- +#if SYSTEM_CHECK_ENABLED + // Call this method on boot with start=true to increase the crash counter // Call it again once the system is stable to decrease the counter -// If the counter reaches CRASH_COUNT_MAX then the system is flagged as unstable +// If the counter reaches SYSTEM_CHECK_MAX then the system is flagged as unstable // setting _systemOK = false; // // An unstable system will only have serial access, WiFi in AP mode and OTA @@ -156,7 +158,7 @@ void systemCheck(bool stable) { value = 0; DEBUG_MSG_P(PSTR("[MAIN] System OK\n")); } else { - if (++value > CRASH_COUNT_MAX) { + if (++value > SYSTEM_CHECK_MAX) { _systemStable = false; value = 0; DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n")); @@ -170,6 +172,17 @@ bool systemCheck() { return _systemStable; } +void systemCheckLoop() { + static bool checked = false; + if (!checked && (millis() > SYSTEM_CHECK_TIME)) { + // Check system as stable + systemCheck(true); + checked = true; + } +} + +#endif + // ----------------------------------------------------------------------------- char * ltrim(char * s) { diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index b6a039c9..370d0664 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -66,7 +66,9 @@ void wifiConfigure() { jw.cleanNetworks(); // If system is flagged unstable we do not init wifi networks - if (!systemCheck()) return; + #if SYSTEM_CHECK_ENABLED + if (!systemCheck()) return; + #endif int i; for (i = 0; i< WIFI_MAX_NETWORKS; i++) {