Skip to content

Commit acebfb9

Browse files
committed
Fix for wifi reconnection issues, delay retry for 30 sec #919
1 parent f08eafc commit acebfb9

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/sys/net/hasp_wifi.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -509,36 +509,39 @@ void wifiSetup()
509509

510510
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
511511
wifiReconnect();
512-
WiFi.setAutoReconnect(true); // done in wifiEvery5Seconds
512+
WiFi.setAutoReconnect(false); // done in wifiEvery5Seconds
513513
LOG_TRACE(TAG_WIFI, F(D_WIFI_CONNECTING_TO), wifiSsid);
514514
}
515515
#endif
516516
}
517517

518518
bool wifiEvery5Seconds()
519519
{
520+
static uint8_t disconnectionPeriod = 0; // WiFi disconnection period counter
521+
520522
#if defined(STM32F4xx)
521-
if(wifiShowAP()) { // no ssid is set yet wait for user on-screen input
522-
return false;
523-
}
523+
if(wifiShowAP()) { // no ssid is set yet wait for user on-screen input
524+
return false;
525+
}
524526
#else
525-
if(WiFi.getMode() == WIFI_AP || WiFi.getMode() == WIFI_AP_STA) {
526-
LOG_DEBUG(TAG_WIFI, F("5sec mode AP %d"), WiFi.getMode());
527-
return false;
528-
}
527+
if(WiFi.getMode() == WIFI_AP || WiFi.getMode() == WIFI_AP_STA) {
528+
LOG_DEBUG(TAG_WIFI, F("5sec mode AP %d"), WiFi.getMode());
529+
return false;
530+
}
529531
#endif
530532

531-
if(WiFi.status() == WL_CONNECTED && WiFi.localIP() > 0) {
532-
return true;
533-
}
534-
535-
// Issue #919 : Reconnects happens to quickly
536-
// if(wifiEnabled) {
537-
// LOG_WARNING(TAG_WIFI, F("No Connection... retry %d"), network_reconnect_counter);
538-
// wifiReconnect();
539-
// }
540-
541-
return false;
533+
if(WiFi.status() == WL_CONNECTED && WiFi.localIP() > 0) {
534+
disconnectionPeriod = 0; // Reset the counter if connection was established
535+
return true;
536+
}
537+
538+
if(WiFi.status() != WL_CONNECTED) { // If WiFi disconnected...
539+
if(++disconnectionPeriod >= 6) { // If 30 seconds have passed since the disconnection...
540+
disconnectionPeriod = 0; // Restart timeout period
541+
wifiReconnect(); // Reconnect to WiFi
542+
}
543+
}
544+
return false;
542545
}
543546

544547
bool wifiValidateSsid(const char* ssid, const char* pass)

0 commit comments

Comments
 (0)