diff --git a/wled00/json.cpp b/wled00/json.cpp index c09b543f1c..9559bbf922 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -484,8 +484,21 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId) WiFi.softAPdisconnect(true); apActive = false; } - //bool restart = wifi[F("restart")] | false; - //if (restart) forceReconnect = true; + if (!wifi[F("on")].isNull()) { + bool sta = getBoolVal(wifi[F("on")], wifiEnabled); + bool pwrOff = getBoolVal(wifi[F("pwrOff")], false); + if(!sta){ + if(apActive) { + dnsServer.stop(); + WiFi.softAPdisconnect(pwrOff); + apActive = false; + } else if (Network.isConnected()) { + WiFi.disconnect(pwrOff); + } + } + wifiEnabled = forceReconnect = sta; + wifiPower = sta | !pwrOff; + } } stateUpdated(callMode); diff --git a/wled00/network.cpp b/wled00/network.cpp index 79209ff5e9..98d1eefce4 100644 --- a/wled00/network.cpp +++ b/wled00/network.cpp @@ -363,7 +363,8 @@ void WiFiEvent(WiFiEvent_t event) case ARDUINO_EVENT_WIFI_STA_CONNECTED: // followed by IDLE and SCAN_DONE DEBUG_PRINTF_P(PSTR("WiFi-E: Connected! @ %lus\n"), millis()/1000); - wasConnected = true; + if(!wifiEnabled) WiFi.disconnect(!wifiPower); + else wasConnected = true; break; case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: if (wasConnected && interfacesInited) { diff --git a/wled00/wled.cpp b/wled00/wled.cpp index cc338d23f2..0802fd8545 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -753,6 +753,7 @@ void WLED::handleConnection() const unsigned long nowS = now/1000; 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))) @@ -816,7 +817,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 } @@ -901,4 +902,4 @@ void WLED::handleStatusLED() #endif } } -#endif +#endif \ No newline at end of file diff --git a/wled00/wled.h b/wled00/wled.h index f8dc1252a8..1f3dc36253 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -345,20 +345,24 @@ typedef class WiFiOptions { uint8_t apBehavior : 3; bool noWifiSleep : 1; bool force802_3g : 1; + bool wifiEnabled : 1; + bool wifiPower : 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, bool p) { selectedWiFi = s; apChannel = c; apHide = h; apBehavior = b; noWifiSleep = sl; force802_3g = g; + wifiEnabled = e; + wifiPower = p; } } __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, 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, true})); #endif #define selectedWiFi wifiOpt.selectedWiFi #define apChannel wifiOpt.apChannel @@ -366,6 +370,8 @@ 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 +#define wifiPower wifiOpt.wifiPower #else WLED_GLOBAL int8_t selectedWiFi _INIT(0); WLED_GLOBAL byte apChannel _INIT(1); // 2.4GHz WiFi AP channel (1-13) @@ -377,6 +383,8 @@ 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); +WLED_GLOBAL bool wifiPower _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))