espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.73k stars 7.43k forks source link

WiFi.localIP() reports 0.0.0.0 after succesfull connect - Arduino Core 3.0.7 #10580

Closed CelliesProjects closed 2 weeks ago

CelliesProjects commented 2 weeks ago

Board

esp32-s3

Device Description

Just a plain old esp32-s3

Hardware Configuration

Nothing of consequence

Version

v3.0.7

IDE Name

PlatformIO

Operating System

Mint Linux

Flash frequency

80

PSRAM enabled

yes

Upload speed

921600

Description

WiFi.localIP() should report the IP address. Instead it displays 0.0.0.0 after a succesfull connection. After a while it does show the correct IP.

Fix

A quick fix here was to change isConnected() in WiFiSTA.cpp to the snippet below.

This change gives the expected behaviour.

/**
 * is STA interface connected?
 * @return true if STA is connected and has a IP
 */
bool WiFiSTAClass::isConnected() {
  return STA.connected() && STA.hasIP();
}

Sketch

#include <Arduino.h>
#include <WiFi.h>

const char *SSID = "xxx";
const char *PSK = "xxx";

void setup()
{
    Serial.begin(115200);
    while (!Serial)
        delay(10);

    Serial.println("connecting..");

    WiFi.begin(SSID, PSK);

    while (!WiFi.isConnected())
        delay(10);

    Serial.println(WiFi.localIP());
}

void loop()
{
    delay(1000);
    Serial.println(WiFi.localIP());
}

Debug Message

connecting..
0.0.0.0
192.168.0.253
192.168.0.253
192.168.0.253
192.168.0.253
192.168.0.253
192.168.0.253
192.168.0.253
192.168.0.253

Other Steps to Reproduce

I did not try any other hw.

I have checked existing issues, online documentation and the Troubleshooting Guide

TD-er commented 2 weeks ago

I do agree it may sound confusing, but I am not sure this really is a bug. Afterall these are two quite different concepts, connected and gotIP. It can take a while to get an IP from the DHCP server, especially for IPv6 it may take several seconds.

Not sure what "Arduino" claims how 'connected' should be interpreted.

CelliesProjects commented 2 weeks ago

@TD-er Maybe not all, but most example sketches for esp32 use this construct to connect to WiFi.

    while (!WiFi.isConnected())
        delay(10);

I don't claim it is a bug, but it surprised me. And my assumptions about code behaviour might be wrong.

TD-er commented 2 weeks ago

Yep, that's why I also wonder what "the Arduino" expected interpretation should be.

SuGlider commented 2 weeks ago

@me-no-dev - FYI.

Issue report confirmed. This behaviour started with Arduino Core 3.0.0. Such behaviour is not noticed with Arduino Core 2.0.17, which marks a change break.

Arduino WiFi Library (upstream) has no isConnect() API. It relies on status() to know when it is connected to WiFi. https://docs.arduino.cc/libraries/wifi/

Possible Status are:

typedef enum {
    WL_NO_SHIELD = 255,
        WL_IDLE_STATUS = 0,
        WL_NO_SSID_AVAIL,
        WL_SCAN_COMPLETED,
        WL_CONNECTED,
        WL_CONNECT_FAILED,
        WL_CONNECTION_LOST,
        WL_DISCONNECTED
} wl_status_t;

The WiFi Arduino API may consider implicitly that as soon as status is WL_CONNECT, there is a valid IP address associated.

The current behaviour may be acceptable or not. We will discuss it internally. I'll report here the results.

SuGlider commented 2 weeks ago

Decision: We will change WiFi.isConnected() to only return when WiFi is connected and ESP32 has got an IP address to make it work as before. I'll add a PR to fix it.

SuGlider commented 2 weeks ago

@CelliesProjects - Thank you for the report and quick fix. We will apply it!