Browse Source

Check system stability and run only AP and OTA if unstable

fastled
Xose Pérez 7 years ago
parent
commit
daa5a09058
5 changed files with 93 additions and 21 deletions
  1. +10
    -1
      code/espurna/config/general.h
  2. +16
    -12
      code/espurna/debug.ino
  3. +29
    -8
      code/espurna/espurna.ino
  4. +35
    -0
      code/espurna/utils.ino
  5. +3
    -0
      code/espurna/wifi.ino

+ 10
- 1
code/espurna/config/general.h View File

@ -57,6 +57,14 @@
#define TERMINAL_SUPPORT 1 // Enable terminal commands #define TERMINAL_SUPPORT 1 // Enable terminal commands
#endif #endif
//------------------------------------------------------------------------------
// CRASH
//------------------------------------------------------------------------------
#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
// the system is flagged as unstable
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// EEPROM // EEPROM
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -65,7 +73,8 @@
#define EEPROM_RELAY_STATUS 0 // Address for the relay status (1 byte) #define EEPROM_RELAY_STATUS 0 // Address for the relay status (1 byte)
#define EEPROM_ENERGY_COUNT 1 // Address for the energy counter (4 bytes) #define EEPROM_ENERGY_COUNT 1 // Address for the energy counter (4 bytes)
#define EEPROM_CUSTOM_RESET 5 // Address for the reset reason (1 byte) #define EEPROM_CUSTOM_RESET 5 // Address for the reset reason (1 byte)
#define EEPROM_DATA_END 6 // End of custom EEPROM data block
#define EEPROM_CRASH_COUNTER 6 // Address for the crash counter (1 byte)
#define EEPROM_DATA_END 7 // End of custom EEPROM data block
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// HEARTBEAT // HEARTBEAT


+ 16
- 12
code/espurna/debug.ino View File

@ -33,13 +33,15 @@ void debugSend(const char * format, ...) {
#endif #endif
#if DEBUG_UDP_SUPPORT #if DEBUG_UDP_SUPPORT
udpDebug.beginPacket(DEBUG_UDP_IP, DEBUG_UDP_PORT);
udpDebug.write(buffer);
if (len > DEBUG_MESSAGE_MAX_LENGTH) {
udpDebug.write(" (...)\n");
if (systemCheck()) {
udpDebug.beginPacket(DEBUG_UDP_IP, DEBUG_UDP_PORT);
udpDebug.write(buffer);
if (len > DEBUG_MESSAGE_MAX_LENGTH) {
udpDebug.write(" (...)\n");
}
udpDebug.endPacket();
delay(1);
} }
udpDebug.endPacket();
delay(1);
#endif #endif
} }
@ -64,13 +66,15 @@ void debugSend_P(PGM_P format, ...) {
#endif #endif
#if DEBUG_UDP_SUPPORT #if DEBUG_UDP_SUPPORT
udpDebug.beginPacket(DEBUG_UDP_IP, DEBUG_UDP_PORT);
udpDebug.write(buffer);
if (len > DEBUG_MESSAGE_MAX_LENGTH) {
udpDebug.write(" (...)\n");
if (systemCheck()) {
udpDebug.beginPacket(DEBUG_UDP_IP, DEBUG_UDP_PORT);
udpDebug.write(buffer);
if (len > DEBUG_MESSAGE_MAX_LENGTH) {
udpDebug.write(" (...)\n");
}
udpDebug.endPacket();
delay(1);
} }
udpDebug.endPacket();
delay(1);
#endif #endif
} }


+ 29
- 8
code/espurna/espurna.ino View File

@ -47,6 +47,14 @@ void hardwareSetup() {
void hardwareLoop() { void hardwareLoop() {
// System check
static bool checked = false;
if (!checked && (millis() > CRASH_SAFE_TIME)) {
// Check system as stable
systemCheck(true);
checked = true;
}
// Heartbeat // Heartbeat
static unsigned long last_uptime = 0; static unsigned long last_uptime = 0;
if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) { if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) {
@ -198,15 +206,29 @@ void welcome() {
void setup() { void setup() {
// Init EEPROM, Serial and SPIFFS
hardwareSetup(); hardwareSetup();
// Question system stability
systemCheck(false);
// Show welcome message and system configuration
welcome(); welcome();
// Init persistance and terminal features
settingsSetup(); settingsSetup();
if (getSetting("hostname").length() == 0) { if (getSetting("hostname").length() == 0) {
setSetting("hostname", getIdentifier()); setSetting("hostname", getIdentifier());
saveSettings(); saveSettings();
} }
delay(500);
wifiSetup();
otaSetup();
// Do not run the next services if system is flagged stable
if (!systemCheck()) return;
#if WEB_SUPPORT #if WEB_SUPPORT
webSetup(); webSetup();
#endif #endif
@ -217,11 +239,6 @@ void setup() {
relaySetup(); relaySetup();
buttonSetup(); buttonSetup();
ledSetup(); ledSetup();
delay(500);
wifiSetup();
otaSetup();
mqttSetup(); mqttSetup();
#ifdef ITEAD_SONOFF_RFBRIDGE #ifdef ITEAD_SONOFF_RFBRIDGE
@ -276,13 +293,17 @@ void setup() {
void loop() { void loop() {
hardwareLoop(); hardwareLoop();
settingsLoop();
wifiLoop();
otaLoop();
// Do not run the next services if system is flagged stable
if (!systemCheck()) return;
buttonLoop(); buttonLoop();
relayLoop(); relayLoop();
ledLoop(); ledLoop();
wifiLoop();
otaLoop();
mqttLoop(); mqttLoop();
settingsLoop();
#ifdef ITEAD_SONOFF_RFBRIDGE #ifdef ITEAD_SONOFF_RFBRIDGE
rfbLoop(); rfbLoop();


+ 35
- 0
code/espurna/utils.ino View File

@ -122,6 +122,8 @@ void heartbeat() {
} }
// -----------------------------------------------------------------------------
void customReset(unsigned char status) { void customReset(unsigned char status) {
EEPROM.write(EEPROM_CUSTOM_RESET, status); EEPROM.write(EEPROM_CUSTOM_RESET, status);
EEPROM.commit(); EEPROM.commit();
@ -137,6 +139,39 @@ unsigned char customReset() {
return status; return status;
} }
// -----------------------------------------------------------------------------
// 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
// setting _systemOK = false;
//
// An unstable system will only have serial access, WiFi in AP mode and OTA
bool _systemStable = true;
void systemCheck(bool stable) {
unsigned char value = EEPROM.read(EEPROM_CRASH_COUNTER);
if (stable) {
value = 0;
DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
} else {
if (++value > CRASH_COUNT_MAX) {
_systemStable = false;
value = 0;
DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
}
}
EEPROM.write(EEPROM_CRASH_COUNTER, value);
EEPROM.commit();
}
bool systemCheck() {
return _systemStable;
}
// -----------------------------------------------------------------------------
char * ltrim(char * s) { char * ltrim(char * s) {
char *p = s; char *p = s;
while ((unsigned char) *p == ' ') ++p; while ((unsigned char) *p == ' ') ++p;


+ 3
- 0
code/espurna/wifi.ino View File

@ -59,6 +59,9 @@ void wifiConfigure() {
jw.setAPMode(WIFI_AP_MODE); jw.setAPMode(WIFI_AP_MODE);
jw.cleanNetworks(); jw.cleanNetworks();
// If system is flagged unstable we do not init wifi networks
if (!systemCheck()) return;
int i; int i;
for (i = 0; i< WIFI_MAX_NETWORKS; i++) { for (i = 0; i< WIFI_MAX_NETWORKS; i++) {
if (getSetting("ssid" + String(i)).length() == 0) break; if (getSetting("ssid" + String(i)).length() == 0) break;


Loading…
Cancel
Save