⚡ Makes wifi try to connect to latest
This commit is contained in:
@@ -42,6 +42,7 @@ inline WiFiSettings WiFiSettings_defaults() {
|
|||||||
strncpy(settings.hostname, FACTORY_WIFI_HOSTNAME, sizeof(settings.hostname) - 1);
|
strncpy(settings.hostname, FACTORY_WIFI_HOSTNAME, sizeof(settings.hostname) - 1);
|
||||||
settings.priority_rssi = true;
|
settings.priority_rssi = true;
|
||||||
settings.wifi_networks_count = 0;
|
settings.wifi_networks_count = 0;
|
||||||
|
settings.selected_network = 0;
|
||||||
if (strlen(FACTORY_WIFI_SSID) > 0) {
|
if (strlen(FACTORY_WIFI_SSID) > 0) {
|
||||||
settings.wifi_networks[0] = WiFiNetwork_defaults();
|
settings.wifi_networks[0] = WiFiNetwork_defaults();
|
||||||
settings.wifi_networks_count = 1;
|
settings.wifi_networks_count = 1;
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ class WiFiService : public StatefulService<WiFiSettings> {
|
|||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
void setupMDNS(const char *hostname);
|
void setupMDNS(const char *hostname);
|
||||||
|
void selectNetwork(uint32_t index);
|
||||||
|
|
||||||
const char *getHostname() { return state().hostname; }
|
const char *getHostname() { return state().hostname; }
|
||||||
|
|
||||||
@@ -43,7 +44,6 @@ class WiFiService : public StatefulService<WiFiSettings> {
|
|||||||
|
|
||||||
void reconfigureWiFiConnection();
|
void reconfigureWiFiConnection();
|
||||||
void manageSTA();
|
void manageSTA();
|
||||||
void connectToWiFi();
|
|
||||||
void configureNetwork(WiFiNetwork &network);
|
void configureNetwork(WiFiNetwork &network);
|
||||||
|
|
||||||
unsigned long _lastConnectionAttempt;
|
unsigned long _lastConnectionAttempt;
|
||||||
|
|||||||
@@ -27,6 +27,14 @@ void WiFiService::begin() {
|
|||||||
|
|
||||||
_persistence.readFromFS();
|
_persistence.readFromFS();
|
||||||
_lastConnectionAttempt = 0;
|
_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() {
|
void WiFiService::reconfigureWiFiConnection() {
|
||||||
@@ -34,6 +42,16 @@ void WiFiService::reconfigureWiFiConnection() {
|
|||||||
if (WiFi.disconnect(true)) _stopping = true;
|
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()); }
|
void WiFiService::loop() { EXECUTE_EVERY_N_MS(reconnectDelay, manageSTA()); }
|
||||||
|
|
||||||
esp_err_t WiFiService::handleScan(httpd_req_t *request) {
|
esp_err_t WiFiService::handleScan(httpd_req_t *request) {
|
||||||
@@ -137,8 +155,10 @@ void WiFiService::manageSTA() {
|
|||||||
|
|
||||||
if (!attempted && state().wifi_networks_count > 0) {
|
if (!attempted && state().wifi_networks_count > 0) {
|
||||||
attempted = true;
|
attempted = true;
|
||||||
ESP_LOGI(TAG, "Connecting to: %s", state().wifi_networks[0].ssid);
|
uint32_t idx = state().selected_network;
|
||||||
configureNetwork(state().wifi_networks[0]);
|
if (idx >= state().wifi_networks_count) idx = 0;
|
||||||
|
ESP_LOGI(TAG, "Connecting to: %s", state().wifi_networks[idx].ssid);
|
||||||
|
configureNetwork(state().wifi_networks[idx]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -82,6 +82,7 @@ message WifiSettings {
|
|||||||
string hostname = 1;
|
string hostname = 1;
|
||||||
bool priority_rssi = 2;
|
bool priority_rssi = 2;
|
||||||
repeated WifiNetwork wifi_networks = 3; // max 5 networks
|
repeated WifiNetwork wifi_networks = 3; // max 5 networks
|
||||||
|
uint32 selected_network = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
message WifiSettingsRequest {}
|
message WifiSettingsRequest {}
|
||||||
|
|||||||
Reference in New Issue
Block a user