diff --git a/code/espurna/button.ino b/code/espurna/button.ino index b11aa986..439ab31c 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -93,11 +93,15 @@ void buttonEvent(unsigned int id, unsigned char event) { } } if (action == BUTTON_MODE_AP) createAP(); - if (action == BUTTON_MODE_RESET) ESP.restart(); + if (action == BUTTON_MODE_RESET) { + customReset(CUSTOM_RESET_HARDWARE); + ESP.restart(); + } if (action == BUTTON_MODE_PULSE) relayPulseToggle(); if (action == BUTTON_MODE_FACTORY) { DEBUG_MSG_P(PSTR("\n\nFACTORY RESET\n\n")); settingsFactoryReset(); + customReset(CUSTOM_RESET_FACTORY); ESP.restart(); } diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 1361da29..432c1e51 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -29,7 +29,41 @@ #define EEPROM_RELAY_STATUS 0 #define EEPROM_ENERGY_COUNT 1 -#define EEPROM_DATA_END 5 +#define EEPROM_CUSTOM_RESET 5 +#define EEPROM_DATA_END 6 + +//-------------------------------------------------------------------------------- +// RESET +//-------------------------------------------------------------------------------- + +#define CUSTOM_RESET_HARDWARE 1 +#define CUSTOM_RESET_WEB 2 +#define CUSTOM_RESET_TERMINAL 3 +#define CUSTOM_RESET_MQTT 4 +#define CUSTOM_RESET_RPC 5 +#define CUSTOM_RESET_OTA 6 +#define CUSTOM_RESET_NOFUSS 8 +#define CUSTOM_RESET_UPGRADE 9 +#define CUSTOM_RESET_FACTORY 10 + +#define CUSTOM_RESET_MAX 10 + +#include + +PROGMEM const char custom_reset_hardware[] = "Hardware button"; +PROGMEM const char custom_reset_web[] = "Reset from web interface"; +PROGMEM const char custom_reset_terminal[] = "Reset from terminal"; +PROGMEM const char custom_reset_mqtt[] = "Reset from MQTT"; +PROGMEM const char custom_reset_rpc[] = "Reset from RPC"; +PROGMEM const char custom_reset_ota[] = "Reset after successful OTA update"; +PROGMEM const char custom_reset_nofuss[] = "Reset after successful NoFUSS update"; +PROGMEM const char custom_reset_upgrade[] = "Reset after successful web update"; +PROGMEM const char custom_reset_factory[] = "Factory reset"; +PROGMEM const char* const custom_reset_string[] = { + custom_reset_hardware, custom_reset_web, custom_reset_terminal, + custom_reset_mqtt, custom_reset_rpc, custom_reset_ota, + custom_reset_nofuss, custom_reset_upgrade, custom_reset_factory +}; //-------------------------------------------------------------------------------- // BUTTON diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 940140a1..2e79c9c6 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -104,6 +104,21 @@ void heartbeat() { } +void customReset(unsigned char status) { + EEPROM.write(EEPROM_CUSTOM_RESET, status); + EEPROM.commit(); +} + +unsigned char customReset() { + static unsigned char status = 255; + if (status == 255) { + status = EEPROM.read(EEPROM_CUSTOM_RESET); + if (status > 0) customReset(0); + if (status > CUSTOM_RESET_MAX) status = 0; + } + return status; +} + void hardwareSetup() { EEPROM.begin(4096); #ifdef DEBUG_PORT @@ -138,7 +153,16 @@ void welcome() { DEBUG_MSG_P(PSTR("%s\n%s\n\n"), (char *) APP_AUTHOR, (char *) APP_WEBSITE); DEBUG_MSG_P(PSTR("ChipID: %06X\n"), ESP.getChipId()); DEBUG_MSG_P(PSTR("CPU frequency: %d MHz\n"), ESP.getCpuFreqMHz()); - DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str()); + + unsigned char custom_reset = customReset(); + if (custom_reset > 0) { + char buffer[32]; + strcpy_P(buffer, custom_reset_string[custom_reset-1]); + DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), buffer); + } else { + DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str()); + } + DEBUG_MSG_P(PSTR("Memory size (SDK): %d bytes\n"), ESP.getFlashChipSize()); DEBUG_MSG_P(PSTR("Memory size (CHIP): %d bytes\n"), ESP.getFlashChipRealSize()); DEBUG_MSG_P(PSTR("Free heap: %d bytes\n"), ESP.getFreeHeap()); diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 48e805ce..67ba5a86 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -166,6 +166,7 @@ void _mqttOnMessage(char* topic, char* payload, unsigned int len) { String t = mqttSubtopic((char *) topic); if (t.equals(MQTT_TOPIC_ACTION)) { if (strcmp(message, MQTT_ACTION_RESET) == 0) { + customReset(CUSTOM_RESET_MQTT); ESP.restart(); } } diff --git a/code/espurna/ota.ino b/code/espurna/ota.ino index 584f2ac0..2967453d 100644 --- a/code/espurna/ota.ino +++ b/code/espurna/ota.ino @@ -28,6 +28,7 @@ void otaSetup() { }); ArduinoOTA.onEnd([]() { + customReset(CUSTOM_RESET_OTA); DEBUG_MSG_P(PSTR("\n[OTA] End\n")); wsSend("{\"action\": \"reload\"}"); delay(100); diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index f58fc0ac..0850a87f 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -96,6 +96,7 @@ void settingsSetup() { Embedis::command( F("RESET"), [](Embedis* e) { e->response(Embedis::OK); + customReset(CUSTOM_RESET_TERMINAL); ESP.restart(); }); diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 27bef38c..c4bed52b 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -86,6 +86,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { DEBUG_MSG_P(PSTR("[WEBSOCKET] Requested action: %s\n"), action.c_str()); if (action.equals("reset")) { + customReset(CUSTOM_RESET_WEB); ESP.restart(); } @@ -779,7 +780,10 @@ void _onRPC(AsyncWebServerRequest *request) { if (action.equals("reset")) { response = 200; - deferred.once_ms(100, []() { ESP.restart(); }); + deferred.once_ms(100, []() { + customReset(CUSTOM_RESET_RPC); + ESP.restart(); + }); } } @@ -863,6 +867,7 @@ void _onUpgrade(AsyncWebServerRequest *request) { response->addHeader("Connection", "close"); if (!Update.hasError()) { deferred.once_ms(100, []() { + customReset(CUSTOM_RESET_UPGRADE); ESP.restart(); }); }