network fallback, fixes #3090

This commit is contained in:
MichaelDvP
2026-05-24 16:54:04 +02:00
parent d85c1dc8e8
commit 31166794dd
5 changed files with 24 additions and 25 deletions

View File

@@ -45,3 +45,4 @@ For more details go to [emsesp.org](https://emsesp.org/).
- refactored network code into a single class [#3052](https://github.com/emsesp/EMS-ESP32/pull/3052)
- check and read 0x470 as summer2_typeids[0] only if received [#2686](https://github.com/emsesp/EMS-ESP32/issues/2686), [#3055](https://github.com/emsesp/EMS-ESP32/issues/3055)
- default bus-id: gateway1(0x49), tx-mode: auto
- network fallback to AP only after start [#3090](https://github.com/emsesp/EMS-ESP32/issues/3090)

View File

@@ -93,7 +93,7 @@ lib_deps =
bblanchon/ArduinoJson @ 7.4.3
ESP32Async/AsyncTCP @ 3.4.10
ESP32Async/ESPAsyncWebServer @ 3.11.0
https://github.com/mobizt/ReadyMail.git @ 0.4.0
https://github.com/mobizt/ReadyMail.git @ 0.4.2
https://github.com/mobizt/ESP_SSLClient.git @ 3.1.3
; https://github.com/emsesp/EMS-ESP-Modules.git @ 1.0.8

View File

@@ -82,7 +82,8 @@ void Network::begin() {
WiFi.persistent(false);
WiFi.setAutoReconnect(false);
WiFi.mode(WIFI_STA);
WiFi.disconnect(true); // wipe old settings in NVS
WiFi.disconnect(true, true); // wipe old settings in NVS
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
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
@@ -235,7 +236,6 @@ void Network::reconnect() {
last_disconnect_reason_ = 0;
connect_retry_ = 0;
reconnect_count_ = 0;
phase_ = NetPhase::ETHERNET; // begin() will refine this once settings are reloaded
// reload the network settings and apply them
begin();
@@ -304,23 +304,15 @@ void Network::checkConnection() {
}
if (!still_up) {
if (network_iface_ == NetIface::WIFI) {
uint8_t reason = last_disconnect_reason_;
if (reason == 0) {
reason = WIFI_REASON_UNSPECIFIED; // event hasn't fired yet (or was cleared); avoid logging "0"
}
LOG_INFO("WiFi connection lost (reason %u: %s)", reason, disconnectReason(reason));
wifi_connect_pending_ = false;
} else if (network_iface_ == NetIface::ETHERNET) {
LOG_INFO("Ethernet connection lost");
ethernet_connect_pending_ = false;
}
juststopped_ = true;
network_iface_ = NetIface::NONE;
network_ip_ = 0;
has_ipv6_ = false;
connect_retry_ = 0;
phase_ = initialPhase();
if (network_iface_ == NetIface::ETHERNET) {
LOG_WARNING("Ethernet connection lost");
return;
}
begin();
}
#endif
}
@@ -659,7 +651,9 @@ void Network::findNetworks() {
} else if (network_iface_ == NetIface::AP) {
phase_ = NetPhase::AP;
}
if (network_iface_ == NetIface::ETHERNET && network_ip_ != 0) {
ethernet_ever_connected_ = true;
}
LOG_INFO("Network connected via %s (IP: " IPSTR ")",
network_iface_ == NetIface::ETHERNET ? "Ethernet"
: network_iface_ == NetIface::WIFI ? "WiFi"
@@ -705,18 +699,21 @@ void Network::findNetworks() {
if (connect_retry_ >= MAX_NETWORK_RECONNECTION_ATTEMPTS && phase_ != NetPhase::AP) {
if (phase_ == NetPhase::ETHERNET) {
if (ssid_.isEmpty()) {
LOG_WARNING("Ethernet failed to connect after %u attempts, falling back to AP", connect_retry_);
phase_ = NetPhase::AP;
if (!ethernet_ever_connected_) {
LOG_WARNING("Ethernet failed to connect after %u attempts, falling back to AP", connect_retry_);
phase_ = NetPhase::AP;
}
} else {
LOG_WARNING("Ethernet failed to connect after %u attempts, switching to WiFi", connect_retry_);
phase_ = NetPhase::WIFI;
}
ethernet_connect_pending_ = false;
} else if (phase_ == NetPhase::WIFI) {
LOG_WARNING("WiFi failed to connect after %u attempts, falling back to AP", connect_retry_);
phase_ = NetPhase::AP;
if (!wifi_ever_connected_) {
LOG_WARNING("WiFi failed to connect after %u attempts, falling back to AP", connect_retry_);
phase_ = NetPhase::AP;
}
wifi_connect_pending_ = false;
WiFi.disconnect(true);
}
connect_retry_ = 0;
}

View File

@@ -204,8 +204,9 @@ class Network {
NetPhase phase_ = NetPhase::ETHERNET;
bool wifi_events_registered_ = false; // ensure WiFi.onEvent() handlers are registered only once across begin()/reconnect() cycles
bool wifi_ever_connected_ = false; // set true once we've successfully obtained an IP
bool wifi_events_registered_ = false; // ensure WiFi.onEvent() handlers are registered only once across begin()/reconnect() cycles
bool wifi_ever_connected_ = false; // set true once we've successfully obtained an IP
bool ethernet_ever_connected_ = false; // set true once we've successfully obtained an IP
// Network and AP settings
bool enableMDNS_;

View File

@@ -1 +1 @@
#define EMSESP_APP_VERSION "3.9.0-dev.8"
#define EMSESP_APP_VERSION "3.9.0-dev.9"