From 467f69f50e668a55da39fe360076206f5e7c7072 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Tue, 21 Nov 2023 17:23:51 +0100 Subject: [PATCH 1/4] Switch off AP after 5min - when no clients are connected in "No connection after boot" mode --- wled00/wled.cpp | 60 +++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 33c0350a6..dde3bbc85 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -34,6 +34,8 @@ void WLED::reset() void WLED::loop() { + static uint32_t lastHeap = UINT32_MAX; + static unsigned long heapTime = 0; #ifdef WLED_DEBUG static unsigned long lastRun = 0; unsigned long loopMillis = millis(); @@ -151,6 +153,21 @@ void WLED::loop() createEditHandler(false); } + // reconnect WiFi to clear stale allocations if heap gets too low + if (millis() - heapTime > 15000) { + uint32_t heap = ESP.getFreeHeap(); + if (heap < MIN_HEAP_SIZE && lastHeap < MIN_HEAP_SIZE) { + DEBUG_PRINT(F("Heap too low! ")); DEBUG_PRINTLN(heap); + forceReconnect = true; + strip.purgeSegments(true); // remove all but one segments from memory + } else if (heap < MIN_HEAP_SIZE) { + DEBUG_PRINTLN(F("Heap low, purging segments.")); + strip.purgeSegments(); + } + lastHeap = heap; + heapTime = millis(); + } + //LED settings have been saved, re-init busses //This code block causes severe FPS drop on ESP32 with the original "if (busConfigs[0] != nullptr)" conditional. Investigate! if (doInitBusses) { @@ -816,34 +833,20 @@ void WLED::initInterfaces() void WLED::handleConnection() { static byte stacO = 0; - static uint32_t lastHeap = UINT32_MAX; - static unsigned long heapTime = 0; unsigned long now = millis(); if (now < 2000 && (!WLED_WIFI_CONFIGURED || apBehavior == AP_BEHAVIOR_ALWAYS)) return; - if (lastReconnectAttempt == 0) { - DEBUG_PRINTLN(F("lastReconnectAttempt == 0")); + if (lastReconnectAttempt == 0 || forceReconnect) { + DEBUG_PRINTLN(F("Initial connect or forced reconnect.")); initConnection(); + interfacesInited = false; + forceReconnect = false; + wasConnected = false; return; } - // reconnect WiFi to clear stale allocations if heap gets too low - if (now - heapTime > 5000) { - uint32_t heap = ESP.getFreeHeap(); - if (heap < MIN_HEAP_SIZE && lastHeap < MIN_HEAP_SIZE) { - DEBUG_PRINT(F("Heap too low! ")); - DEBUG_PRINTLN(heap); - forceReconnect = true; - strip.purgeSegments(true); // remove all but one segments from memory - } else if (heap < MIN_HEAP_SIZE) { - strip.purgeSegments(); - } - lastHeap = heap; - heapTime = now; - } - byte stac = 0; if (apActive) { #ifdef ESP8266 @@ -865,14 +868,6 @@ void WLED::handleConnection() } } } - if (forceReconnect) { - DEBUG_PRINTLN(F("Forcing reconnect.")); - initConnection(); - interfacesInited = false; - forceReconnect = false; - wasConnected = false; - return; - } if (!Network.isConnected()) { if (interfacesInited) { DEBUG_PRINTLN(F("Disconnected!")); @@ -890,8 +885,15 @@ void WLED::handleConnection() initConnection(); } if (!apActive && now - lastReconnectAttempt > 12000 && (!wasConnected || apBehavior == AP_BEHAVIOR_NO_CONN)) { - DEBUG_PRINTLN(F("Not connected AP.")); - initAP(); + if (!(apBehavior == AP_BEHAVIOR_BOOT_NO_CONN && now > 300000)) { + DEBUG_PRINTLN(F("Not connected AP.")); + initAP(); // start AP only within first 5min + } + } if (apActive && apBehavior == AP_BEHAVIOR_BOOT_NO_CONN && now > 300000 && stac == 0) { // disconnect AP after 5min + dnsServer.stop(); + WiFi.softAPdisconnect(true); + apActive = false; + DEBUG_PRINTLN(F("Access point disabled (after 5min).")); } } else if (!interfacesInited) { //newly connected DEBUG_PRINTLN(""); From 27532a423744b2c4b9af1438a21c0f97321e9ca7 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Wed, 22 Nov 2023 15:30:04 +0100 Subject: [PATCH 2/4] Add new AP type to prevent user frustration. --- wled00/const.h | 1 + wled00/data/settings_wifi.htm | 10 ++++++---- wled00/wled.cpp | 6 +++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/wled00/const.h b/wled00/const.h index 5930445af..5fd87cac7 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -156,6 +156,7 @@ #define AP_BEHAVIOR_NO_CONN 1 //Open when no connection (either after boot or if connection is lost) #define AP_BEHAVIOR_ALWAYS 2 //Always open #define AP_BEHAVIOR_BUTTON_ONLY 3 //Only when button pressed for 6 sec +#define AP_BEHAVIOR_BOOT_NO_CONN_5MIN 4 //Open AP when no connection after boot but only for 5min //Notifier callMode #define CALL_MODE_INIT 0 //no updates on init, can be used to disable updates diff --git a/wled00/data/settings_wifi.htm b/wled00/data/settings_wifi.htm index f083a91d8..a503bc6ec 100644 --- a/wled00/data/settings_wifi.htm +++ b/wled00/data/settings_wifi.htm @@ -186,10 +186,12 @@ Access Point WiFi channel:
AP opens:
+ + + + + +
AP IP: Not active

Experimental

Disable WiFi sleep:
diff --git a/wled00/wled.cpp b/wled00/wled.cpp index dde3bbc85..0ad0ad716 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -885,11 +885,11 @@ void WLED::handleConnection() initConnection(); } if (!apActive && now - lastReconnectAttempt > 12000 && (!wasConnected || apBehavior == AP_BEHAVIOR_NO_CONN)) { - if (!(apBehavior == AP_BEHAVIOR_BOOT_NO_CONN && now > 300000)) { + if (!(apBehavior == AP_BEHAVIOR_BOOT_NO_CONN_5MIN && now > 300000)) { DEBUG_PRINTLN(F("Not connected AP.")); initAP(); // start AP only within first 5min } - } if (apActive && apBehavior == AP_BEHAVIOR_BOOT_NO_CONN && now > 300000 && stac == 0) { // disconnect AP after 5min + } if (apActive && apBehavior == AP_BEHAVIOR_BOOT_NO_CONN_5MIN && now > 300000 && stac == 0) { // disconnect AP after 5min dnsServer.stop(); WiFi.softAPdisconnect(true); apActive = false; @@ -914,7 +914,7 @@ void WLED::handleConnection() dnsServer.stop(); WiFi.softAPdisconnect(true); apActive = false; - DEBUG_PRINTLN(F("Access point disabled (handle).")); + DEBUG_PRINTLN(F("Access point disabled (connected).")); } } } From 6f3b5fc559b8d3d5c2cd13ba33688b7daccfd436 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sun, 10 Dec 2023 09:59:36 +0100 Subject: [PATCH 3/4] Add JSON API for AP toggle --- wled00/json.cpp | 13 +++++++++++++ wled00/wled.cpp | 13 ++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/wled00/json.cpp b/wled00/json.cpp index d3b1ca250..67f4de609 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -466,6 +466,19 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId) } } + JsonObject wifi = root[F("wifi")]; + if (!wifi.isNull()) { + bool apMode = getBoolVal(wifi[F("ap")], apActive); + if (!apActive && apMode) WLED::instance().initAP(); // start AP mode immediately + else if (apActive && !apMode) { // stop AP mode immediately + dnsServer.stop(); + WiFi.softAPdisconnect(true); + apActive = false; + } + //bool restart = wifi[F("restart")] | false; + //if (restart) forceReconnect = true; + } + stateUpdated(callMode); if (presetToRestore) currentPreset = presetToRestore; diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 0ad0ad716..3158cb31d 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -889,11 +889,14 @@ void WLED::handleConnection() DEBUG_PRINTLN(F("Not connected AP.")); initAP(); // start AP only within first 5min } - } if (apActive && apBehavior == AP_BEHAVIOR_BOOT_NO_CONN_5MIN && now > 300000 && stac == 0) { // disconnect AP after 5min - dnsServer.stop(); - WiFi.softAPdisconnect(true); - apActive = false; - DEBUG_PRINTLN(F("Access point disabled (after 5min).")); + } if (apActive && apBehavior == AP_BEHAVIOR_BOOT_NO_CONN_5MIN && now > 300000 && stac == 0) { // disconnect AP after 5min if no clients connected + // if AP was enabled more than 10min after boot or if client was connected more than 10min after boot do not disconnect AP mode + if (now < 600000) { + dnsServer.stop(); + WiFi.softAPdisconnect(true); + apActive = false; + DEBUG_PRINTLN(F("Access point disabled (after 5min).")); + } } } else if (!interfacesInited) { //newly connected DEBUG_PRINTLN(""); From 5c7b7e4182e9f2d565f7f10f730a981aee537b61 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Fri, 26 Jan 2024 15:31:09 +0100 Subject: [PATCH 4/4] Constant & override --- wled00/const.h | 5 ++++- wled00/data/settings_wifi.htm | 2 +- wled00/wled.cpp | 9 +++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/wled00/const.h b/wled00/const.h index 645859e8d..71c3cd5dd 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -157,7 +157,10 @@ #define AP_BEHAVIOR_NO_CONN 1 //Open when no connection (either after boot or if connection is lost) #define AP_BEHAVIOR_ALWAYS 2 //Always open #define AP_BEHAVIOR_BUTTON_ONLY 3 //Only when button pressed for 6 sec -#define AP_BEHAVIOR_BOOT_NO_CONN_5MIN 4 //Open AP when no connection after boot but only for 5min +#define AP_BEHAVIOR_TEMPORARY 4 //Open AP when no connection after boot but only temporary +#ifndef WLED_AP_TIMEOUT + #define WLED_AP_TIMEOUT 300000 //Temporary AP timeout +#endif //Notifier callMode #define CALL_MODE_INIT 0 //no updates on init, can be used to disable updates diff --git a/wled00/data/settings_wifi.htm b/wled00/data/settings_wifi.htm index b4a20d314..e4a6676d8 100644 --- a/wled00/data/settings_wifi.htm +++ b/wled00/data/settings_wifi.htm @@ -190,7 +190,7 @@ - +
AP IP: Not active

Experimental

diff --git a/wled00/wled.cpp b/wled00/wled.cpp index b6f7a4c8d..f5fe63ad3 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -913,17 +913,18 @@ void WLED::handleConnection() initConnection(); } if (!apActive && now - lastReconnectAttempt > 12000 && (!wasConnected || apBehavior == AP_BEHAVIOR_NO_CONN)) { - if (!(apBehavior == AP_BEHAVIOR_BOOT_NO_CONN_5MIN && now > 300000)) { + if (!(apBehavior == AP_BEHAVIOR_TEMPORARY && now > WLED_AP_TIMEOUT)) { DEBUG_PRINTLN(F("Not connected AP.")); initAP(); // start AP only within first 5min } - } if (apActive && apBehavior == AP_BEHAVIOR_BOOT_NO_CONN_5MIN && now > 300000 && stac == 0) { // disconnect AP after 5min if no clients connected + } + if (apActive && apBehavior == AP_BEHAVIOR_TEMPORARY && now > WLED_AP_TIMEOUT && stac == 0) { // disconnect AP after 5min if no clients connected // if AP was enabled more than 10min after boot or if client was connected more than 10min after boot do not disconnect AP mode - if (now < 600000) { + if (now < 2*WLED_AP_TIMEOUT) { dnsServer.stop(); WiFi.softAPdisconnect(true); apActive = false; - DEBUG_PRINTLN(F("Access point disabled (after 5min).")); + DEBUG_PRINTLN(F("Temporary AP disabled.")); } } } else if (!interfacesInited) { //newly connected