From e99474c3cf2ea000af58f929adc52b9fc3a5fb53 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Sat, 4 Nov 2023 11:14:40 +0100 Subject: [PATCH] Reset wifi and socket once per day --- include/controller.h | 4 ++++ include/example_config.h | 3 +++ src/controller.cpp | 11 +++++++++++ src/main.cpp | 1 + 4 files changed, 19 insertions(+) diff --git a/include/controller.h b/include/controller.h index 0850293..9d5acd8 100644 --- a/include/controller.h +++ b/include/controller.h @@ -20,6 +20,8 @@ struct WifiConfiguration { // The interval to reconnect to WiFi if the connection is broken uint32_t reconnectInterval; + + uint32_t periodicReconnectInterval; }; struct KeyConfiguration { @@ -76,4 +78,6 @@ private: void prepareResponseBuffer(SesameEvent event, uint8_t deviceId = 0); void sendPreparedLocalResponse(AsyncWebServerRequest *request); void sendPreparedServerResponse(); + + void periodicallyReconnectWifiAndSocket(uint32_t millis); }; \ No newline at end of file diff --git a/include/example_config.h b/include/example_config.h index 5f767d9..fc3b399 100644 --- a/include/example_config.h +++ b/include/example_config.h @@ -49,6 +49,9 @@ constexpr const char* networkName = "Sesame-Device"; // The interval to reconnect to WiFi if the connection is broken constexpr uint32_t wifiReconnectInterval = 10000; +// The interval to reconnect to WiFi if the connection is broken +constexpr uint32_t wifiPeriodicReconnectInterval = 86400; + /* Local server */ // The port for the local server to directly receive messages over WiFi diff --git a/src/controller.cpp b/src/controller.cpp index f879589..ee7c9ef 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -46,6 +46,7 @@ void SesameController::loop(uint32_t millis) { server.loop(); servo.loop(millis); + periodicallyReconnectWifiAndSocket(millis); ensureWiFiConnection(millis); ensureWebSocketConnection(); } @@ -213,4 +214,14 @@ bool SesameController::convertHexMessageToBinary(const char* str) { receivedMessageBuffer[pos/2] = (uint8_t)(hashmap[idx0] << 4) | hashmap[idx1]; }; return true; +} + +void SesameController::periodicallyReconnectWifiAndSocket(uint32_t millis) { + static uint32_t nextWifiReconnect = wifiConfig.periodicReconnectInterval; + if (millis > nextWifiReconnect) { + nextWifiReconnect += wifiConfig.periodicReconnectInterval; + + server.disconnect(); + WiFi.disconnect(); + } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ffde7a5..2386f7e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,6 +49,7 @@ void setup() { .password = wifiPassword, .networkName = networkName, .reconnectInterval = wifiReconnectInterval, + .periodicReconnectInterval = wifiPeriodicReconnectInterval, }; KeyConfiguration keyConfig {