DustinWatts / FreeTouchDeck

For interfacing with Windows/macOS/Linux using an ESP32, a touchscreen and BLE.
https://www.youtube.com/dustinwatts
MIT License
617 stars 124 forks source link

Reconnecting to AP when connection was lost #106

Closed jklaverstijn closed 1 year ago

jklaverstijn commented 1 year ago

I experience frequent lost connections when using the the configurator in station mode. I propose this code change to add a reconnect event to mitigate the issue. Works a treat in my own copy running on the Touchdown hardware. Here is my patch.

--- a/ConfigHelper.h
+++ b/ConfigHelper.h
@@ -5,6 +5,15 @@ bool startWifiStation(){
   Serial.printf("[INFO]: Connecting to %s", wificonfig.ssid);
   if (String(WiFi.SSID()) != String(wificonfig.ssid))
   {
+
+       // Attempt reconnect if connection to AP is lost
+      WiFiEventId_t eventID = WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info){
+        Serial.print("WiFi lost connection. Reason: ");
+        Serial.println(info.disconnected.reason);
+        Serial.println("Reconnecting ...");
+        WiFi.reconnect();
+      }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
+
       WiFi.mode(WIFI_STA);
       WiFi.begin(wificonfig.ssid, wificonfig.password);
       uint8_t attempts = wificonfig.attempts;
DustinWatts commented 1 year ago

Sorry for the late reply. I'll test this and if this and if it works as it should. I'll implement it! Great for doing this btw, thanks!

github-actions[bot] commented 1 year ago

This issue has been stale for a while now

github-actions[bot] commented 1 year ago

This issue has been stale for a while now