mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-05-02 12:07:02 +00:00
performance updates
This commit is contained in:
@@ -65,31 +65,38 @@ void Network::begin() {
|
|||||||
ap_subnetMask_ = settings.subnetMask;
|
ap_subnetMask_ = settings.subnetMask;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Initialise WiFi - we only do this once, when the network service is started
|
// 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.
|
// We want the device to come up in opmode=0 (WIFI_OFF), which is not the default after a flash erase.
|
||||||
// If needed, we save opmode=0 before disabling persistence so the device boots with WiFi disabled in the future.
|
// Persistence is true by default, so this WiFi.mode() call writes opmode=0 to NVS for future boots.
|
||||||
if (WiFi.getMode() != WIFI_OFF) {
|
|
||||||
WiFi.mode(WIFI_OFF);
|
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.persistent(false);
|
||||||
WiFi.setAutoReconnect(false);
|
WiFi.setAutoReconnect(false);
|
||||||
|
|
||||||
WiFi.mode(WIFI_STA);
|
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
|
// 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.setScanMethod(WIFI_ALL_CHANNEL_SCAN); // default is FAST_SCAN
|
||||||
// WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); // is default, no need to set
|
// 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
|
// 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
|
// 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; },
|
// 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);
|
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
|
// 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; }, ARDUINO_EVENT_WIFI_STA_GOT_IP);
|
WiFi.onEvent(
|
||||||
|
[this](WiFiEvent_t /*event*/, WiFiEventInfo_t /*info*/) {
|
||||||
|
last_disconnect_reason_ = 0;
|
||||||
|
wifi_connect_pending_ = false;
|
||||||
|
},
|
||||||
|
ARDUINO_EVENT_WIFI_STA_GOT_IP);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,8 +180,9 @@ void Network::reconnect() {
|
|||||||
network_iface_ = NetIface::NONE;
|
network_iface_ = NetIface::NONE;
|
||||||
has_ipv6_ = false;
|
has_ipv6_ = false;
|
||||||
juststopped_ = true;
|
juststopped_ = true;
|
||||||
|
wifi_connect_pending_ = false;
|
||||||
|
|
||||||
// reload the network settings
|
// reload the network settings, as this could be called from the console
|
||||||
begin();
|
begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -410,13 +418,11 @@ const char * Network::disconnectReason(uint8_t code) {
|
|||||||
// WiFi management
|
// WiFi management
|
||||||
void Network::startWIFI() {
|
void Network::startWIFI() {
|
||||||
#ifndef EMSESP_STANDALONE
|
#ifndef EMSESP_STANDALONE
|
||||||
// Abort if already connected, or if we have no SSID
|
// Abort if already connected, or if we have no SSID or another Wifi.begin() is already in progress
|
||||||
if (WiFi.isConnected() || ssid_.length() == 0) {
|
if (WiFi.isConnected() || ssid_.length() == 0 || wifi_connect_pending_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_DEBUG("Managing WiFi"); // TODO remove
|
|
||||||
|
|
||||||
WiFi.setHostname(hostname_.c_str()); // updates shared default_hostname buffer
|
WiFi.setHostname(hostname_.c_str()); // updates shared default_hostname buffer
|
||||||
WiFi.enableSTA(true); // creates the STA netif
|
WiFi.enableSTA(true); // creates the STA netif
|
||||||
WiFi.STA.setHostname(hostname_.c_str()); // pushes to esp_netif_set_hostname
|
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
|
// attempt to connect to the network
|
||||||
uint8_t bssid[6];
|
uint8_t bssid[6];
|
||||||
wl_status_t status;
|
wl_status_t status;
|
||||||
|
wifi_connect_pending_ = true; // set before begin() so the event handlers can race-clear it safely
|
||||||
|
|
||||||
if (formatBSSID(bssid_, bssid)) {
|
if (formatBSSID(bssid_, bssid)) {
|
||||||
status = WiFi.begin(ssid_.c_str(), password_.c_str(), 0, bssid);
|
status = WiFi.begin(ssid_.c_str(), password_.c_str(), 0, bssid);
|
||||||
} else {
|
} 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());
|
status = WiFi.begin(ssid_.c_str(), password_.c_str());
|
||||||
}
|
}
|
||||||
if (status == WL_CONNECT_FAILED) {
|
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);
|
LOG_ERROR("WiFi connection failed (code %d)", status);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -478,8 +486,6 @@ void Network::startEthernet() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_DEBUG("Managing Ethernet"); // TODO remove
|
|
||||||
|
|
||||||
// configure Ethernet
|
// configure Ethernet
|
||||||
int mdc = 23; // Pin# of the I²C clock signal for the Ethernet PHY - hardcoded
|
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
|
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;
|
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 we have a connection and it's a new one, set it up
|
||||||
if (best_iface != NetIface::NONE && best_iface != previous_iface) {
|
if (best_iface != NetIface::NONE && best_iface != previous_iface) {
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
|
|
||||||
namespace emsesp {
|
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 NETWORK_RECONNECTION_DELAY_LONG 60000 // 60 seconds
|
||||||
|
|
||||||
#define MAX_NETWORK_RECONNECTION_ATTEMPTS 3 // maximum number of network reconnection attempts
|
#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
|
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;
|
volatile uint8_t last_disconnect_reason_ = 0;
|
||||||
uint16_t connnect_retry_ = 0; // number of network re-connection attempts
|
uint16_t connnect_retry_ = 0; // number of network re-connection attempts
|
||||||
|
volatile bool wifi_connect_pending_ = false;
|
||||||
|
|
||||||
// Network and AP settings
|
// Network and AP settings
|
||||||
bool enableMDNS_;
|
bool enableMDNS_;
|
||||||
|
|||||||
@@ -1648,9 +1648,12 @@ bool System::check_upgrade() {
|
|||||||
// changes going to v3.9 from an earlier version
|
// changes going to v3.9 from an earlier version
|
||||||
if (settings_version.major() == 3 && settings_version.minor() < 9) {
|
if (settings_version.major() == 3 && settings_version.minor() < 9) {
|
||||||
EMSESP::esp32React.getAPSettingsService()->update([&](APSettings & apSettings) {
|
EMSESP::esp32React.getAPSettingsService()->update([&](APSettings & apSettings) {
|
||||||
|
if (apSettings.provisionMode == 0) {
|
||||||
apSettings.provisionMode = AP_MODE_DISCONNECTED; // AP_MODE_ALWAYS has been removed
|
apSettings.provisionMode = AP_MODE_DISCONNECTED; // AP_MODE_ALWAYS has been removed
|
||||||
LOG_INFO("Upgrade: Setting AP provision mode to auto");
|
LOG_INFO("Upgrade: Setting AP provision mode to auto");
|
||||||
return StateUpdateResult::CHANGED;
|
return StateUpdateResult::CHANGED;
|
||||||
|
}
|
||||||
|
return StateUpdateResult::UNCHANGED;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user