Switch off AP after 5min

- when no clients are connected in "No connection after boot" mode
This commit is contained in:
Blaz Kristan 2023-11-21 17:23:51 +01:00
parent b5a8f3156c
commit 467f69f50e

View File

@ -34,6 +34,8 @@ void WLED::reset()
void WLED::loop() void WLED::loop()
{ {
static uint32_t lastHeap = UINT32_MAX;
static unsigned long heapTime = 0;
#ifdef WLED_DEBUG #ifdef WLED_DEBUG
static unsigned long lastRun = 0; static unsigned long lastRun = 0;
unsigned long loopMillis = millis(); unsigned long loopMillis = millis();
@ -151,6 +153,21 @@ void WLED::loop()
createEditHandler(false); 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 //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! //This code block causes severe FPS drop on ESP32 with the original "if (busConfigs[0] != nullptr)" conditional. Investigate!
if (doInitBusses) { if (doInitBusses) {
@ -816,34 +833,20 @@ void WLED::initInterfaces()
void WLED::handleConnection() void WLED::handleConnection()
{ {
static byte stacO = 0; static byte stacO = 0;
static uint32_t lastHeap = UINT32_MAX;
static unsigned long heapTime = 0;
unsigned long now = millis(); unsigned long now = millis();
if (now < 2000 && (!WLED_WIFI_CONFIGURED || apBehavior == AP_BEHAVIOR_ALWAYS)) if (now < 2000 && (!WLED_WIFI_CONFIGURED || apBehavior == AP_BEHAVIOR_ALWAYS))
return; return;
if (lastReconnectAttempt == 0) { if (lastReconnectAttempt == 0 || forceReconnect) {
DEBUG_PRINTLN(F("lastReconnectAttempt == 0")); DEBUG_PRINTLN(F("Initial connect or forced reconnect."));
initConnection(); initConnection();
interfacesInited = false;
forceReconnect = false;
wasConnected = false;
return; 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; byte stac = 0;
if (apActive) { if (apActive) {
#ifdef ESP8266 #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 (!Network.isConnected()) {
if (interfacesInited) { if (interfacesInited) {
DEBUG_PRINTLN(F("Disconnected!")); DEBUG_PRINTLN(F("Disconnected!"));
@ -890,8 +885,15 @@ void WLED::handleConnection()
initConnection(); initConnection();
} }
if (!apActive && now - lastReconnectAttempt > 12000 && (!wasConnected || apBehavior == AP_BEHAVIOR_NO_CONN)) { if (!apActive && now - lastReconnectAttempt > 12000 && (!wasConnected || apBehavior == AP_BEHAVIOR_NO_CONN)) {
DEBUG_PRINTLN(F("Not connected AP.")); if (!(apBehavior == AP_BEHAVIOR_BOOT_NO_CONN && now > 300000)) {
initAP(); 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 } else if (!interfacesInited) { //newly connected
DEBUG_PRINTLN(""); DEBUG_PRINTLN("");