Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ONB] Fix failsafe mode #2080

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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