Page 1 of 1

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.