ESP WiFi hung solution
-
This is how I solved the issue of ESP32 GW hanging when WiFi connection is lost or not established. It is not 100% my original work, it has been put together using bits and pieces, but it works.
I simply use a watchdog timer:
#include "esp_system.h" // added for WDT hw_timer_t* timer = NULL; // WDT related void IRAM_ATTR resetModule() { Serial.println("reboot\n"); ESP.restart(); } void WdtReset() { timerWrite(timer, 0); //reset timer (feed watchdog) } void StartWatchDog() { timer = timerBegin(0, 240, true); //timer 0, div 80 timerAttachInterrupt(timer, &resetModule, true); timerAlarmWrite(timer, 5000000, false); //set time in us timerAlarmEnable(timer); //enable interrupt } IPAddress ConnectWiFi() { Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(MY_WIFI_SSID); /* Explicitly set the ESP to be a WiFi-client, otherwise, it by default, would try to act as both a client and an access-point and could cause network-issues with your other WiFi-devices on your WiFi-network. */ #ifdef ESP32 WiFi.persistent(false); WiFi.mode(WIFI_STA); #endif // ESP32 WiFi.begin(MY_WIFI_SSID, MY_WIFI_PASSWORD); while (WiFi.status() != WL_CONNECTED) { digitalWrite(LED_BUILTIN, LED_ON); wait(500); Serial.print("."); digitalWrite(LED_BUILTIN, LED_OFF); wait(500); } #ifdef ESP32 WiFi.setAutoReconnect(true); #endif // ESP32 return WiFi.localIP(); } void before() { StartWatchDog(); // We start by connecting to a WiFi network localIP = ConnectWiFi(); myHostname = WiFi.getHostname(); WdtReset(); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(localIP); Serial.println(myHostname); // do other stuff ... } void loop() { // check WiFi connection if (WiFi.status() != WL_CONNECTED) return; // the WDT is not reset WdtReset(); // do other stuff ... }Hope it helps
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login