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.