diff --git a/Network.cpp b/Network.cpp index 2a7c143..45b00bd 100644 --- a/Network.cpp +++ b/Network.cpp @@ -10,8 +10,6 @@ #include "SSDP.h" #include "MQTT.h" -int testFallback = 5; - extern ConfigSettings settings; extern Web webServer; extern SocketEmitter sockEmit; @@ -397,13 +395,20 @@ void Network::updateHostname() { } } bool Network::connectWiFi() { + if(this->softAPOpened && WiFi.softAPgetStationNum() > 0) { + WiFi.disconnect(false); + this->_connecting = false; + this->connType = conn_types::unset; + return true; + } + if(settings.WIFI.ssid[0] != '\0') { if(WiFi.status() == WL_CONNECTED && WiFi.SSID().compareTo(settings.WIFI.ssid) == 0) { // If we are connected to the target SSID then just return. this->disconnected = 0; return true; } - if(this->softAPOpened) Serial.printf("connectWiFi: %d\n", WiFi.softAPgetStationNum()); + if(this->_connecting) return true; this->_connecting = true; this->connTarget = conn_types::wifi; this->connType = conn_types::unset; @@ -419,6 +424,7 @@ bool Network::connectWiFi() { else Serial.println("Connecting to AP"); // If the soft AP is currently opened then we do not want to kill it. WiFi.setSleep(false); + WiFi.disconnect(false); //WiFi.mode(WIFI_MODE_NULL); if(!settings.IP.dhcp) { if(!WiFi.config(settings.IP.ip, settings.IP.gateway, settings.IP.subnet, settings.IP.dns1, settings.IP.dns2)) @@ -517,6 +523,7 @@ bool Network::getStrongestAP(const char *ssid, uint8_t *bssid, int32_t *channel) } bool Network::openSoftAP() { if(this->softAPOpened || this->openingSoftAP) return true; + WiFi.disconnect(false); this->openingSoftAP = true; Serial.println(); Serial.println("Turning the HotSpot On"); diff --git a/SomfyController.ino.esp32.bin b/SomfyController.ino.esp32.bin index fadaf01..827292b 100644 Binary files a/SomfyController.ino.esp32.bin and b/SomfyController.ino.esp32.bin differ diff --git a/SomfyController.ino.esp32s3.bin b/SomfyController.ino.esp32s3.bin index e497aa8..b4c5890 100644 Binary files a/SomfyController.ino.esp32s3.bin and b/SomfyController.ino.esp32s3.bin differ diff --git a/Web.cpp b/Web.cpp index 10faf68..fdbc058 100644 --- a/Web.cpp +++ b/Web.cpp @@ -2171,7 +2171,8 @@ void Web::begin() { if(server.method() == HTTP_OPTIONS) { server.send(200, "OK"); return; } esp_task_wdt_delete(NULL); - int n = WiFi.scanNetworks(); + if(net.softAPOpened) WiFi.disconnect(false); + int n = WiFi.scanNetworks(false, true); esp_task_wdt_add(NULL); Serial.print("Scanned "); @@ -2377,21 +2378,28 @@ void Web::begin() { settings.save(); reboot = true; } - if(settings.connType == conn_types::wifi) { - if(obj.containsKey("ssid") && obj["ssid"].as().compareTo(settings.WIFI.ssid) != 0) reboot = true; - if(obj.containsKey("passphrase") && obj["passphrase"].as().compareTo(settings.WIFI.passphrase) != 0) reboot = true; + if(obj.containsKey("wifi")) { + JsonObject objWifi = obj["wifi"]; + if(settings.connType == conn_types::wifi) { + if(objWifi.containsKey("ssid") && objWifi["ssid"].as().compareTo(settings.WIFI.ssid) != 0) { + if(WiFi.softAPgetStationNum() == 0) reboot = true; + } + if(objWifi.containsKey("passphrase") && objWifi["passphrase"].as().compareTo(settings.WIFI.passphrase) != 0) { + if(WiFi.softAPgetStationNum() == 0) reboot = true; + } + } + settings.WIFI.fromJSON(objWifi); + settings.WIFI.save(); } - else { + if(obj.containsKey("ethernet")) + { + JsonObject objEth = obj["ethernet"]; // This is an ethernet connection so if anything changes we need to reboot. - reboot = true; + if(settings.connType == conn_types::ethernet || settings.connType == conn_types::ethernetpref) + reboot = true; + settings.Ethernet.fromJSON(objEth); + settings.Ethernet.save(); } - JsonObject objWifi = obj["wifi"]; - JsonObject objEth = obj["ethernet"]; - settings.WIFI.fromJSON(objWifi); - settings.Ethernet.fromJSON(objEth); - - settings.WIFI.save(); - settings.Ethernet.save(); if (reboot) { Serial.println("Rebooting ESP for new Network settings..."); rebootDelay.reboot = true;