ESP32 dev board will not connect to wifi

Hi all,

I'm new to programming on ESP32 and networking so please forgive the simple mistakes. I am trying to connect my board to my home wifi without success. I searched a number of other posts but can't seem to figure this one out.

I am able to get it to connect to my iphone hotspot and get an IP address, so that's a plus.

I can see my home network when i do a network scan.

When i attempt to connect, the return code is 6 for the first attempt or 2.. then after that it's 4.

The only thing i can think of is it doesn't like my wifi name.

Thanks in advance for any ideas or help.

The debug messages I get out are here:
13:07:24.638 -> 13 networks found
13:07:24.638 -> Nr | SSID | RSSI | CH | Encryption
13:07:24.678 -> 1 | DIRECT-3I-EPSON-ET-2850 Series | -65 | 10 | WPA2
13:07:24.678 -> 2 | birds aren’t real | -66 | 10 | WPA2
13:07:24.678 -> 3 | Life-Ministries | -79 | 1 | WPA2
13:07:24.709 -> 4 | SpectrumSetup-36 | -83 | 11 | WPA2
13:07:24.709 -> 5 | kooper11 | -85 | 6 | WPA2
13:07:24.709 -> 6 | SpectrumSetup-A1 | -86 | 6 | WPA2
13:07:24.709 -> 7 | NETGEAR03 | -87 | 7 | WPA2
13:07:24.743 -> 8 | Jasper | -89 | 3 | WPA2
13:07:24.743 -> 9 | CenturyLink9198 | -91 | 1 | WPA+WPA2
13:07:24.743 -> 10 | NETGEAR81 | -92 | 1 | WPA2
13:07:24.775 -> 11 | NETGEAR28 | -92 | 1 | WPA2
13:07:24.775 -> 12 | Life-Ministries | -92 | 11 | WPA2
13:07:24.775 -> 13 | VTECH_7764_391a | -94 | 1 | WPA2
13:07:24.822 ->

13:07:24.822 ->

13:07:24.822 -> Scan done

13:07:24.822 ->

13:07:24.822 -> Connecting to WiFi Network ..

13:07:24.822 ->

13:07:24.822 -> Connecting to WiFi Network ..

13:07:24.822 -> .6

13:07:30.421 -> Connecting to WiFi Network ..

13:07:30.421 -> .4

13:07:35.606 -> Connecting to WiFi Network ..

13:07:35.606 -> .4

13:07:40.618 -> Connecting to WiFi Network ..

13:07:40.618 -> .4

#include "WiFi.h"

// Replace with your own network credentials
const char* ssid = "birds aren’t real";
const char* password = "mypassword";
 
void setup()
{
    Serial.begin(115200);
 
    // Set WiFi to station mode and disconnect from an AP if it was previously connected.
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    delay(5000);

        // WiFi.scanNetworks will return the number of networks found.
    Serial.println("Scan start");
    int n = WiFi.scanNetworks();
    
    if (n == 0) {
        Serial.println("no networks found");
    } else {
        Serial.print(n);
        Serial.println(" networks found");
        Serial.println("Nr | SSID                             | RSSI | CH | Encryption");
        for (int i = 0; i < n; ++i) {
            // Print SSID and RSSI for each network found
            Serial.printf("%2d",i + 1);
            Serial.print(" | ");
            Serial.printf("%-32.32s", WiFi.SSID(i).c_str());
            Serial.print(" | ");
            Serial.printf("%4d", WiFi.RSSI(i));
            Serial.print(" | ");
            Serial.printf("%2d", WiFi.channel(i));
            Serial.print(" | ");
            switch (WiFi.encryptionType(i))
            {
            case WIFI_AUTH_OPEN:
                Serial.print("open");
                break;
            case WIFI_AUTH_WEP:
                Serial.print("WEP");
                break;
            case WIFI_AUTH_WPA_PSK:
                Serial.print("WPA");
                break;
            case WIFI_AUTH_WPA2_PSK:
                Serial.print("WPA2");
                break;
            case WIFI_AUTH_WPA_WPA2_PSK:
                Serial.print("WPA+WPA2");
                break;
            case WIFI_AUTH_WPA2_ENTERPRISE:
                Serial.print("WPA2-EAP");
                break;
            case WIFI_AUTH_WPA3_PSK:
                Serial.print("WPA3");
                break;
            case WIFI_AUTH_WPA2_WPA3_PSK:
                Serial.print("WPA2+WPA3");
                break;
            case WIFI_AUTH_WAPI_PSK:
                Serial.print("WAPI");
                break;
            default:
                Serial.print("unknown");
            }
            Serial.println();
            delay(10);
        }
    }
    Serial.println("");

    Serial.println("Scan done");
 
    // Delete the scan result to free memory for code below.
    WiFi.scanDelete();


// Start connection to wifi
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    Serial.println("\nConnecting to WiFi Network ..");
 
    while(WiFi.status() != WL_CONNECTED){
        WiFi.begin(ssid, password);
        Serial.println("\nConnecting to WiFi Network ..");
        Serial.print(".");
        Serial.print(WiFi.status());
        delay(5000);
    }
 
    Serial.println("\nConnected to the WiFi network");
    Serial.print("Local ESP32 IP: ");
    Serial.println(WiFi.localIP());
 
 
    Serial.println("Setup done");
}

void loop()
{

 

}

Tried the code on my ESP32 and it connected just fine. I would double check your SSID and password.

I also looked at the status code, they aren't very helpful but here you go:

    WL_NO_SHIELD        = 255,   // for compatibility with WiFi Shield library
    WL_IDLE_STATUS      = 0,
    WL_NO_SSID_AVAIL    = 1,
    WL_SCAN_COMPLETED   = 2,
    WL_CONNECTED        = 3,
    WL_CONNECT_FAILED   = 4,
    WL_CONNECTION_LOST  = 5,
    WL_DISCONNECTED     = 6

Thanks. I have been able to get it to connect to my phone hotspot. Wondering if something doesn't like the apostrophe in "aren't" ..

I'm pretty sure it's the culprit.

Based on the output that doesn't look like an apostrophe (ASCII code 0x27), it's an "acute accent" so I must suppose the WiFi SSID uses such character, so I suspect you can't set it with Arduino because AFAIK using the IDE if you use extended codes in strings it'll convert them into Unicode/UTF8.
And unless you find how to encode that on Arduino, the only other practical solution I see is to change your WiFi SSID to not include any "special character" (e.g. replace the "acute accent" with a proper, standard "apostrophe").

Try this and see if it works:

const char* ssid = "birds aren\xB4t real";

Ok. thanks for the input everyone. I found the culprit. I'm using this board:

https://www.amazon.com/AITRIP-Development-Microcontroller-Integrated-Amplifiers/dp/B0B19KRPRC/ref=cm_cr_arp_d_product_top?ie=UTF8&th=1

Turns out, the wifi range seems to be awful. when i put it right next to my router, it connects, if it's 15-20 ft away, it does not.

I'm not so sure it's (only) a poor board WiFi, because on the WiFi networks dump you posted here I see your RSSI was -66 dBm, not very high but not so bad to prevent you from connecting to it. But I see also a printer configured on the same channel (10) with apparently the same strength of the router, so a problem could be a radio interference together with a poor ESP32 WiFi module. To test that, I'd try changing one of the channels (e.g. the router to channel 3 or 13) or turn the printer off, and see what happens trying to connect from the same previous position. it still won't connect, it's surely a very bad board and I'd immediately return it asking for a refund (and buy another one from a different seller...:wink: ).

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