trigger wifi reconnect by mqtt or ntp failures.

This commit is contained in:
Thomas Göttgens 2022-11-18 18:03:30 +01:00
parent 9665c08b59
commit 144afee29e
3 changed files with 19 additions and 13 deletions

View File

@ -39,6 +39,14 @@ unsigned long lastrun_ntp = 0;
static bool needReconnect = true; // If we create our reconnector, run it once at the beginning static bool needReconnect = true; // If we create our reconnector, run it once at the beginning
static Periodic *wifiReconnect;
void triggerReconnect()
{
needReconnect = true;
wifiReconnect->setIntervalFromNow(1000);
}
static int32_t reconnectWiFi() static int32_t reconnectWiFi()
{ {
const char *wifiName = config.network.wifi_ssid; const char *wifiName = config.network.wifi_ssid;
@ -74,6 +82,9 @@ static int32_t reconnectWiFi()
} else { } else {
DEBUG_MSG("NTP Update failed\n"); DEBUG_MSG("NTP Update failed\n");
WiFi.disconnect(false, true);
needReconnect = true;
return 1000;
} }
} }
#endif #endif
@ -85,8 +96,6 @@ static int32_t reconnectWiFi()
} }
} }
static Periodic *wifiReconnect;
bool isWifiAvailable() bool isWifiAvailable()
{ {
@ -100,20 +109,10 @@ bool isWifiAvailable()
// Disable WiFi // Disable WiFi
void deinitWifi() void deinitWifi()
{ {
/*
Note from Jm (jm@casler.org - Sept 16, 2020):
A bug in the ESP32 SDK was introduced in Oct 2019 that keeps the WiFi radio from
turning back on after it's shut off. See:
https://github.com/espressif/arduino-esp32/issues/3522
Until then, WiFi should only be allowed when there's no power
saving on the 2.4g transceiver.
*/
DEBUG_MSG("WiFi deinit\n"); DEBUG_MSG("WiFi deinit\n");
if (isWifiAvailable()) { if (isWifiAvailable()) {
WiFi.disconnect(true);
WiFi.mode(WIFI_MODE_NULL); WiFi.mode(WIFI_MODE_NULL);
DEBUG_MSG("WiFi Turned Off\n"); DEBUG_MSG("WiFi Turned Off\n");
// WiFi.printDiag(Serial); // WiFi.printDiag(Serial);

View File

@ -13,6 +13,8 @@ bool initWifi();
void deinitWifi(); void deinitWifi();
void triggerReconnect();
bool isWifiAvailable(); bool isWifiAvailable();
uint8_t getWifiDisconnectReason(); uint8_t getWifiDisconnectReason();

View File

@ -1,4 +1,5 @@
#include "MQTT.h" #include "MQTT.h"
#include "mesh/http/WiFiAPClient.h"
#include "MeshService.h" #include "MeshService.h"
#include "NodeDB.h" #include "NodeDB.h"
#include "PowerFSM.h" #include "PowerFSM.h"
@ -164,6 +165,10 @@ void MQTT::reconnect()
sendSubscriptions(); sendSubscriptions();
} else } else
DEBUG_MSG("Failed to contact MQTT server...\n"); DEBUG_MSG("Failed to contact MQTT server...\n");
#if HAS_WIFI
WiFi.disconnect(false, true);
triggerReconnect();
#endif
} }
} }