ESP32 Not connecting to WiFi consistently

juliushuck
Posts: 1
Joined: Mon Jul 20, 2020 12:25 pm

ESP32 Not connecting to WiFi consistently

Postby juliushuck » Mon Jul 20, 2020 1:07 pm

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?

Paul-K.
Posts: 5
Joined: Tue Jul 28, 2020 8:31 am

Re: ESP32 Not connecting to WiFi consistently

Postby Paul-K. » Tue Jul 28, 2020 2:53 pm

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--;
  }
 

j4e8a16n
Posts: 9
Joined: Mon Nov 18, 2019 5:55 pm

Re: ESP32 Not connecting to WiFi consistently

Postby j4e8a16n » Wed Sep 16, 2020 1:51 pm

Hi,

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

this refers to what?


Regards


JPD
Arduino 1.8.10; Windows 10; ESP32 VROOM

ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 Not connecting to WiFi consistently

Postby ESP_Sprite » Thu Sep 17, 2020 8:23 am

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.

billthemaker
Posts: 3
Joined: Fri Jan 08, 2021 6:57 am

Re: ESP32 Not connecting to WiFi consistently

Postby billthemaker » Fri Jan 08, 2021 7:15 am

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.

jannemanbrandt
Posts: 1
Joined: Mon Apr 12, 2021 10:59 pm

Re: ESP32 Not connecting to WiFi consistently

Postby jannemanbrandt » Mon Apr 12, 2021 11:04 pm

increase the wait to 5000
delay(5000);

Another option to investigate:

WiFi.mode(WIFI_STA);

uzer123
Posts: 12
Joined: Thu May 07, 2020 4:42 pm

Re: ESP32 Not connecting to WiFi consistently

Postby uzer123 » Tue Apr 27, 2021 4:05 pm

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.

lbernstone
Posts: 831
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 Not connecting to WiFi consistently

Postby lbernstone » Tue Apr 27, 2021 5:56 pm

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

ullixesp
Posts: 83
Joined: Wed Oct 16, 2019 9:34 am
Location: Germany

Re: ESP32 Not connecting to WiFi consistently

Postby ullixesp » Fri Apr 30, 2021 9:10 am

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?

ullixesp
Posts: 83
Joined: Wed Oct 16, 2019 9:34 am
Location: Germany

Re: ESP32 Not connecting to WiFi consistently

Postby ullixesp » Sun May 16, 2021 8:13 am

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

Who is online

Users browsing this forum: Bing [Bot] and 38 guests