Skip to content

Commit

Permalink
main.cpp: fix wifi crash due to bug in ESP8266 WiFi stack
Browse files Browse the repository at this point in the history
Begin wifi after disconnecting, to solve a weird bug reported at
- esp8266/Arduino#1997 (comment)
- http://blog.flynnmetrics.com/uncategorized/esp8266-exception-3/
  • Loading branch information
ThomasDevoogdt committed Oct 18, 2020
1 parent 971cbb9 commit f9fcb7d
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,14 @@ bool beginWiFi()
return false;
}

logger.log(F("[WiFi] SSID found, begin connection attempt"), Logger::DEBUG);
WiFi.persistent(false); // 2.2.0 Exception (3): #1997
WiFi.disconnect(true);
// Begin wifi after disconnecting, to solve a weird bug reported at
// https://github.com/esp8266/Arduino/issues/1997#issuecomment-436673828
WiFi.mode(WIFI_STA);
WiFi.begin(ssid.c_str(), pass.c_str());
return true;
WiFi.begin(ssid, pass);
return true;
}

bool reconnectWiFi(bool force)
Expand Down Expand Up @@ -808,15 +813,31 @@ void setup()
// first things first ...
// forces a closed relay,
// this guarantees an equal working when a user turns the light on
p_var = new GlobalVar();
auto &logger = p_var->logger;
logger.log("[setup] initialize and close the relays");
pinMode(pins_arr, OUTPUT);
digitalWrite(pins_arr, HIGH);

// start serial session
Serial.begin(115200);
Serial.println();
Serial.println("[setup] setup workspace");
p_var = new GlobalVar();
auto &logger = p_var->logger;
logger.log("[setup] initialize and close the relays done!");

// if a crash occurs, then do not immediately try to restart,
// otherwise the releys are flickering all the time
rst_info *reset_info = ESP.getResetInfoPtr();
switch (reset_info ? reset_info->reason : rst_reason::REASON_DEFAULT_RST) {
case rst_reason::REASON_WDT_RST:
case rst_reason::REASON_EXCEPTION_RST:
case rst_reason::REASON_SOFT_WDT_RST:
logger.log("[setup] exceptional reset occurred: " + ESP.getResetReason() + ", I'm going to sleep ... ");
while (true) {
delay(1000);
}
break;
default:
break;
}

// update boot count
DynamicJsonBuffer jsonBuffer;
Expand Down

0 comments on commit f9fcb7d

Please sign in to comment.