ESP32 Not connecting to WiFi consistently

Marcus90
Posts: 2
Joined: Fri May 21, 2021 6:01 pm

Re: ESP32 Not connecting to WiFi consistently

Postby Marcus90 » Fri May 21, 2021 6:18 pm

Hello, when trying to connect to the WiFi I always get error messages.
For the connection I used the following code in the Ardunio IDE:

Code: Select all

WiFi.mode(WIFI_STA);
WiFi.setHostname("esp32");
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
}
Serial.println(WiFi.localIP());
}
The serial monitor is flooded with the following messages:

Code: Select all

19:48:30.481 -> E (64328) wifi:Set status to INIT
19:48:30.529 -> E (64348) wifi:Set status to INIT
19:48:30.529 -> E (64367) wifi:Set status to INIT
19:48:30.577 -> E (64385) wifi:Set status to INIT
19:48:30.577 -> E (64403) wifi:Set status to INIT
19:48:30.577 -> E (64418) wifi:Set status to INIT
19:48:30.625 -> E (64433) wifi:Set status to INIT
19:48:30.625 -> E (64450) wifi:Set status to INIT
19:48:30.625 -> E (64466) wifi:Set status to INIT
19:48:30.672 -> E (64480) wifi:Set status to INIT
19:48:30.672 -> E (64499) wifi:Set status to INIT
19:48:30.672 -> E (64517) wifi:Set status to INIT
19:48:30.719 -> E (64542) wifi:Set status to INIT
19:48:31.373 -> ...........
However, I have successfully used this code on another WiFi network, so I think the problems are not related to the esp or the code.
Any tips on how to check the settings on my network?
I never had connection problems with other devices (laptop, iPhone/iPad, Raspberry).

Thanks in advance for your help.

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

Re: ESP32 Not connecting to WiFi consistently

Postby billthemaker » Fri May 21, 2021 9:35 pm

It's printing a '.' every loop it's not connected in your while loop, but there's nothing in the loop that says to try to connect again. Copy and paste the wifi.begin function to happen in your while not connected loop. Then it will try to connect to the internet repeatedly until successful or until you turn it off.

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

Re: ESP32 Not connecting to WiFi consistently

Postby ullixesp » Sat May 22, 2021 8:32 am

@Marcus90 Your error messages are strange, I have never seen them. Furthermore, I just scanned my WiFi lib and such a message does not exist. My lib's error messages look like this:

Code: Select all

log_e("[WIFI] no networks found");
All "wifi" is capitalized and in square brackets. My lib is:
name=WiFi
version=1.0
author=Hristo Gochkov
maintainer=Hristo Gochkov <hristo@espressif.com>
sentence=Enables network connection (local and Internet) using the ESP32 built-in WiFi.
paragraph=...
category=Communication
url=
architectures=esp32
which I believe is as default as it can be. Which lib are you using and which device exactly?

Your code should work as is, UNLESS, however, you have one of those systems, which needs "double-hitting", i.e. issuing the wifi.begin() command twice, see here: viewtopic.php?f=19&t=16622#p75761

This has been solved - at least in my case - by upgrading the ESP core from 1.0.4 to 1.0.6, see viewtopic.php?f=19&t=16622#p76600

As obviously something impacting Wifi has been changed in 1.0.6, I do strongly recommend upgrading to this release!

Marcus90
Posts: 2
Joined: Fri May 21, 2021 6:01 pm

Re: ESP32 Not connecting to WiFi consistently

Postby Marcus90 » Sat May 22, 2021 8:36 am

Thank you very much for your comment!
However, the problem was definitely related to the router.
It seems that the esp32 cannot connect to 5GHz WiFi networks. My router has both networks (2.4GHz and 5GHz) on the same network name.
The solution was to separate the two networks, e.g. 2.4GHz ssid: network1 / 5GHz ssid: network2.

As it turned out, all other devices continue to run on the 5GHz network and the esp32 connection to the 2.4GHz network is stable.
Interesting side effect:
If you use the following code to search for networks nearby, the esp32 can only find the 2.4GHz network.

Code: Select all

 // WiFi.scanNetworks will return the number of networks found
  int n = WiFi.scanNetworks();
  Serial.println("scan done");
  if (n == 0) {
      Serial.println("no networks found");
  } else {
    Serial.print(n);
    Serial.println(" networks found");
    for (int i = 0; i < n; ++i) {
      // Print SSID and RSSI for each network found
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.print(WiFi.SSID(i));
      Serial.print(" (");
      Serial.print(WiFi.RSSI(i));
      Serial.print(")");
      Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
      delay(10);
    }
  }
  Serial.println("");

  // Wait a bit before scanning again
  delay(5000);

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

Re: ESP32 Not connecting to WiFi consistently

Postby ullixesp » Sat May 22, 2021 9:39 am

The very first sentence in the ESP32 data sheet is:
ESP32 is a single 2.4 GHz Wi-Fi-and-Bluetooth combo chip...

So, it is not that much of a surprise that the ESP finds only 2.4GHz networks?

I am still wondering why your Wifi lib gives log messages which don't exist in the lib I know?

Who is online

Users browsing this forum: No registered users and 31 guests