Home > Enterprise >  ESP32 WiFi.status() always returns WL_DICSONNECTED (STA_MODE)
ESP32 WiFi.status() always returns WL_DICSONNECTED (STA_MODE)

Time:11-16

I've spent a many hours trying to solve this.

I have added multiple attempts, tried to WiFi.disconnect() before Wifi.begin(). Nothing works: statusremains to be WL_DISCONNECTED (0x06).

WiFi.mode(WIFI_STA);
for(;;) {
        attempt  ;

        Wifi.begin(ssid, password);
        wl_status_t status = WiFi.status();
        String m = connectionStatusMessage(status);
        log("Connection attempt %d: status is%s", attempt, m.c_str());

        if (status == WL_CONNECTED) {       
            Serial.println();
            success("connected (WL_CONNECTED)");
            information();
            break;
        } 

[UPDATE] Note: I use a ESP-WROOM-32 devkit package. The ESP32 sdk being the latest stable available on PlatformIO. I tested others devkits such a one from Az-Delivery too.

CodePudding user response:

I have an idea: WiFi.begin(ssid, password) should be called only once. Put it before the loop:

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
        
for(;;) {
        attempt  ;

        wl_status_t status = WiFi.status();
        String m = connectionStatusMessage(status);
        log("Connection attempt %d: status is%s", attempt, m.c_str());

        if (status == WL_CONNECTED) {       
            Serial.println();
            success("connected (WL_CONNECTED)");
            information();
            break;
        } 
}

CodePudding user response:

I finally found a solution and I think there is a bug in WiFi.status() implementation.

The fix is to use WiFi.waitForConnectResult() instead of WiFi.status() like so;

   WiFi.mode(WIFI_STA);
   for(;;) {
        attempt  ;

        Wifi.begin(ssid, password);

        // >>>> the fix <<<<<
        uint8_t status = WiFi.waitForConnectResult();

        String m = connectionStatusMessage(status);
        log("Connection attempt %d: status is%s", attempt, m.c_str());

        if (status == WL_CONNECTED) {       
            Serial.println();
            success("connected (WL_CONNECTED)");
            information();
            break;
        } 
  • Related