Makes wifi try to connect to latest

This commit is contained in:
Rune Harlyk
2026-01-31 21:05:37 +01:00
parent e5e9841dd3
commit ff1444b2bc
4 changed files with 25 additions and 3 deletions
+22 -2
View File
@@ -27,6 +27,14 @@ void WiFiService::begin() {
_persistence.readFromFS();
_lastConnectionAttempt = 0;
if (state().wifi_networks_count >= 1) {
WiFi.mode(WIFI_MODE_STA);
vTaskDelay(100 / portTICK_PERIOD_MS);
uint32_t idx = state().selected_network;
if (idx >= state().wifi_networks_count) idx = 0;
configureNetwork(state().wifi_networks[idx]);
}
}
void WiFiService::reconfigureWiFiConnection() {
@@ -34,6 +42,16 @@ void WiFiService::reconfigureWiFiConnection() {
if (WiFi.disconnect(true)) _stopping = true;
}
void WiFiService::selectNetwork(uint32_t index) {
if (index >= state().wifi_networks_count) return;
updateWithoutPropagation([&](WiFiSettings &settings) {
settings.selected_network = index;
return StateUpdateResult::CHANGED;
});
_persistence.writeToFS();
reconfigureWiFiConnection();
}
void WiFiService::loop() { EXECUTE_EVERY_N_MS(reconnectDelay, manageSTA()); }
esp_err_t WiFiService::handleScan(httpd_req_t *request) {
@@ -137,8 +155,10 @@ void WiFiService::manageSTA() {
if (!attempted && state().wifi_networks_count > 0) {
attempted = true;
ESP_LOGI(TAG, "Connecting to: %s", state().wifi_networks[0].ssid);
configureNetwork(state().wifi_networks[0]);
uint32_t idx = state().selected_network;
if (idx >= state().wifi_networks_count) idx = 0;
ESP_LOGI(TAG, "Connecting to: %s", state().wifi_networks[idx].ssid);
configureNetwork(state().wifi_networks[idx]);
}
}