diff --git a/wled00/json.cpp b/wled00/json.cpp index 4414681023..48d66b8c34 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -546,12 +546,19 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId) 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; + WLED::instance().shutdownAP(); + } + if (!wifi[F("on")].isNull()) { + bool sta = getBoolVal(wifi[F("on")], wifiEnabled); + if(!sta){ + if(apActive) { + WLED::instance().shutdownAP(); + } else if (WiFi.isConnected()) { + WiFi.disconnect(true); + } + } + wifiEnabled = forceReconnect = sta; } - //bool restart = wifi[F("restart")] | false; - //if (restart) forceReconnect = true; } if (stateChanged) stateUpdated(callMode); diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 63af7c2b89..5e727bc24e 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -606,6 +606,13 @@ void WLED::initAP(bool resetAP) apActive = true; } +void WLED::shutdownAP() +{ + dnsServer.stop(); + WiFi.softAPdisconnect(true); + apActive = false; +} + void WLED::initConnection() { DEBUG_PRINTF_P(PSTR("initConnection() called @ %lus.\n"), millis()/1000); @@ -645,7 +652,7 @@ void WLED::initConnection() initAP(); } else { DEBUG_PRINTLN(F("Access point disabled (init).")); - WiFi.softAPdisconnect(true); + shutdownAP(); WiFi.mode(WIFI_STA); } } @@ -755,6 +762,7 @@ void WLED::handleConnection() #endif const bool wifiConfigured = WLED_WIFI_CONFIGURED; + if (!wifiEnabled) return; // ignore connection handling if WiFi is configured and scan still running // or within first 2s if WiFi is not configured or AP is always active if ((wifiConfigured && multiWiFi.size() > 1 && WiFi.scanComplete() < 0) || (now < 2000 && (!wifiConfigured || apBehavior == AP_BEHAVIOR_ALWAYS))) @@ -818,7 +826,7 @@ void WLED::handleConnection() initConnection(); } if (!apActive && now - lastReconnectAttempt > 12000 && (!wasConnected || apBehavior == AP_BEHAVIOR_NO_CONN)) { - if (!(apBehavior == AP_BEHAVIOR_TEMPORARY && now > WLED_AP_TIMEOUT)) { + if (!(apBehavior == AP_BEHAVIOR_TEMPORARY && now > WLED_AP_TIMEOUT) && (apBehavior != AP_BEHAVIOR_BUTTON_ONLY)) { DEBUG_PRINTF_P(PSTR("Not connected AP (@ %lus).\n"), nowS); initAP(); // start AP only within first 5min } @@ -826,9 +834,7 @@ void WLED::handleConnection() 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 < 2*WLED_AP_TIMEOUT) { - dnsServer.stop(); - WiFi.softAPdisconnect(true); - apActive = false; + shutdownAP(); DEBUG_PRINTF_P(PSTR("Temporary AP disabled (@ %lus).\n"), nowS); } } @@ -848,9 +854,7 @@ void WLED::handleConnection() // shut down AP if (apBehavior != AP_BEHAVIOR_ALWAYS && apActive) { - dnsServer.stop(); - WiFi.softAPdisconnect(true); - apActive = false; + shutdownAP(); DEBUG_PRINTLN(F("Access point disabled (connected).")); } } @@ -903,4 +907,4 @@ void WLED::handleStatusLED() #endif } } -#endif +#endif \ No newline at end of file diff --git a/wled00/wled.h b/wled00/wled.h index 52bb2f9366..4301b489f7 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -348,20 +348,22 @@ typedef class WiFiOptions { uint8_t apBehavior : 3; bool noWifiSleep : 1; bool force802_3g : 1; + bool wifiEnabled : 1; }; - WiFiOptions(uint8_t s, uint8_t c, bool h, uint8_t b, bool sl, bool g) { + WiFiOptions(uint8_t s, uint8_t c, bool h, uint8_t b, bool sl, bool g, bool e) { selectedWiFi = s; apChannel = c; apHide = h; apBehavior = b; noWifiSleep = sl; force802_3g = g; + wifiEnabled = e; } } __attribute__ ((aligned(1), packed)) wifi_options_t; #ifdef ARDUINO_ARCH_ESP32 -WLED_GLOBAL wifi_options_t wifiOpt _INIT_N(({0, 1, false, AP_BEHAVIOR_BOOT_NO_CONN, true, false})); +WLED_GLOBAL wifi_options_t wifiOpt _INIT_N(({0, 1, false, AP_BEHAVIOR_BOOT_NO_CONN, true, false, true})); #else -WLED_GLOBAL wifi_options_t wifiOpt _INIT_N(({0, 1, false, AP_BEHAVIOR_BOOT_NO_CONN, false, false})); +WLED_GLOBAL wifi_options_t wifiOpt _INIT_N(({0, 1, false, AP_BEHAVIOR_BOOT_NO_CONN, false, false, true})); #endif #define selectedWiFi wifiOpt.selectedWiFi #define apChannel wifiOpt.apChannel @@ -369,6 +371,7 @@ WLED_GLOBAL wifi_options_t wifiOpt _INIT_N(({0, 1, false, AP_BEHAVIOR_BOOT_NO_CO #define apBehavior wifiOpt.apBehavior #define noWifiSleep wifiOpt.noWifiSleep #define force802_3g wifiOpt.force802_3g +#define wifiEnabled wifiOpt.wifiEnabled #else WLED_GLOBAL int8_t selectedWiFi _INIT(0); WLED_GLOBAL byte apChannel _INIT(1); // 2.4GHz WiFi AP channel (1-13) @@ -380,6 +383,7 @@ WLED_GLOBAL bool noWifiSleep _INIT(true); // disabling m WLED_GLOBAL bool noWifiSleep _INIT(false); #endif WLED_GLOBAL bool force802_3g _INIT(false); +WLED_GLOBAL bool wifiEnabled _INIT(true); #endif // WLED_SAVE_RAM #ifdef ARDUINO_ARCH_ESP32 #if defined(LOLIN_WIFI_FIX) && (defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)) @@ -1074,6 +1078,7 @@ class WLED { void beginStrip(); void handleConnection(); void initAP(bool resetAP = false); + void shutdownAP(); void initConnection(); void initInterfaces(); #if defined(STATUSLED)