WiFi Auto Reconnect problems
User mickeypop investigated and found some solutions:
setup()
{
WiFi.begin( rssiSSID , password );
int WLcount = 0;
while (WiFi.status() != WL_CONNECTED && WLcount < 250 )
{
delay( 100 );
#ifdef DEBUG
Serial.print(".");
#endif
++WLcount;
}
}
void loop()
{
if (WiFi.status() == WL_CONNECTED)
{
// PUT_YOUR_UP_TIME_CODE_HERE
#ifdef DEBUG
Serial.print("C");
if (UpCount >= 20) // just keep terminal from scrolling sideways
{
UpCount = 0;
Serial.println();
}
++UpCount;
#endif
// END WiFi connected loop()
} else
{
// PUT_YOUR_DOWN_TIME_CODE_HERE
// WiFi DOWN loop
#ifdef DEBUG
WFstatus = getWifiStatus( WFstatus );
#endif
WiFi.begin( rssiSSID , password );
int WLcount = 0;
// loop - depending on router, connect can be from 4 - 20 seconds
// usually longer to reconnect than the connect at boot
// lopping till reconnect seems to let it settle
// usually around 80 count to breakout
while (WiFi.status() != WL_CONNECTED && WLcount < 250 )
{
delay( 100 );
#ifdef DEBUG
Serial.print(".");
// just keep terminal from scrolling sideways in test
if (UpCount >= 20)
{
UpCount = 0;
Serial.println();
}
++UpCount;
#endif
++WLcount;
}
delay( 1000 );
} // END WiFi DOWN
} // END loop()
int getWifiStatus( int WiFiStatus )
{
WiFiStatus = WiFi.status();
Serial.print("\tStatus "); Serial.print( WiFiStatus );
switch( WiFiStatus )
{
case WL_IDLE_STATUS : // WL_IDLE_STATUS = 0,
Serial.println(", WiFi IDLE ");
break;
case WL_NO_SSID_AVAIL: // WL_NO_SSID_AVAIL = 1,
Serial.println(", NO SSID AVAIL ");
break;
case WL_SCAN_COMPLETED: // WL_SCAN_COMPLETED = 2,
Serial.println(", WiFi SCAN_COMPLETED ");
break;
case WL_CONNECTED: // WL_CONNECTED = 3,
Serial.println(", WiFi CONNECTED ");
WiFi.persistent(true);
break;
case WL_CONNECT_FAILED: // WL_CONNECT_FAILED = 4,
Serial.println(", WiFi WL_CONNECT FAILED");
break;
case WL_CONNECTION_LOST: // WL_CONNECTION_LOST = 5,
Serial.println(", WiFi CONNECTION LOST");
WiFi.persistent(false); // don't keep writing FLASH
break;
case WL_DISCONNECTED: // WL_DISCONNECTED = 6
Serial.println(", WiFi DISCONNECTED ==");
break;
}
return WiFiStatus;
} // END getWifiStatus()
Additional explanations:
While one bypasses some library issues the other uses the WiFi.onEvent() service, both simply detect when a connection is broken and re-initiate a new connection.
While some have noted after several hours they keep getting bumped, this is almost certainly a function of the lease time of DHCP server in the router.
If the lease time of the router is not the same sometimes devices “forget” to update causing a no IP address even though sometimes the WiFi connect is still there.
- WiFi.begin() forces the DHCP to get a new IP along with a new connection.
- WiFi.reconnect() works but often assumes the unit still has an IP and may not do a DHCP request.
The simple way around this is to Ping the DHCP router periodically.
This forces the router to know you are still on the network and keep the lease current.
Rebooting may reconnect, but does not address the WHY of getting disconnected and often applications can get out of sync if you are simply rebooting.