Very slow to connect to WiFi with ESP32

Hi all, I am having some issues with connecting to WiFi with an ESP32 board. It is very sloooooooooow. The first few times I tried, I left it alone for 20 minutes and it still wasn't able to connect. So I thought it didn't work. So I tried to use MicroPython and ESP-IDF and they both worked, although MicroPython was somewhat slow (~10-20 seconds), but ESP-IDF was instantaneous. Then I went back to Arduino and this time it eventually connected after 260 seconds (> 4 minutes). Another try took 420 seconds (7 minutes). Has anyone else seen similar issues?

What I have tried:

  • Tried another board of the same model to ensure it's not defective.
  • Tried a different WiFi AP with the same results.

Although MicroPython and ESP-IDF framework work fine, I am still hoping to stick with Arduino for its simplicity and my hatred for Python, but don't hold it against me if you're fond of it :-).

My board:
ESP-WROOM-32(https://www.amazon.com/gp/product/B0B764963C). In Arduino IDE, I'm choosing "ESP32-WROOM-DA Module". I have also tried "uPesy ESP32 Wroom DevKit" with the same results.

My network setup:
TP-Link EAP225, running as a dedicated AP, not a router. My router is Linksys WRT1900ACS running OpenWrt 19.07.2 r10947-65030d81f3.

The other setup I tried is a D-Link DIR-842 running as both an AP and a router, although I didn't connect the upstream port.

My code:

#include <WiFi.h>

const char* ssid = "delingtest";
const char* password = "XxxxxYyyy00";

void setup() {
  Serial.begin(115200);
  Serial.println(millis()); // To time the connection
  Serial.print("Connecting to WiFi network ");
  Serial.print(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin();

  do {
    Serial.print(".");
    delay(5000);
  } while (!WiFi.isConnected());

  Serial.println("done.");
  Serial.println(millis()); // To time the connection
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
}

Additional info:

Wed Mar 13 16:57:55 2024 daemon.info dnsmasq-dhcp[13394]: DHCPDISCOVER(br-lan) 40:22:d8:78:7a:38
Wed Mar 13 16:57:55 2024 daemon.info dnsmasq-dhcp[13394]: DHCPOFFER(br-lan) 192.168.1.145 40:22:d8:78:7a:38
Wed Mar 13 16:57:55 2024 daemon.info dnsmasq-dhcp[13394]: DHCPREQUEST(br-lan) 192.168.1.145 40:22:d8:78:7a:38
Wed Mar 13 16:57:55 2024 daemon.info dnsmasq-dhcp[13394]: DHCPACK(br-lan) 192.168.1.145 40:22:d8:78:7a:38 esp32-787A38

P.S. after looking into the source code of the ESP WiFi library (arduino-esp32/libraries/WiFi/src/WiFiSTA.cpp at 2479efb10d2709246a41562b8a626c7ed14ee164 · espressif/arduino-esp32 · GitHub) I found out that the minimum security mode defaults to WPA2_PSK and my AP is WPS_PSK (which shouldn't work at all, right?!). So I added a WiFi.setMinSecurity(WIFI_AUTH_WPA_PSK); call before connecting. But the result is still the same. :frowning:

WiFi.begin() with no parameters is this overload

/**
 * Use to connect to SDK config.
 * @return wl_status_t
 */
wl_status_t WiFiSTAClass::begin()

Did you actually set the config with esp_wifi_set_config? You might get better results passing the SSID and password directly.

In any case, check the wl_status_t return value. It might be WL_CONNECT_FAILED, which should not progress any further. While it's trying, WiFi.status() should return WL_DISCONNECTED. Check that instead of relying on the boolean isConnected(), which expects WL_CONNECTED.

Compile in more logging: under the Tools menu, set the Core Debug Level to something other than "None", like "Debug".

Thanks for the second pair of eyes. Oops, that was a blunder. I actually forgot to pass the ssid and password! And, yet, it connected after a few minutes?! How? Why?

And I thought that was the whole problem. Well, no so quick! Now, after correcting that mistake, it's still taking 20 seconds to a few minutes to connect.

Thanks for the tip on turning on verbose logging. According to the log, the connection was established after only 1.5 seconds. But it was waiting for an IP address for minutes. But according to my DHCP sever, the DHCP request was immediately granted.

The verbose log also revealed why ping() was taking that long. It was actually doing a ping -c 5, sending 5 packets, with 1 second between packets. Originally I thought it was just sending one packet.

Then I moved close to the AP (from ~8 meters to ~2 meters), and it significantly sped up to about 10 seconds (still slow, but). So, I suppose the signal strength is really the issue here? It still doesn't make sense. Even when I was away from the AP, according to the log, the pings were completed in ~10 milliseconds, which isn't too bad for WiFi.

Huh, I just tried a 3rd board and I think I have found a pattern. If the board is inserted in a breadboard. It's slooooooow, or even impossible to connect. But if I pull it out, it seems to connect in a few seconds. I know there's metal in the breadboard. But this is wild! The antenna on this thing is pretty much non-existent. I guess I'll need to find a board that can connect to an external antenna.

Thanks for the heads-up. I realized I am experiencing the same issue. The breadboard badly affects the connection to WIFI.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.