[SOLVED] Reconnect ESP32 to Wi-Fi Network After Lost Connection
This quick guide shows how you can reconnect your ESP32 to a Wi-Fi network after losing the connection. This can be useful in the following scenarios: the ESP32 temporarily loses Wi-Fi signal; the ESP32 is temporarily out of the router’s Wi-Fi range; the router restarts; the router loses internet connection or other situations [Continue reading…]
——————————————————
Running code: https://gist.github.com/RuiSantosdotme/d5a9d2536c1470008a850b5e01261fb5
Arduino IDE ver 1.8.12
Board Mannager esp32 ver 1.0.4
Use Chrome web from windows 10, and Android phone Chrome to change the two outputs on the web page.
Works fine for 2 hours then neither web browser will connect. Ran test where contact was made every 10 minutes until the 2 hour mark then both browsers failed to connect. Second test where contact was tested at 1 hour, 1 hour + 50 minutes then every minute. Worked for 1 hour 59 minutes then both failed to make connection at the 2 hour mark.
Pressing EN button regains function for another 2 hours then it fails again.
Note added a push button input to cause a Serial.print to verify the processor is functioning and only the connection is failing.
Steve
Hi.
That’s weird.
Do you have another ESP32 to experiment with? Can you try changing the GPIOs you want to control to GPIO 32 and GPIO 33? Just to check that it has nothing to do with the ADC2 pins?
Regards,
Sara
Downloaded a fresh copy of program from https://gist.github.com/RuiSantosdotme/d5a9d2536c1470008a850b5e01261fb5
and only changed the SSID and PASSWORD lines and after downloading ran unit from a separate 2.5Amp supply from my Raspberry Pi.
I used a different ESP32-WROOM-32 DOIT DEVKIT V1 which was just purchased. Original unit with problem was bought about 14 months ago, so not the same batch. (Fail at the 2 hour point)
Changed the GPIOs to 32 and 33 (also Fail at the 2 hour point)
I looked in my file>preference Additional Board Manager URL’s :
https://dl.espressif.com/dl/package_esp32_index.json,
http://arduino.esp8266.com/stable/package_esp8266com_index.json
which was setup when I first did Module 1 Unit 3 of Learn ESP32 with Arduino IDE
I did note that the GitHub page lists the board manager URL as:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
Which is correct? Expect could be a problem in a library update.
Tnx, Steve
Hi Steve.
I’ve tried the web server myself. It’s been more than three hours and the web server is still working properly.
Maybe you can try checking if the wi-fi connection is still available, and if it isn’t, try to reconnect again.
Something like it’s done in this discussion: https://www.reddit.com/r/esp32/comments/7trl0f/reconnect_to_wifi/
The URL we use is https://dl.espressif.com/dl/package_esp32_index.json
However, the other URL you mentioned contains the exact same JSON object, so you can use either one.
Regards,
Sara
Sara, while not finding why my WiFi is shutting down momentarily every 2 hours, your suggested reconnect works well. Also thanks goes to rnt for all the tech publications you put out.
The code I now include was modified since 30 seconds was too long between reconnect attempts (has never taken more than 1 second to reconnect). Also added a reset every 46+ days to avoid the rollover of the millis() function.
// if wifi is down, try reconnecting every 2 second if ((WiFi.status() != WL_CONNECTED) && (millis() > check_wifi)) { // added print for trouble shooting Serial.print(millis()); Serial.println(" ... Reconnecting to WiFi..."); WiFi.disconnect(); WiFi.begin(ssid, password); check_wifi = millis() + 2000; }
//Reset after 46+ days to keep from overflow of unsigned longs used in calcs with millis()
if(millis()>= 4000000000 ){esp_restart();}
Thank you, Steve
Great!
I’m glad it is working now.
Thank you for sharing that snippet of code. It can be useful for our readers that have the same issue.
I’ll mark this issue as resolved, if you need further help, you just need to open a new question in our forum.
Thanks for following our work.
Regards,
Sara
Sara,
This solution works except that there are hidden non-printing characters in the “Reconnecting” println statement. I suggest editing Steve’s response so that other people who copy/paste the text will not have this problem:
Serial.println(” … Reconnecting to WiFi…”);
^
WiFi32-2020-013:1423:3: error: stray ‘\200’ in program
WiFi32-2020-013:1423:3: error: stray ‘\235’ in program
WiFi32-2020-013:1423:3: error: stray ‘\342’ in program
WiFi32-2020-013:1423:3: error: stray ‘\200’ in program
WiFi32-2020-013:1423:3: error: stray ‘\246’ in program
WiFi32-2020-013:1423:3: error: stray ‘\342’ in program
WiFi32-2020-013:1423:3: error: stray ‘\200’ in program
WiFi32-2020-013:1423:3: error: stray ‘\246’ in program
WiFi32-2020-013:1423:3: error: stray ‘\342’ in program
WiFi32-2020-013:1423:3: error: stray ‘\200’ in program
WiFi32-2020-013:1423:3: error: stray ‘\235’ in program