Skip to content

Commit f9fcb7d

Browse files
main.cpp: fix wifi crash due to bug in ESP8266 WiFi stack
Begin wifi after disconnecting, to solve a weird bug reported at - esp8266/Arduino#1997 (comment) - http://blog.flynnmetrics.com/uncategorized/esp8266-exception-3/
1 parent 971cbb9 commit f9fcb7d

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/main.cpp

+27-6
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,14 @@ bool beginWiFi()
637637
return false;
638638
}
639639

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

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

817819
// start serial session
818820
Serial.begin(115200);
819-
Serial.println();
821+
Serial.println("[setup] setup workspace");
822+
p_var = new GlobalVar();
823+
auto &logger = p_var->logger;
824+
logger.log("[setup] initialize and close the relays done!");
825+
826+
// if a crash occurs, then do not immediately try to restart,
827+
// otherwise the releys are flickering all the time
828+
rst_info *reset_info = ESP.getResetInfoPtr();
829+
switch (reset_info ? reset_info->reason : rst_reason::REASON_DEFAULT_RST) {
830+
case rst_reason::REASON_WDT_RST:
831+
case rst_reason::REASON_EXCEPTION_RST:
832+
case rst_reason::REASON_SOFT_WDT_RST:
833+
logger.log("[setup] exceptional reset occurred: " + ESP.getResetReason() + ", I'm going to sleep ... ");
834+
while (true) {
835+
delay(1000);
836+
}
837+
break;
838+
default:
839+
break;
840+
}
820841

821842
// update boot count
822843
DynamicJsonBuffer jsonBuffer;

0 commit comments

Comments
 (0)