After a month of trying various alternatives, I finally solved my ESP-32 intermittent WiFi connection problem. I have an application that uses a BME280 to send weather data to a webserver every 5 minutes, and then sleeps until the next period. About 10% of the time it would not connect to my WiFi despite having a strong signal. I replaced the typical connection code from numerous examples with the code below and it now connects every time. Sometimes I get the “WiFi failed” message once but then it connects immediately after. I hope others may find this helpful.
Serial.println(“Connecting”);
while(1) {
delay(2000);
WiFi.begin(ssid, password);
if(WiFi.waitForConnectResult() == WL_CONNECTED) break;
else Serial.println(“WiFi Failed”);
}
Serial.println(“”);
Serial.print(“Connected to WiFi network with IP Address: “);
Serial.println(WiFi.localIP());
long rssi = WiFi.RSSI();
Serial.print(“RSSI:”);
Serial.println(rssi);
Hi.
Thank you so much for sharing that code snipet.
I will recommend it to anyone having connection problems similar to yours.
Thank you.
Regards,
Sara
My pleasure! Here is the website I was working on – https://jobohoa.com/weather. It is a set of weather gauges for my homeowners association in Costa Rica. It borrows heavily from the code examples and teachings in your tutorials and uses mySQL, PHP, Javascript, HTML, CSS, and ESP32 C++ running on an Unexpected Makers Feather S3 and a LightSail server instance on AWS. Your tutorials and books are great!
Your website looks great!
I’m glad our tutorials were useful.
Thank you so much for your support.
Regards,
Sara
Thanks for sharing these bytes!
I have a similar issue with a free WiFi Hotspot and will also try your solution.
It looks like the current code just worries once about getting connected, but in case of failure no reconnect takes place (my assumption).
Here I need a solution with more endurance and hopefully your approach makes the difference,
Thanks again and happy coding,
Loetluemmel!
Hi.
You can also try taking a look at Wi-Fi events.
You can create an event that listens for a disconnection and tries to reconnect.
Here’s a link to the tutorials:
- https://randomnerdtutorials.com/esp32-useful-wi-fi-functions-arduino/
- https://randomnerdtutorials.com/solved-reconnect-esp32-to-wifi/
I hope this also helps.
Regards,
Sara