Skip to content

Commit

Permalink
[ONB] Fix failsafe mode (1technophile#2080)
Browse files Browse the repository at this point in the history
Erasing the nvs before the start of WM prevent WM from starting, revert a part of 1technophile#2075
Also remove the erase from setupWiFiManager method

Co-authored-by: Florian <[email protected]>
  • Loading branch information
2 people authored and odoral committed Oct 18, 2024
1 parent 1b16a9a commit 3a2f8d6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
3 changes: 2 additions & 1 deletion main/ZsensorGPIOInput.ino
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void MeasureGPIOInput() {
resetTime = millis();
} else if ((millis() - resetTime) > 3000) {
Log.trace(F("Button Held" CR));
gatewayState = GatewayState::WAITING_ONBOARDING;
// Switching off the relay during reset or failsafe operations
# ifdef ZactuatorONOFF
uint8_t level = digitalRead(ACTUATOR_ONOFF_GPIO);
Expand All @@ -72,7 +73,7 @@ void MeasureGPIOInput() {
}
# endif
Log.notice(F("Erasing ESP Config, restarting" CR));
setupWiFiManager(true);
erase(true);
}
} else {
resetTime = 0;
Expand Down
28 changes: 10 additions & 18 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1393,13 +1393,14 @@ void setup() {
#if defined(ESPWifiManualSetup)
setupWiFiFromBuild();
#else
if (loadConfigFromFlash()) {
if (loadConfigFromFlash()) { // Config present
Log.notice(F("Config loaded from flash" CR));
# ifdef ESP32_ETHERNET
setup_ethernet_esp32();
# endif
if (!failSafeMode && !ethConnected) setupWiFiManager(false);
} else {
// If not in failSafeMode and no connection to the network with Ethernet, launch the wifi manager
if (!failSafeMode && !ethConnected) setupWiFiManager();
} else { // No config in flash
# ifdef ESP32_ETHERNET
setup_ethernet_esp32();
# endif
Expand All @@ -1411,7 +1412,7 @@ void setup() {

Log.notice(F("No config in flash, launching wifi manager" CR));
// In failSafeMode we don't want to setup wifi manager as it has already been done before
if (!failSafeMode) setupWiFiManager(false);
if (!failSafeMode) setupWiFiManager();
}

#endif
Expand Down Expand Up @@ -1879,27 +1880,23 @@ void blockingWaitForReset() {
ActuatorTrigger();
}
# endif
ledManager.setMode(-1, -1, LEDManager::STATIC, LED_WAITING_ONBOARD_COLOR, -1);
gatewayState = GatewayState::WAITING_ONBOARDING;
// Checking if the flash has already been erased to identify if we erase it or go into failsafe mode
// going to failsafe mode is done by doing a long button press from a state where the flash has already been erased
if (SPIFFS.begin()) {
Log.trace(F("mounted file system" CR));
if (SPIFFS.exists("/config.json")) {
Log.notice(F("Erasing ESP Config, restarting" CR));
erase(true);
} else {
Log.notice(F("Erasing ESP Config, without restart" CR));
erase(false);
}
}
delay(30000);
if (digitalRead(TRIGGER_GPIO) == LOW) {
Log.notice(F("Going into failsafe mode without peripherals" CR));
// Failsafe mode enable to connect to Wifi or change the firmware without the peripherals setup
failSafeMode = true;
setupWiFiManager(false);
setupWiFiManager();
}
ESPRestart(5);
}
}
}
Expand Down Expand Up @@ -2179,13 +2176,10 @@ bool loadConfigFromFlash() {
return result;
}

void setupWiFiManager(bool reset_settings) {
void setupWiFiManager() {
delay(10);
WiFi.mode(WIFI_STA);

if (reset_settings)
erase(true);

# ifdef USE_MAC_AS_GATEWAY_NAME
String s = WiFi.macAddress();
snprintf(WifiManager_ssid, MAC_NAME_MAX_LEN, "%s_%.2s%.2s", Gateway_Short_Name, s.c_str(), s.c_str() + 3);
Expand Down Expand Up @@ -3346,10 +3340,8 @@ void XtoSYS(const char* topicOri, JsonObject& SYSdata) { // json object decoding
if (strstr(cmd, restartCmd) != NULL) { //restart
ESPRestart(5);
} else if (strstr(cmd, eraseCmd) != NULL) { //erase and restart
#ifndef ESPWifiManualSetup
setupWiFiManager(true);
#endif
} else if (strstr(cmd, statusCmd) != NULL) { //erase and restart
erase(true);
} else if (strstr(cmd, statusCmd) != NULL) {
publishState = true;
}
}
Expand Down

0 comments on commit 3a2f8d6

Please sign in to comment.