diff --git a/platformio.ini b/platformio.ini index e040e8205..7977ddb5c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -102,7 +102,7 @@ lib_ldf_mode = chain lib_compat_mode = strict shared_libdeps_dir = lib framework = arduino -upload_speed = 115200 +upload_speed = 460800 monitor_speed = 115200 ;targets = size, checkprogsize targets = diff --git a/src/ESPEasy/net/NWPluginStructs/NW002_data_struct_WiFi_AP.cpp b/src/ESPEasy/net/NWPluginStructs/NW002_data_struct_WiFi_AP.cpp index ef41d5391..19f6778db 100644 --- a/src/ESPEasy/net/NWPluginStructs/NW002_data_struct_WiFi_AP.cpp +++ b/src/ESPEasy/net/NWPluginStructs/NW002_data_struct_WiFi_AP.cpp @@ -97,6 +97,14 @@ bool NW002_data_struct_WiFi_AP::init(EventStruct *event) # ifdef ESP32 NW002_update_NAPT(); # endif + # if FEATURE_MDNS + # ifdef ESP8266 + + // notifyAPChange() is not present in the ESP32 MDNSResponder + MDNS.notifyAPChange(); + # endif // ifdef ESP8266 + # endif // if FEATURE_MDNS + return true; } @@ -108,7 +116,7 @@ bool NW002_data_struct_WiFi_AP::exit(EventStruct *event) NW_PLUGIN_INTERFACE.end(); # endif // ifdef ESP32 # ifdef ESP8266 - WiFi.softAPdisconnect(); + WiFi.softAPdisconnect(true); # endif // ifdef ESP8266 stats_and_cache.processEvents(); diff --git a/src/ESPEasy/net/_NW001_WiFi_STA.cpp b/src/ESPEasy/net/_NW001_WiFi_STA.cpp index ff39fb866..16bcd2e0e 100644 --- a/src/ESPEasy/net/_NW001_WiFi_STA.cpp +++ b/src/ESPEasy/net/_NW001_WiFi_STA.cpp @@ -97,6 +97,13 @@ bool NWPlugin_001(NWPlugin::Function function, EventStruct *event, String& strin } # endif // ifdef ESP32 + case NWPlugin::Function::NWPLUGIN_CREDENTIALS_CHANGED: + { +// ESPEasy::net::wifi::WiFi_AP_Candidates.force_reload(); // Force reload of the credentials and found APs from the last scan + + break; + } + case NWPlugin::Function::NWPLUGIN_WEBSERVER_SHOULD_RUN: { ESPEasy::net::wifi::NW001_data_struct_WiFi_STA *NW_data = diff --git a/src/ESPEasy/net/_NW002_WiFi_AP.cpp b/src/ESPEasy/net/_NW002_WiFi_AP.cpp index c5e707597..5ca1a32c4 100644 --- a/src/ESPEasy/net/_NW002_WiFi_AP.cpp +++ b/src/ESPEasy/net/_NW002_WiFi_AP.cpp @@ -18,7 +18,7 @@ # include "../../src/WebServer/Markup_Forms.h" # include "../net/Helpers/_NWPlugin_init.h" # include "../net/NWPluginStructs/NW002_data_struct_WiFi_AP.h" - +# include "../net/ESPEasyNetwork.h" // TODO TD-er: This code should be moved to this NW002 plugin # include "../net/wifi/ESPEasyWifi.h" @@ -80,14 +80,15 @@ bool NWPlugin_002(NWPlugin::Function function, EventStruct *event, String& strin case NWPlugin::Function::NWPLUGIN_WEBSERVER_SHOULD_RUN: { success = ESPEasy::net::wifi::WifiIsAP(WiFi.getMode()); -// success = ESPEasy::net::wifi::wifiAPmodeActivelyUsed(); + + // success = ESPEasy::net::wifi::wifiAPmodeActivelyUsed(); break; } case NWPlugin::Function::NWPLUGIN_FALLBACK_INTERFACE_SHOULD_START: { - if (Settings.getNetworkInterface_isFallback(event->NetworkIndex)){ + if (Settings.getNetworkInterface_isFallback(event->NetworkIndex)) { success = ESPEasy::net::wifi::shouldStartAP_fallback(); } else { success = true; @@ -181,13 +182,18 @@ bool NWPlugin_002(NWPlugin::Function function, EventStruct *event, String& strin case NWPlugin::Function::NWPLUGIN_WEBFORM_SHOW_HOSTNAME: { if (event->kvWriter) { - event->kvWriter->write({ F("Hostname"), + String hostname; + + if (ESPEasy::net::wifi::WifiIsAP(WiFi.getMode())) { # ifdef ESP32 - WiFi.AP.SSID() + hostname = WiFi.AP.SSID(); # else - WiFi.softAPSSID() + hostname = //ESPEasy::net::NetworkCreateRFCCompliantHostname(); + WiFi.softAPSSID(); # endif // ifdef ESP32 - }); + } + + event->kvWriter->write({ F("Hostname"), hostname }); success = true; } break; @@ -271,15 +277,15 @@ bool NWPlugin_002(NWPlugin::Function function, EventStruct *event, String& strin // See wifi_5g_channel_bit_t for all supported channels const int wifiChannels[] = - { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 // 2.4 GHz - ,36, 40, 44, 48 // 5 GHz U-NII-1 - ,52, 56, 60, 64, 68 // 5 GHz U-NII-2A + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 // 2.4 GHz + , 36, 40, 44, 48 // 5 GHz U-NII-1 + , 52, 56, 60, 64, 68 // 5 GHz U-NII-2A // ,72, 76, 80, 84, 88 /* , 92 */ // 5 GHz U-NII-2B - ,/* 96,*/ 100,104, 108, 112, 116, 120, 124, 128, 132, 136, 140 // 5 GHz U-NII-2C - ,144 // 5 GHz U-NII-2C/3 - ,149, 153, 157, 161, 165 // 5 GHz U-NII-3 - ,169 // 5 GHz U-NII-3/4 - ,173, 177 // 5 GHz U-NII-4 + , /* 96,*/ 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140 // 5 GHz U-NII-2C + , 144 // 5 GHz U-NII-2C/3 + , 149, 153, 157, 161, 165 // 5 GHz U-NII-3 + , 169 // 5 GHz U-NII-3/4 + , 173, 177 // 5 GHz U-NII-4 }; constexpr int nrwifiChannels = NR_ELEMENTS(wifiChannels); const FormSelectorOptions selector( diff --git a/src/ESPEasy/net/wifi/ESPEasyWiFi_state_machine.cpp b/src/ESPEasy/net/wifi/ESPEasyWiFi_state_machine.cpp index c246472b8..652ada9f3 100644 --- a/src/ESPEasy/net/wifi/ESPEasyWiFi_state_machine.cpp +++ b/src/ESPEasy/net/wifi/ESPEasyWiFi_state_machine.cpp @@ -1,8 +1,10 @@ #include "../wifi/ESPEasyWiFi_state_machine.h" +#include "ESPEasy/net/wifi/WiFi_State.h" #if FEATURE_WIFI # include "../../../src/ESPEasyCore/ESPEasy_Log.h" +# include "../../../src/Globals/ESPEasy_Scheduler.h" # include "../../../src/Globals/RTC.h" # include "../../../src/Globals/SecuritySettings.h" # include "../../../src/Globals/Settings.h" @@ -23,7 +25,7 @@ namespace wifi { # define WIFI_STATE_MACHINE_AP_ONLY_TIMEOUT 60000 void ESPEasyWiFi_t::setup() { - if (!Settings.getNetworkEnabled(NETWORK_INDEX_WIFI_STA)) return; + if (!Settings.getNetworkEnabled(NETWORK_INDEX_WIFI_STA)) { return; } // TODO TD-er: Must maybe also call 'disable()' first? @@ -47,9 +49,9 @@ void ESPEasyWiFi_t::begin() { if (WiFi_AP_Candidates.hasCandidates()) { setState(WiFiState_e::IdleWaiting, 100); } else { -// if (!Settings.DoNotStartAPfallback_ConnectFail()) { -// setState(WiFiState_e::AP_only, WIFI_STATE_MACHINE_AP_ONLY_TIMEOUT); -// } else + // if (!Settings.DoNotStartAPfallback_ConnectFail()) { + // setState(WiFiState_e::AP_only, WIFI_STATE_MACHINE_AP_ONLY_TIMEOUT); + // } else { if (WifiIsAP(WiFi.getMode())) { // TODO TD-er: Must check if any client is connected. @@ -76,7 +78,13 @@ void ESPEasyWiFi_t::loop() { // TODO TD-er: Must check what error was given??? _callbackError = false; - setState(WiFiState_e::WiFiOFF); + + if (getMode() == ESPEasyWiFi_mode_e::Setup /* && _state == WiFiState_e::AP_Fallback */) { + + setState(_state == WiFiState_e::STA_Connected_Setup ? WiFiState_e::STA_Connected : WiFiState_e::IdleWaiting); + } else { + setState(WiFiState_e::WiFiOFF); + } } } @@ -93,16 +101,24 @@ void ESPEasyWiFi_t::loop() case WiFiState_e::AP_only: case WiFiState_e::AP_Fallback: - if (WiFi_AP_Candidates.hasCandidates() || - (_state_timeout.timeReached() && - !ESPEasy::net::wifi::wifiAPmodeActivelyUsed())) { - setState(WiFiState_e::IdleWaiting, 100); + // TODO TD-er: Should also check for 'setup' mode, so we switch to connecting state instead of IdleWaiting + // For sure not just when there are candidates. + if (!ESPEasy::net::wifi::wifiAPmodeActivelyUsed()) { + if (WiFi_AP_Candidates.hasCandidates() || + _state_timeout.timeReached()) { + setState(WiFiState_e::IdleWaiting, 100); + } } break; case WiFiState_e::IdleWaiting: if (connected()) { - setState(WiFiState_e::STA_Connected, 100); + if (getMode() == ESPEasyWiFi_mode_e::Setup) { + setState(WiFiState_e::STA_Connected_Setup, 60000); + } + else { + setState(WiFiState_e::STA_Connected, 100); + } break; } @@ -183,6 +199,7 @@ void ESPEasyWiFi_t::loop() // FIXME TD-er: This might not be a responsibility of this state machine.... if (shouldStartAP_fallback()) { setState(WiFiState_e::AP_Fallback, Settings.APfallback_minimal_on_time_sec() * 1000); + // TODO TD-er: Must keep track of whether the user has forced AP to be autostarted. } else { setState(WiFiState_e::WiFiOFF, 1000); @@ -249,7 +266,12 @@ void ESPEasyWiFi_t::loop() const STA_connected_state sta_connected_state = getSTA_connected_state(); if (sta_connected_state == STA_connected_state::Connected) { - setState(WiFiState_e::STA_Connected); + if (getMode() == ESPEasyWiFi_mode_e::Setup) { + setState(WiFiState_e::STA_Connected_Setup, 60000); + } + else { + setState(WiFiState_e::STA_Connected, 100); + } } else if (_state_timeout.timeReached()) { if (_state == WiFiState_e::STA_Connecting) { setState(WiFiState_e::STA_Reconnecting, WIFI_STATE_MACHINE_STA_CONNECTING_TIMEOUT); @@ -317,10 +339,10 @@ void ESPEasyWiFi_t::setState(WiFiState_e newState, uint32_t timeout) { } # endif // ifndef BUILD_NO_DEBUG - if (_state == WiFiState_e::AP_only || - _state == WiFiState_e::AP_Fallback) { - setAPinternal(false); - setAP(false); + if ((_state == WiFiState_e::AP_only) || + (_state == WiFiState_e::AP_Fallback)) { + Scheduler.setNetworkExitTimer(0, NETWORK_INDEX_WIFI_AP); +// setAP(false); } if (_state == WiFiState_e::STA_Connected) @@ -366,7 +388,7 @@ void ESPEasyWiFi_t::setState(WiFiState_e newState, uint32_t timeout) { break; case WiFiState_e::AP_only: case WiFiState_e::AP_Fallback: - setAPinternal(true); + Scheduler.setNetworkInitTimer(0, NETWORK_INDEX_WIFI_AP); break; case WiFiState_e::IdleWaiting: // Do nothing here as we're waiting till the timeout is over @@ -386,6 +408,7 @@ void ESPEasyWiFi_t::setState(WiFiState_e newState, uint32_t timeout) { // Start connecting ++_connect_attempt; + if (!connectSTA()) { // TODO TD-er: Must keep track of failed attempts and start AP when either no credentials present or nr. of attempts failed > some // threshold. @@ -396,6 +419,20 @@ void ESPEasyWiFi_t::setState(WiFiState_e newState, uint32_t timeout) { } } break; + + case WiFiState_e::STA_Connected_Setup: + { + Scheduler.setNetworkInitTimer(0, NETWORK_INDEX_WIFI_AP); + _last_seen_connected.setNow(); + auto wifi_STA_data = getWiFi_STA_NWPluginData_static_runtime(); + + if (wifi_STA_data) { + wifi_STA_data->mark_connected(); + } + + break; + } + case WiFiState_e::STA_Connected: { # ifdef ESP32 @@ -403,6 +440,11 @@ void ESPEasyWiFi_t::setState(WiFiState_e newState, uint32_t timeout) { // FIXME TD-er: Must move to ESP32-specific cpp file // WiFi.STA.setDefault(); # endif // ifdef ESP32 + + if (_state == WiFiState_e::STA_Connected_Setup) { + Scheduler.setNetworkExitTimer(0, NETWORK_INDEX_WIFI_AP); + setMode(ESPEasyWiFi_mode_e::STA_only); + } _connect_attempt = 0; _last_seen_connected.setNow(); _state_timeout.clear(); @@ -464,7 +506,7 @@ bool ESPEasyWiFi_t::connectSTA() // No need to wait longer to start AP mode. if (!Settings.DoNotStartAPfallback_ConnectFail()) { - // setAPinternal(true); + // Scheduler.setNetworkInitTimer(0, NETWORK_INDEX_WIFI_AP); } return false; } @@ -589,8 +631,13 @@ bool ESPEasyWiFi_t::connectSTA() bool ESPEasyWiFi_t::shouldStartAP_fallback() const { - if ((Settings.APfallback_autostart_max_uptime_m() * 1000) > millis()) { - return false; + if (!Settings.getNetworkInterface_isFallback(NETWORK_INDEX_WIFI_AP)) { return false; } + + + if (Settings.APfallback_autostart_max_uptime_m() != 0) { + if ((Settings.APfallback_autostart_max_uptime_m() * 60000) < millis()) { + return false; + } } if (Settings.StartAPfallback_NoCredentials() && !SecuritySettings.hasWiFiCredentials()) { @@ -601,12 +648,10 @@ bool ESPEasyWiFi_t::shouldStartAP_fallback() const return false; } - return (Settings.ConnectFailRetryCount > 0) && + return (Settings.ConnectFailRetryCount > 0) && (_connect_attempt > Settings.ConnectFailRetryCount); } - - } // namespace wifi } // namespace net } // namespace ESPEasy diff --git a/src/ESPEasy/net/wifi/ESPEasyWiFi_state_machine.h b/src/ESPEasy/net/wifi/ESPEasyWiFi_state_machine.h index a72a12b23..8bdaedd5a 100644 --- a/src/ESPEasy/net/wifi/ESPEasyWiFi_state_machine.h +++ b/src/ESPEasy/net/wifi/ESPEasyWiFi_state_machine.h @@ -20,6 +20,31 @@ class ESPEasyWiFi_t { public: + /* + + Modes of WiFi: + - Setup: AP active, no WiFi credentials, or no known WiFi AP in reach + Should be initiated via /setup page (or 'setup' command????) + - Normal operations: + - Off + - STA-only, AP as fallback + - STA+AP (e.g. to allow others to use ESP as gateway, ESP32-only) + - AP-only + + */ + enum class ESPEasyWiFi_mode_e { + Off, + STA_only, + STA_AP, + AP_only, + Setup + }; + + ESPEasyWiFi_mode_e getMode() const { return _mode; } + + void setMode(ESPEasyWiFi_mode_e mode) { _mode = mode; } + + // Called after settings have changed or at boot void setup(); @@ -85,6 +110,8 @@ private: // uint8_t _last_channel = 0; WiFiState_e _state = WiFiState_e::Disabled; + ESPEasyWiFi_mode_e _mode = ESPEasyWiFi_mode_e::Off; + LongTermTimer _last_state_change; LongTermTimer _state_timeout; LongTermTimer _last_seen_connected; diff --git a/src/ESPEasy/net/wifi/ESPEasyWifi_abstracted.cpp b/src/ESPEasy/net/wifi/ESPEasyWifi_abstracted.cpp index b6fad622d..2d91f58db 100644 --- a/src/ESPEasy/net/wifi/ESPEasyWifi_abstracted.cpp +++ b/src/ESPEasy/net/wifi/ESPEasyWifi_abstracted.cpp @@ -138,13 +138,15 @@ bool doSetAPinternal(bool enable) channel = WiFi.channel(); } +#ifdef ESP32 doSetAP(true); +#endif if (WiFi.softAP(softAPSSID.c_str(), pwd.c_str(), channel)) { auto data = getWiFi_AP_NWPluginData_static_runtime(); if (data) { data->mark_start(); } -#ifndef BUILD_NO_DEBUG +//#ifndef BUILD_NO_DEBUG if (loglevelActiveFor(LOG_LEVEL_INFO)) { addLogMove(LOG_LEVEL_INFO, strformat( F("WIFI : AP Mode enabled. SSID: %s IP: %s ch: %d"), @@ -152,14 +154,16 @@ bool doSetAPinternal(bool enable) formatIP(WiFi.softAPIP()).c_str(), channel)); } +//#endif } else { +//#ifndef BUILD_NO_DEBUG if (loglevelActiveFor(LOG_LEVEL_ERROR)) { addLogMove(LOG_LEVEL_ERROR, strformat( F("WIFI : Error while starting AP Mode with SSID: %s IP: %s"), softAPSSID.c_str(), formatIP(apIP).c_str())); } -#endif +//#endif return false; } @@ -186,8 +190,8 @@ bool doSetAPinternal(bool enable) # if FEATURE_DNS_SERVER if (!dnsServerActive) { - dnsServerActive = true; - dnsServer.start(DNS_PORT, "*", WiFi.softAPIP()); + dnsServerActive = + dnsServer.start(DNS_PORT, "*", WiFi.softAPIP()); } # endif // if FEATURE_DNS_SERVER # endif // ifdef ESP32 diff --git a/src/ESPEasy/net/wifi/ESPEasyWifi_abstracted_ESP32.cpp b/src/ESPEasy/net/wifi/ESPEasyWifi_abstracted_ESP32.cpp index 1c7932725..2e893adf0 100644 --- a/src/ESPEasy/net/wifi/ESPEasyWifi_abstracted_ESP32.cpp +++ b/src/ESPEasy/net/wifi/ESPEasyWifi_abstracted_ESP32.cpp @@ -45,19 +45,21 @@ bool WiFi_pre_STA_setup() void doWiFiDisconnect() { uint8_t retry = 3; + while (!WiFi.disconnect(Settings.WiFiRestart_connection_lost()) && retry) { --retry; delay(100); } -/* - { - const IPAddress ip; - const IPAddress gw; - const IPAddress subnet; - const IPAddress dns; - WiFi.config(ip, gw, subnet, dns); - } -*/ + + /* + { + const IPAddress ip; + const IPAddress gw; + const IPAddress subnet; + const IPAddress dns; + WiFi.config(ip, gw, subnet, dns); + } + */ } bool doWifiIsAP(WiFiMode_t wifimode) { return (wifimode == WIFI_MODE_AP) || (wifimode == WIFI_MODE_APSTA); } @@ -67,12 +69,15 @@ bool doWifiIsSTA(WiFiMode_t wifimode) { return (wifimode == WIFI_MODE_STA) || (w bool doSetWifiMode(WiFiMode_t new_mode) { if (!Settings.getNetworkEnabled(NETWORK_INDEX_WIFI_AP)) { - if (new_mode == WIFI_MODE_AP) new_mode = WIFI_OFF; - if (new_mode == WIFI_MODE_APSTA) new_mode = WIFI_MODE_STA; + if (new_mode == WIFI_MODE_AP) { new_mode = WIFI_OFF; } + + if (new_mode == WIFI_MODE_APSTA) { new_mode = WIFI_MODE_STA; } } + if (!Settings.getNetworkEnabled(NETWORK_INDEX_WIFI_STA)) { - if (new_mode == WIFI_MODE_STA) new_mode = WIFI_OFF; - if (new_mode == WIFI_MODE_APSTA) new_mode = WIFI_MODE_AP; + if (new_mode == WIFI_MODE_STA) { new_mode = WIFI_OFF; } + + if (new_mode == WIFI_MODE_APSTA) { new_mode = WIFI_MODE_AP; } } const WiFiMode_t cur_mode = WiFi.getMode(); @@ -112,21 +117,22 @@ bool doSetWifiMode(WiFiMode_t new_mode) doWiFiDisconnect(); // delay(100); - //processDisconnect(); + // processDisconnect(); // WiFiEventData.clear_processed_flags(); } addLog(LOG_LEVEL_INFO, concat(F("WIFI : Set WiFi to "), doGetWifiModeString(new_mode))); - # if FEATURE_DNS_SERVER + # if FEATURE_DNS_SERVER + if (!doWifiIsAP(new_mode)) { if (dnsServerActive) { dnsServerActive = false; dnsServer.stop(); } } - #endif + # endif // if FEATURE_DNS_SERVER int retry = 2; @@ -205,15 +211,15 @@ bool doSetWifiMode(WiFiMode_t new_mode) if (doWifiIsAP(cur_mode) != new_mode_AP_enabled) { // Mode has changed - doSetAPinternal(new_mode_AP_enabled); + // May cause recursive loop ???? + // FIXME TD-er: Should we do this here??? + if (new_mode_AP_enabled) { + Scheduler.setNetworkInitTimer(0, NETWORK_INDEX_WIFI_AP); + } + else { + Scheduler.setNetworkExitTimer(0, NETWORK_INDEX_WIFI_AP); + } } - # if FEATURE_MDNS - # ifdef ESP8266 - - // notifyAPChange() is not present in the ESP32 MDNSResponder - MDNS.notifyAPChange(); - # endif // ifdef ESP8266 - # endif // if FEATURE_MDNS return true; } @@ -240,7 +246,8 @@ void doWifiScan(bool async, uint8_t channel) { } START_TIMER; -// WiFiEventData.lastScanMoment.setNow(); + + // WiFiEventData.lastScanMoment.setNow(); # ifndef BUILD_NO_DEBUG if (loglevelActiveFor(LOG_LEVEL_INFO)) { @@ -252,13 +259,15 @@ void doWifiScan(bool async, uint8_t channel) { } # endif // ifndef BUILD_NO_DEBUG bool show_hidden = true; -// WiFiEventData.lastGetScanMoment.setNow(); -// WiFiEventData.lastScanChannel = channel; + + // WiFiEventData.lastGetScanMoment.setNow(); + // WiFiEventData.lastScanChannel = channel; unsigned int nrScans = 1 + (async ? 0 : Settings.ConnectFailRetryCount); while (nrScans > 0) { WiFi_AP_Candidates.begin_scan(); + if (!async) { FeedSW_watchdog(); } @@ -283,7 +292,8 @@ void doWifiScan(bool async, uint8_t channel) { if (!async) { FeedSW_watchdog(); WiFi_AP_Candidates.process_WiFiscan(); -// processScanDone(); + + // processScanDone(); } } # if FEATURE_TIMING_STATS @@ -303,9 +313,10 @@ void doWifiScan(bool async, uint8_t channel) { addLog(LOG_LEVEL_INFO, F("WiFi : Disconnect after scan")); # endif -// const bool needReconnect = WiFiEventData.wifiConnectAttemptNeeded; + // const bool needReconnect = WiFiEventData.wifiConnectAttemptNeeded; WifiDisconnect(); -// WiFiEventData.wifiConnectAttemptNeeded = needReconnect; + + // WiFiEventData.wifiConnectAttemptNeeded = needReconnect; } # endif // if ESP_IDF_VERSION_MAJOR < 5 # if CONFIG_SOC_WIFI_SUPPORT_5G @@ -467,11 +478,11 @@ void doSetConnectionSpeed(bool ForceWiFi_bg_mode) # ifndef SOC_WIFI_SUPPORTED return; # else -#if ESP_IDF_VERSION_MAJOR < 6 +# if ESP_IDF_VERSION_MAJOR < 6 esp_wifi_set_bandwidth(WIFI_IF_STA, WIFI_BW_HT20); -#else +# else esp_wifi_set_bandwidth(WIFI_IF_STA, WIFI_BW20); -#endif +# endif // if ESP_IDF_VERSION_MAJOR < 6 # if CONFIG_SOC_WIFI_SUPPORT_5G diff --git a/src/ESPEasy/net/wifi/ESPEasyWifi_abstracted_ESP8266.cpp b/src/ESPEasy/net/wifi/ESPEasyWifi_abstracted_ESP8266.cpp index f88aa4d2f..e5824e7a0 100644 --- a/src/ESPEasy/net/wifi/ESPEasyWifi_abstracted_ESP8266.cpp +++ b/src/ESPEasy/net/wifi/ESPEasyWifi_abstracted_ESP8266.cpp @@ -137,14 +137,6 @@ bool doSetWifiMode(WiFiMode_t new_mode) delay(1); } - -# if FEATURE_MDNS - # ifdef ESP8266 - - // notifyAPChange() is not present in the ESP32 MDNSResponder - MDNS.notifyAPChange(); - # endif // ifdef ESP8266 - # endif // if FEATURE_MDNS return true; } diff --git a/src/ESPEasy/net/wifi/WiFi_State.cpp b/src/ESPEasy/net/wifi/WiFi_State.cpp index 33b66b9b0..132545f31 100644 --- a/src/ESPEasy/net/wifi/WiFi_State.cpp +++ b/src/ESPEasy/net/wifi/WiFi_State.cpp @@ -21,6 +21,7 @@ const __FlashStringHelper* toString(WiFiState_e state) case ESPEasy::net::wifi::WiFiState_e::STA_Connecting: return F("STA_Connecting"); case ESPEasy::net::wifi::WiFiState_e::STA_Reconnecting: return F("STA_Reconnecting"); case ESPEasy::net::wifi::WiFiState_e::STA_Connected: return F("STA_Connected"); + case ESPEasy::net::wifi::WiFiState_e::STA_Connected_Setup: return F("STA_Connected_Setup"); } return F(""); } diff --git a/src/ESPEasy/net/wifi/WiFi_State.h b/src/ESPEasy/net/wifi/WiFi_State.h index 6842150e6..98e8f039b 100644 --- a/src/ESPEasy/net/wifi/WiFi_State.h +++ b/src/ESPEasy/net/wifi/WiFi_State.h @@ -86,7 +86,9 @@ enum class WiFiState_e STA_Reconnecting, // Connected to an AP - STA_Connected + STA_Connected, + + STA_Connected_Setup }; diff --git a/src/src/Commands/WiFi.cpp b/src/src/Commands/WiFi.cpp index fccd772a1..3a043318b 100644 --- a/src/src/Commands/WiFi.cpp +++ b/src/src/Commands/WiFi.cpp @@ -16,6 +16,7 @@ # include "../Helpers/StringConverter.h" # include "../../ESPEasy/net/wifi/ESPEasyWifi.h" +# include "../../ESPEasy/net/ESPEasyNetwork.h" # define WIFI_MODE_MAX (WiFiMode_t)4 @@ -92,7 +93,7 @@ const __FlashStringHelper* Command_Wifi_Disconnect(struct EventStruct *event, co const __FlashStringHelper* Command_Wifi_APMode(struct EventStruct *event, const char *Line) { - if (!Settings.getNetworkEnabled(1)) { return return_command_failed_flashstr(); } + if (!Settings.getNetworkEnabled(NETWORK_INDEX_WIFI_AP)) { return return_command_failed_flashstr(); } String TmpStr1; bool APenable = true; @@ -113,10 +114,10 @@ const __FlashStringHelper* Command_Wifi_APMode(struct EventStruct *event, const } if (APenable) { - Scheduler.setNetworkInitTimer(10, 1); + Scheduler.setNetworkInitTimer(10, NETWORK_INDEX_WIFI_AP); } else { - Scheduler.setNetworkExitTimer(10, 1); + Scheduler.setNetworkExitTimer(10, NETWORK_INDEX_WIFI_AP); } return return_command_success_flashstr(); } diff --git a/src/src/Helpers/ESPEasy_Storage.cpp b/src/src/Helpers/ESPEasy_Storage.cpp index db82bd04e..5f0d9b3dc 100644 --- a/src/src/Helpers/ESPEasy_Storage.cpp +++ b/src/src/Helpers/ESPEasy_Storage.cpp @@ -891,7 +891,7 @@ String SaveSecuritySettings(bool forFactoryReset) { ESPEasy::net::wifi::WiFi_AP_Candidates.force_reload(); // Force reload of the credentials and found APs from the last scan if (!ESPEasy::net::NetworkConnected()) { // WiFiEventData.wifiConnectAttemptNeeded = true; - ESPEasy::net::wifi::resetWiFi(); + ESPEasy::net::wifi::initWiFi();// resetWiFi(); String dummy; ESPEasy::net::NWPluginCall( NWPlugin::Function::NWPLUGIN_CREDENTIALS_CHANGED, 0, dummy); diff --git a/src/src/Helpers/Misc.cpp b/src/src/Helpers/Misc.cpp index 1d081705f..f7c42a343 100644 --- a/src/src/Helpers/Misc.cpp +++ b/src/src/Helpers/Misc.cpp @@ -88,7 +88,7 @@ bool setNetworkEnableStatus(ESPEasy::net::networkIndex_t networkIndex, bool enab if (!enabled) { // Use the scheduler as this also removes any pending init calls. - Scheduler.setNetworkExitTimer(10, networkIndex); + Scheduler.setNetworkExitTimer(0, networkIndex); } Settings.setNetworkEnabled(networkIndex, enabled); diff --git a/src/src/Helpers/Scheduler_NetworkTimer.cpp b/src/src/Helpers/Scheduler_NetworkTimer.cpp index d4b705e07..694b01b9a 100644 --- a/src/src/Helpers/Scheduler_NetworkTimer.cpp +++ b/src/src/Helpers/Scheduler_NetworkTimer.cpp @@ -55,6 +55,10 @@ void ESPEasy_Scheduler::setNetworkTimer(unsigned long msecFromNow systemTimers[timerID.mixed_id] = timer_data; setNewTimerAt(timerID, millis() + msecFromNow); } + + if (msecFromNow == 0) { + process_network_timer(timerID); + } } void ESPEasy_Scheduler::process_network_timer(SchedulerTimerID id) diff --git a/src/src/WebServer/SetupPage.cpp b/src/src/WebServer/SetupPage.cpp index a00a35694..d2ccbb2d5 100644 --- a/src/src/WebServer/SetupPage.cpp +++ b/src/src/WebServer/SetupPage.cpp @@ -1,4 +1,5 @@ #include "../WebServer/SetupPage.h" +#include "ESPEasy/net/Globals/ESPEasyWiFi.h" #ifdef WEBSERVER_SETUP @@ -140,6 +141,7 @@ void handle_setup() { // WiFiEventData.wifiSetupConnect = true; // WiFiEventData.wifiConnectAttemptNeeded = true; ESPEasy::net::wifi::WiFi_AP_Candidates.force_reload(); // Force reload of the credentials and found APs from the last scan + ESPEasyWiFi.setMode(ESPEasy::net::wifi::ESPEasyWiFi_t::ESPEasyWiFi_mode_e::Setup); # ifndef BUILD_NO_DEBUG if (loglevelActiveFor(LOG_LEVEL_INFO)) {