• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

WiFi Connection Drops / Auto Reconnect

Q&A Forum › Category: ESP32 › WiFi Connection Drops / Auto Reconnect
0 Vote Up Vote Down
David Fenner asked 6 years ago

[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…]

——————————————————-

Hi all,

I have being having a problem with a number of esp wroom 32 in that intermittently they will drop the WiFi connection.

I understand via internet groups this is happening to a number of people, the date stamp on the post suggest that problems have been experienced for a number of year.

So my first question is; has there been a firmware upgrade or a code routine that will automatically reconnect or even an update to the wifi.h /wifi.cpp files to correct the issue?

And my 2nd question is; I know that the code that is designed to reconnect has problems and the use of  WiFi. disconnect(true) without the use of  WiFi.persistent(false) could present an issue due to impact on the NV FLASH.

So in the context  of the overall problem of dropping wifi connections and then auto reconnecting, what is the correct way to use them with for example the following code snippet that eventually would be used in my loop routine  :

while (WiFi.status() != WL_CONNECTED)
    {
        WiFi.mode(WIFI_STA);
    WiFi.disconnect();
 
    delay(100);
       WiFi.begin(ssid, pass);
       Serial.print("Attempting to connect to  network, SSID: ");
        delay(5000);
                Serial.println(ssid);
 
       Serial.println("IP address: ");
       Serial.println(WiFi.localIP());
 
        wifiStatus = WiFi.status() == WL_CONNECTED;
 
    }
Question Tags: WiFi Connection
9 Answers
1 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

[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…]

——————————————————-

Hi David.

Yes, it seems that some people have problems with the ESP32 – it disconnects randomly for some reason.

Some people with those problems suggest creating a function that reconnects to wi-fi. Then, in the loop, check if it is still connected. If it isn’t ,call the reconnect function.

In the following link there’s a simple example of implementation:
github.com/espressif/arduino-esp32/issues/653#issuecomment-425645575

There’s also a similar implementation in the following link using timers:
reddit.com/r/esp32/comments/7trl0f/reconnect_to_wifi/

These implementations are very similar to what you’ve posted. I’m not familiar with a different way to reconnect. Everything that I’ve read so far uses a similar approach.

I hope this helps.

Regards,
Sara

0 Vote Up Vote Down
David Fenner answered 6 years ago

Thank you Sara for your feedback.
The first link you identified I have been studying and this thread was where I learned of the NV FLASH issue see mickeypop discussion on 9 Feb 2018.
I presume that the point of using WiFi.persistent(false) is to prevent writing to the NV FLASH when there is no need to hence maximising its life. So if anyone can help with the placement of this command e.g. is it before or after WiFi. disconnect(true) that would be appreciated.
My understanding of the ESP32 is that there are two manufactures of the ESP32 device one being wroom and presumably the device has the ability to restart Wi-Fi in the event of a failure, so my approach to start with is to look at any firmware updates. However I am hindered as I do not know how to contact wroom and how to interrogate the chip for its current firmware version ID again any help would be appreciated.

0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Hi David.

After reading some discussions, it seems that WiFi.persistent(false) was not working properly and nobody gave a solution or a fix for that. I don’t know if the problem persists or not: https://github.com/espressif/arduino-esp32/issues/1393

The firmware depends on what you flash the ESP32 with. To have the latest firmware you need to have your ESP32 boards add-on updated in your Arduino IDE. If you’re using Arduino IDE, you need to go to the Boards Manager, search for ESP32 and update to the latest version.

If you’re using PlatformIO, you can follow this suggestion: https://github.com/espressif/arduino-esp32/issues/653#issuecomment-475758778
(I don’t use platformIO, so I’m not familiar on how to do that).

I hope this helps.
Regards,
Sara

0 Vote Up Vote Down
David Fenner answered 6 years ago

Sara, that you  for the help regarding using Boards Manager to update firmware 

0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

You’re welcome.
Then, let me know if that solved your problem.

0 Vote Up Vote Down
David Fenner answered 6 years ago

Hi Sara,
No, as the boards manager confirmed I am using the newest version ie   esp32 by Espressif Systems 1.0.2. 
Could you help with the following:- .
Now  I will have to use code to correct my problem for the Setup routine I have been investigating the use of  ESP.restart() however it  works but I have found that it keep repeating the command. What I need to achieve is for this command to achieve a hard reset via software, like pressing the  EN  on the ESP32 board button once.
Best regards 
 

0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Hi.
To reset the ESP32, you use ESP.restart(), however you’re probably using it in the wrong way.
Can you share your setup() so that I can take a look?
Regards
Sara

0 Vote Up Vote Down
David Fenner answered 6 years ago

Thank you Sara

#include <WiFi.h>
const char* ssid = "My SSID";
const char* password = "My PW";
void setup() {
int NAcounts=0;
Serial.begin(115200);

WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED && NAcounts < 7 )
{
delay(500);
Serial.print(".");
NAcounts ++;
}
if (WiFi.status() != WL_CONNECTED){
Serial.println("Resetting");
ESP.restart();
}
else
{
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
}

The video demonstrates how it just keep repeating  were it should be just once and then execute the code in your sketch just as if you pressed the EN key.

https://www.youtube.com/watch?v=VmKtrx3ocVc

0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Hi David.

I tested your code and it works just fine for me. When there is no internet, the ESP restarts after 7 attempts.

In the video, it keeps resetting because he is using just a sample code to exemplify the process. It keeps resetting because it doesn’t have any condition. So, every time it resets, it runs the setup() that tells it to reset again.

Your code works fine. But if you want your ESP to reconnect automatically, when it disconnects to Wi-Fi, you should verify that condition in your loop. For example, every X number of seconds, verify that it is connected, and if it isn’t reset the board.

I hope my explanation is clear. Let me know if you need further help.

Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.