Page 1 of 2

ESP32 Not connecting to WiFi consistently

Posted: Mon Jul 20, 2020 1:07 pm
by juliushuck
Hello,
my ESP32 (I use a development board: Says ESP-WROOM 32 on the actual chip) is not connecting to my WiFi consistently.

When using Arduino Ide:
Connects 50% of the time. Often connects first try but then on a reset it does not connect. Then on the third try it connects again and so on. I have not tested if this is always like this. But often it is like this.

With Platform IO in VS Code:
Did work once for me. But since then (3h of trying) it did not work again.

In both IDEs I use this example sketch with an HTTPs server:

Code: Select all

#include <WiFi.h>
#include <HTTPSServer.hpp>
#include <SSLCert.hpp>
#include <HTTPRequest.hpp>
#include <HTTPResponse.hpp>
 
using namespace httpsserver;
 
const char* ssid = "****";
const char* password =  "****";
 
SSLCert * cert;
HTTPSServer * secureServer;
 
void setup() {
 
  Serial.begin(115200);
 
  Serial.println("Creating certificate...");
   
  cert = new SSLCert();
 
  int createCertResult = createSelfSignedCert(
    *cert,
    KEYSIZE_2048,
    "CN=myesp.local,O=acme,C=US");
   
  if (createCertResult != 0) {
    Serial.printf("Error generating certificate");
    return; 
  }
 
  Serial.println("Certificate created with success");
   
  secureServer = new HTTPSServer(cert);
 
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
  
  Serial.println(WiFi.localIP());
 
 
  ResourceNode * nodeRoot = new ResourceNode("/", "GET", [](HTTPRequest * req, HTTPResponse * res){
    res->println("Secure Hello World!!!");
  });
 
  secureServer->registerNode(nodeRoot);
 
  secureServer->start();
   
  if (secureServer->isRunning()) {
    Serial.println("Server ready.");
  }
}
 
void loop() {
  
  secureServer->loop();
  
  delay(10);
}
For Platform IO I also use this as configuration in platform.ini:

Code: Select all

[env:esp32dev]
board = esp32dev
framework = arduino
lib_deps = esp32_https_server
monitor_speed = 115200
platform = espressif32
upload_port = COM7
I also tried this example sketch:

Code: Select all

#include <WiFi.h>
#include <ESPmDNS.h>
#include <ArduinoOTA.h>
#include <WebServer.h>

const char* ssid = "Huck";
const char* password = "NutzedenTag!";

WebServer server(80);

const char* www_username = "admin";
const char* www_password = "esp32";

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("WiFi Connect Failed! Rebooting...");
    delay(1000);
    ESP.restart();
  }
  ArduinoOTA.begin();

  server.on("/", []() {
    if (!server.authenticate(www_username, www_password)) {
      return server.requestAuthentication();
    }
    server.send(200, "text/plain", "Login OK");
  });
  server.begin();

  Serial.print("Open http://");
  Serial.print(WiFi.localIP());
  Serial.println("/ in your browser to see it working");
}

void loop() {
  ArduinoOTA.handle();
  server.handleClient();
}
It works every second time I press reset (Actually it always reboots automatically after the first fail itself). Maybe there is not enough time between the disconnect and the connect when pressing the reset button on the development board.

I know that a lot of people have experienced similar problems but I could not find a solution. Does somebody know why it is not working/why it is that inconsistent?

Re: ESP32 Not connecting to WiFi consistently

Posted: Tue Jul 28, 2020 2:53 pm
by Paul-K.
I experience the same issue and simply implemented a timeout in the connection routine, to call the reconnect function after some attempts.
This way I always get a connection to the wifi in a matter of seconds and without the need of a reset.

here is an example snippet:

Code: Select all

WiFi.begin(mySSID, myWifiPwd);
  int retryIntervalMs = 500;
  int timeoutCounter = this->wifiClientConnectionTimeoutSeconds * (1000 / retryIntervalMs);
  while (WiFi.status() != WL_CONNECTED && timeoutCounter > 0)
  {
    delay(retryIntervalMs);
    if (timeoutCounter == (this->wifiClientConnectionTimeoutSeconds * 2 - 3))
    {
      WiFi.reconnect();
    }
    timeoutCounter--;
  }
 

Re: ESP32 Not connecting to WiFi consistently

Posted: Wed Sep 16, 2020 1:51 pm
by j4e8a16n
Hi,

error: invalid use of 'this' in non-member function

this refers to what?


Regards


JPD

Re: ESP32 Not connecting to WiFi consistently

Posted: Thu Sep 17, 2020 8:23 am
by ESP_Sprite
My guess is that that is just a snippet of code to illustrate how he solves the issue, not something you can/should copy/paste in your own program.

Re: ESP32 Not connecting to WiFi consistently

Posted: Fri Jan 08, 2021 7:15 am
by billthemaker
I just fought an internet connection battle on my first esp32 with a similar issue and just added another Wifi.begin function inside of the while loop:
while (WiFi.status() != WL_CONNECTED) {
Serial.print("WL not connected, trying again...");
WiFi.begin(ssid, password);
delay(1000);

Cut and paste this and it will keep trying until it connects.
extra explanation:
the while loop means it keeps doing this until the condition changes, the != means not-equal-to
in plain english it would read:
while the wifi status is not equal to WL connected, keep doing the next things
print "*****"
try to connect to the internet again using the username "ssid" and password "password"
then wait 1000 units of time

Extra credit: look up how to create a loop counter and after a certain number of tries have it type an error message and go through a reset, not especially hard code to look up and would teach you some very helpful programming skills.

Re: ESP32 Not connecting to WiFi consistently

Posted: Mon Apr 12, 2021 11:04 pm
by jannemanbrandt
increase the wait to 5000
delay(5000);

Another option to investigate:

WiFi.mode(WIFI_STA);

Re: ESP32 Not connecting to WiFi consistently

Posted: Tue Apr 27, 2021 4:05 pm
by uzer123
increase the wait to 5000
delay(5000);
i didnt double tested, but the change from 1s to 5s shows to improve the fails..

Actually, i try 4-5 times with 1s delay, then i try another 5 times, with 5s. If all these attempts fail, i continue.

Re: ESP32 Not connecting to WiFi consistently

Posted: Tue Apr 27, 2021 5:56 pm
by lbernstone
billthemaker wrote:
Fri Jan 08, 2021 7:15 am
Extra credit: look up how to create a loop counter and after a certain number of tries have it type an error message and go through a reset, not especially hard code to look up and would teach you some very helpful programming skills.
The WiFi library already includes a function to loop for 10 seconds waiting for a connection- waitForConnectResult(). https://github.com/espressif/arduino-es ... A.cpp#L373

Re: ESP32 Not connecting to WiFi consistently

Posted: Fri Apr 30, 2021 9:10 am
by ullixesp
This discussion strongly reminds me of the lengthy github topic ESP32 WiFi.begin works only every second time. There the essence is that mostly modern Fritz!Boxes - though not exclusively - require a double-hitter approach to establish a WiFi connection. That is, you do a first try, which almost always fails, then you retry, which almost always succeeds. My Fritz!Box is a 7490, and it behaves this way in >95% of all cases.

Other routers of mine, including older FritzBoxes, almost always connect on the first try with the same set of ESP32 hardware and code.

In this topic here there is no mention of the router being used. I wonder if these are also FritzBoxes?

However, it may not be a FB issue, at least not only. I once tested the ESP_IDF directly with 4 different ESP32s (WROOM, WROVER, PICO), and there the connection to my "bad" FB always (!) succeeded on the first try: https://github.com/espressif/arduino-es ... -649422935. This was confirmed by others: https://github.com/espressif/arduino-es ... -708704583

It was later pointed out that the current ESP_IDF is version 4.x, while Arduino still uses 3.x.

So, which IDF version are you using, directly or indirectly?

Re: ESP32 Not connecting to WiFi consistently

Posted: Sun May 16, 2021 8:13 am
by ullixesp
If you are still suffering from Wifi connection issues, consider upgrading to ESP core 1.0.6. The need for double hitting is gone. Details here:

https://github.com/espressif/arduino-es ... -841649304