From e40beeadd48ea0df7f3f4eca4a14752ca7fdd69a Mon Sep 17 00:00:00 2001 From: proddy Date: Fri, 1 May 2026 17:04:46 +0200 Subject: [PATCH] performance updates --- src/core/network.cpp | 59 ++++++++++++++++++++++++-------------------- src/core/network.h | 3 ++- src/core/system.cpp | 9 ++++--- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/core/network.cpp b/src/core/network.cpp index 6d0cb0a4e..d29978491 100644 --- a/src/core/network.cpp +++ b/src/core/network.cpp @@ -65,31 +65,38 @@ void Network::begin() { ap_subnetMask_ = settings.subnetMask; }); - // Initialise WiFi - we only do this once, when the network service is started - // We want the device to come up in opmode=0 (WIFI_OFF), when erasing the flash this is not the default. - // If needed, we save opmode=0 before disabling persistence so the device boots with WiFi disabled in the future. - if (WiFi.getMode() != WIFI_OFF) { - WiFi.mode(WIFI_OFF); - } + // Initialise WiFi - we only do this once, when the network service is started. + // We want the device to come up in opmode=0 (WIFI_OFF), which is not the default after a flash erase. + // Persistence is true by default, so this WiFi.mode() call writes opmode=0 to NVS for future boots. + WiFi.mode(WIFI_OFF); - // Disable WiFi config persistance and auto reconnect + // From here on, mode changes stay in RAM only and don't touch NVS WiFi.persistent(false); WiFi.setAutoReconnect(false); - WiFi.mode(WIFI_STA); - WiFi.mode(WIFI_OFF); // scan settings give connect issues since arduino 2.0.14 and arduino 3.x.x with some wifi systems // WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); // default is FAST_SCAN // WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); // is default, no need to set // capture the WIFI_REASON_* code on every STA disconnect event so check_connection() can - // log a meaningful reason when its periodic poll notices we're no longer associated - WiFi.onEvent([this](WiFiEvent_t /*event*/, WiFiEventInfo_t info) { last_disconnect_reason_ = info.wifi_sta_disconnected.reason; }, - ARDUINO_EVENT_WIFI_STA_DISCONNECTED); + // log a meaningful reason when its periodic poll notices we're no longer associated. + // Also release the connect-pending guard so the next loop tick can issue a fresh WiFi.begin() + WiFi.onEvent( + [this](WiFiEvent_t /*event*/, WiFiEventInfo_t info) { + last_disconnect_reason_ = info.wifi_sta_disconnected.reason; + wifi_connect_pending_ = false; + LOG_WARNING("WiFi lost connection. Reason: %s", disconnectReason(last_disconnect_reason_)); + }, + ARDUINO_EVENT_WIFI_STA_DISCONNECTED); - // clear the saved reason on a fresh STA association so we don't log a stale code on the next disconnect - WiFi.onEvent([this](WiFiEvent_t /*event*/, WiFiEventInfo_t /*info*/) { last_disconnect_reason_ = 0; }, ARDUINO_EVENT_WIFI_STA_GOT_IP); + // clear the saved reason and the connect-pending guard on a fresh STA association + WiFi.onEvent( + [this](WiFiEvent_t /*event*/, WiFiEventInfo_t /*info*/) { + last_disconnect_reason_ = 0; + wifi_connect_pending_ = false; + }, + ARDUINO_EVENT_WIFI_STA_GOT_IP); #endif } @@ -169,12 +176,13 @@ void Network::reconnect() { #endif // reset network state - network_ip_ = 0; - network_iface_ = NetIface::NONE; - has_ipv6_ = false; - juststopped_ = true; + network_ip_ = 0; + network_iface_ = NetIface::NONE; + has_ipv6_ = false; + juststopped_ = true; + wifi_connect_pending_ = false; - // reload the network settings + // reload the network settings, as this could be called from the console begin(); } @@ -410,13 +418,11 @@ const char * Network::disconnectReason(uint8_t code) { // WiFi management void Network::startWIFI() { #ifndef EMSESP_STANDALONE - // Abort if already connected, or if we have no SSID - if (WiFi.isConnected() || ssid_.length() == 0) { + // Abort if already connected, or if we have no SSID or another Wifi.begin() is already in progress + if (WiFi.isConnected() || ssid_.length() == 0 || wifi_connect_pending_) { return; } - LOG_DEBUG("Managing WiFi"); // TODO remove - WiFi.setHostname(hostname_.c_str()); // updates shared default_hostname buffer WiFi.enableSTA(true); // creates the STA netif WiFi.STA.setHostname(hostname_.c_str()); // pushes to esp_netif_set_hostname @@ -438,13 +444,15 @@ void Network::startWIFI() { // attempt to connect to the network uint8_t bssid[6]; wl_status_t status; + wifi_connect_pending_ = true; // set before begin() so the event handlers can race-clear it safely + if (formatBSSID(bssid_, bssid)) { status = WiFi.begin(ssid_.c_str(), password_.c_str(), 0, bssid); } else { - LOG_DEBUG("Connecting to WiFi SSID %s with password %s, hostname %s", ssid_.c_str(), password_.c_str(), hostname_.c_str()); // TODO remove status = WiFi.begin(ssid_.c_str(), password_.c_str()); } if (status == WL_CONNECT_FAILED) { + wifi_connect_pending_ = false; // begin() didn't actually start anything, allow next tick to retry LOG_ERROR("WiFi connection failed (code %d)", status); } @@ -478,8 +486,6 @@ void Network::startEthernet() { return; } - LOG_DEBUG("Managing Ethernet"); // TODO remove - // configure Ethernet int mdc = 23; // Pin# of the I²C clock signal for the Ethernet PHY - hardcoded int mdio = 18; // Pin# of the I²C IO signal for the Ethernet PHY - hardcoded @@ -581,7 +587,6 @@ bool Network::findNetworks() { } auto previous_iface = NetIface::NONE; - // LOG_DEBUG("best_iface: %d, previous_iface: %d, network_iface_: %d", best_iface, previous_iface, network_iface_); // TODO remove // if we have a connection and it's a new one, set it up if (best_iface != NetIface::NONE && best_iface != previous_iface) { diff --git a/src/core/network.h b/src/core/network.h index 4caf632b9..9b5f7ed04 100644 --- a/src/core/network.h +++ b/src/core/network.h @@ -35,7 +35,7 @@ namespace emsesp { -#define NETWORK_RECONNECTION_DELAY_SHORT 2000 // 2 seconds +#define NETWORK_RECONNECTION_DELAY_SHORT 3000 // 3 seconds #define NETWORK_RECONNECTION_DELAY_LONG 60000 // 60 seconds #define MAX_NETWORK_RECONNECTION_ATTEMPTS 3 // maximum number of network reconnection attempts @@ -172,6 +172,7 @@ class Network { bool eth_started_ = false; // true after ETH.begin() has succeeded once; prevents repeated re-init while DHCP is still running volatile uint8_t last_disconnect_reason_ = 0; uint16_t connnect_retry_ = 0; // number of network re-connection attempts + volatile bool wifi_connect_pending_ = false; // Network and AP settings bool enableMDNS_; diff --git a/src/core/system.cpp b/src/core/system.cpp index c89e3a6c3..d291dfc70 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -1648,9 +1648,12 @@ bool System::check_upgrade() { // changes going to v3.9 from an earlier version if (settings_version.major() == 3 && settings_version.minor() < 9) { EMSESP::esp32React.getAPSettingsService()->update([&](APSettings & apSettings) { - apSettings.provisionMode = AP_MODE_DISCONNECTED; // AP_MODE_ALWAYS has been removed - LOG_INFO("Upgrade: Setting AP provision mode to auto"); - return StateUpdateResult::CHANGED; + if (apSettings.provisionMode == 0) { + apSettings.provisionMode = AP_MODE_DISCONNECTED; // AP_MODE_ALWAYS has been removed + LOG_INFO("Upgrade: Setting AP provision mode to auto"); + return StateUpdateResult::CHANGED; + } + return StateUpdateResult::UNCHANGED; }); }