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: 3
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: 3914
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.

Who is online

Users browsing this forum: Google [Bot] and 8 guests