ESP8266 STA mode with Static IP wifi error

Hello. I have facing an issue with ESP8266 configured as Station with Static IP.

Sometimes it works well, and sometimes it "thinks" is already connected to the AP and does not try to reconnect (including it gets WL_CONNECTED when WiFi.status() is called)

I find somewhere (I don't know where) that having Static IP is an issue because the WL_CONNECTED not verifies if you are connected to WiFi, in truth it verifies if you have a local IP, maybe that is the problem.

Otherwise, I could handle the problem by ping the AP connected, so when it's really connected the ping should work well. But I've trying to find how to get the IP from the connected AP using ESP8266 as Station, but I'd no luck, every time I look for this I only find topics and tutorials saying something about ESP8266 as AP.

Here is the connection code:

this tooFar is a bool variable that is only looking if RSSI signal of WiFi is lesser than an RSSI limit to change to a better AP

if ((!WiFi.isConnected()) || (WiFi.status() != WL_CONNECTED) || (tooFar))
    if (wifi_scan())
    {
      WiFi.begin(ssid, password, WiFi.channel(best_wifi_index), mac, true);
      digitalWrite(PIN_LED_RED, HIGH);
      if (DEBUG)
      {
        Serial.print("Connecting to ");
        Serial.println(ssid);
      }
      uint8_t i = 0;
      while (WiFi.status() != WL_CONNECTED)
      {
        if (DEBUG)
        {
          Serial.print("Attempt ");
          Serial.println(i);
        }
        i++;
        delay(500);
        if (i >= 60)
        {
          WiFi.disconnect(true);
          ESP.restart();
        }
        yield();
        ESP.wdtFeed();
      }

      server.begin();
      server.setNoDelay(true);
      WiFi.setAutoReconnect(true);
      digitalWrite(PIN_LED_RED, LOW);
      if (DEBUG)
      {
        Serial.print("Ready! Use 'telnet ");
        Serial.print(WiFi.localIP());
        Serial.println(" 46864' to connect");
      }
      time_disconnected = millis();
    }

and here is the wifi_scan routine, it scan WiFi and choose the best RSSI signal

bool wifi_scan()
{
  if (DEBUG)
    Serial.println("WiFi Scan");
  int best_wifi_rssi = -1000;
  int n = WiFi.scanNetworks();
  if (n == 0)
  {
    if (DEBUG)
      Serial.println("No networks found");
    return false;
  }
  else
  {
    for (int i = 0; i < n; ++i)
      if (WiFi.SSID(i) == ssid)
        if (WiFi.RSSI(i) > best_wifi_rssi)
        {
          best_wifi_rssi = WiFi.RSSI(i);
          best_wifi_index = i;
        }
    if (best_wifi_index > -1)
    {
      for (int j = 0; j < 6; j++)
        mac[j] = WiFi.BSSID(best_wifi_index)[j];
      if (DEBUG)
      {
        Serial.print("Best WiFi: ");
        Serial.print(WiFi.SSID(best_wifi_index));
        Serial.print(" (");
        Serial.print(WiFi.RSSI(best_wifi_index));
        Serial.print(") - BSSID: ");
        for (int j = 0; j < 6; j++)
        {
          Serial.print(mac[j]);
          Serial.print(":");
        }
        Serial.print(" - Channel: ");
        Serial.println(WiFi.channel(best_wifi_index));
      }
      if (abs(WiFi.RSSI(best_wifi_index)) < LIMIT_BREAK_RSSI)
        return true;
      else
      {
        if (DEBUG)
          Serial.println("Network is too Far to conect");
        return false;
      }
    }
    else
    {
      if (DEBUG)
      {
        Serial.print("No ");
        Serial.print(ssid);
        Serial.println(" networks found");
      }
      return false;
    }
  }
}

I already know scan WiFi is working well.
And I know this is a WiFi connection problem because when the problem happens, if I scan the network I cannot find the ESP. But when the problem doesn't occurs it appears in the list.

But I've trying to find how to get the IP from the connected AP using ESP8266 as Station,

you are trying to find what ? the ESP knows what it's IP is, you just need to extract that info. Either you can use the Serial port (as it is use in debug)

if (WiFi.status() == WL_CONNECTED) {
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

or you can run a Webserver on the ESP (and run it in both AP & STA mode) and have a page to log onto, and/or you use mDNS so you could log onto it (as an STA) from the AP.

I find somewhere (I don't know where) that having Static IP is an issue because the WL_CONNECTED not verifies if you are connected to WiFi, in truth it verifies if you have a local IP, maybe that is the problem

That is a new one for me.

Sometimes it works well, and sometimes it "thinks" is already connected to the AP and does not try to reconnect (including it gets WL_CONNECTED when WiFi.status() is called)

Again i have never encountered this issue, though i don't deal with very weak networks often.
WiFi.begin(ssid, password, WiFi.channel(best_wifi_index), mac, true);for static IP i always use

WiFi.config(staIP, gateway, subnet);
WiFi.begin(ssid, password);

Since you only posted snippets, i can't be sure what is going on, that your scan method works is (i think it should ! ) but i have no context for you general connection routine, and no reference for the gateway/submask (which may not even be the same for the different networks you are connecting to)

use
WiFi.setPersistent(false);
WiFi.setAutoConnect(false)

Deva_Rishi:
you are trying to find what ? the ESP knows what it's IP is, you just need to extract that info. Either you can use the Serial port (as it is use in debug)

if (WiFi.status() == WL_CONNECTED) {

Serial.print("Connected to ");
   Serial.println(ssid);
   Serial.print("IP address: ");
   Serial.println(WiFi.localIP());



or you can run a Webserver on the ESP (and run it in both AP & STA mode) and have a page to log onto, and/or you use mDNS so you could log onto it (as an STA) from the AP.

I don't want to find the ESP IP, but the IP address of AP which ESP is connected (I have several APs, which one with it respective IP)

Deva_Rishi:

WiFi.begin(ssid, password, WiFi.channel(best_wifi_index), mac, true);

for static IP i always use

WiFi.config(staIP, gateway, subnet);

WiFi.begin(ssid, password);



Since you only posted snippets, i can't be sure what is going on, that your scan method works is (i think it should ! ) but i have no context for you general connection routine, and no reference for the gateway/submask (which may not even be the same for the different networks you are connecting to)

Juraj:
use
WiFi.setPersistent(false);
WiFi.setAutoConnect(false)

I Used at setup

WiFi.setAutoConnect(true);
  WiFi.setAutoReconnect(true);
  if (WiFi.getMode() != WIFI_STA) WiFi.mode(WIFI_STA);
  WiFi.hostname(deviceName);
  WiFi.config(ip, dns, gateway, subnet);
  wifi_connecting(); // Goes to function to try connect WiFi

what WiFi.setPersistent(false) should do?

I find something about it (but still not implemented and tested):
At here: Problem manually handling WiFi reconnect · Issue #2735 · esp8266/Arduino · GitHub

somebody says:

Just realized that autoconnect dosen't work with static wifi configuration.
If the esp is configured and connected with static ip and then powered off it auto connects with dhcp to the same AP on the next power up.

Anyway, I implemented an way to handle the problem but I didn't test yet, since I wasn't able to ping the AP because I don't know it IP, I put an extra confirm with RSSI signal, so I erase the previous scan (to clean the RSSI buffer), and then after tried a connection and the ESP thinks it already connect (what is not necessarily truth) it goes to a RSSI test, if fails loses connection and try from the beginning

setAutoConnect(false)
static IP configuration is not persistent. only SSID and password are. auto connect uses DHCP

I don't want to find the ESP IP, but the IP address of AP which ESP is connected (I have several APs, which one with it respective IP)

Well only the AP knows that, what kind of AP are we talking about ?