Skip to content

Commit 473307b

Browse files
author
Robert Strouse
committed
Added additional checks for wifi dropoffs and disconnect from STA for AP scan.
1 parent 2dc49a6 commit 473307b

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

Network.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#include "SSDP.h"
1111
#include "MQTT.h"
1212

13-
int testFallback = 5;
14-
1513
extern ConfigSettings settings;
1614
extern Web webServer;
1715
extern SocketEmitter sockEmit;
@@ -397,13 +395,20 @@ void Network::updateHostname() {
397395
}
398396
}
399397
bool Network::connectWiFi() {
398+
if(this->softAPOpened && WiFi.softAPgetStationNum() > 0) {
399+
WiFi.disconnect(false);
400+
this->_connecting = false;
401+
this->connType = conn_types::unset;
402+
return true;
403+
}
404+
400405
if(settings.WIFI.ssid[0] != '\0') {
401406
if(WiFi.status() == WL_CONNECTED && WiFi.SSID().compareTo(settings.WIFI.ssid) == 0) {
402407
// If we are connected to the target SSID then just return.
403408
this->disconnected = 0;
404409
return true;
405410
}
406-
if(this->softAPOpened) Serial.printf("connectWiFi: %d\n", WiFi.softAPgetStationNum());
411+
if(this->_connecting) return true;
407412
this->_connecting = true;
408413
this->connTarget = conn_types::wifi;
409414
this->connType = conn_types::unset;
@@ -419,6 +424,7 @@ bool Network::connectWiFi() {
419424
else Serial.println("Connecting to AP");
420425
// If the soft AP is currently opened then we do not want to kill it.
421426
WiFi.setSleep(false);
427+
WiFi.disconnect(false);
422428
//WiFi.mode(WIFI_MODE_NULL);
423429
if(!settings.IP.dhcp) {
424430
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)
517523
}
518524
bool Network::openSoftAP() {
519525
if(this->softAPOpened || this->openingSoftAP) return true;
526+
WiFi.disconnect(false);
520527
this->openingSoftAP = true;
521528
Serial.println();
522529
Serial.println("Turning the HotSpot On");

SomfyController.ino.esp32.bin

128 Bytes
Binary file not shown.

SomfyController.ino.esp32s3.bin

128 Bytes
Binary file not shown.

Web.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,8 @@ void Web::begin() {
21712171

21722172
if(server.method() == HTTP_OPTIONS) { server.send(200, "OK"); return; }
21732173
esp_task_wdt_delete(NULL);
2174-
int n = WiFi.scanNetworks();
2174+
if(net.softAPOpened) WiFi.disconnect(false);
2175+
int n = WiFi.scanNetworks(false, true);
21752176
esp_task_wdt_add(NULL);
21762177

21772178
Serial.print("Scanned ");
@@ -2377,21 +2378,28 @@ void Web::begin() {
23772378
settings.save();
23782379
reboot = true;
23792380
}
2380-
if(settings.connType == conn_types::wifi) {
2381-
if(obj.containsKey("ssid") && obj["ssid"].as<String>().compareTo(settings.WIFI.ssid) != 0) reboot = true;
2382-
if(obj.containsKey("passphrase") && obj["passphrase"].as<String>().compareTo(settings.WIFI.passphrase) != 0) reboot = true;
2381+
if(obj.containsKey("wifi")) {
2382+
JsonObject objWifi = obj["wifi"];
2383+
if(settings.connType == conn_types::wifi) {
2384+
if(objWifi.containsKey("ssid") && objWifi["ssid"].as<String>().compareTo(settings.WIFI.ssid) != 0) {
2385+
if(WiFi.softAPgetStationNum() == 0) reboot = true;
2386+
}
2387+
if(objWifi.containsKey("passphrase") && objWifi["passphrase"].as<String>().compareTo(settings.WIFI.passphrase) != 0) {
2388+
if(WiFi.softAPgetStationNum() == 0) reboot = true;
2389+
}
2390+
}
2391+
settings.WIFI.fromJSON(objWifi);
2392+
settings.WIFI.save();
23832393
}
2384-
else {
2394+
if(obj.containsKey("ethernet"))
2395+
{
2396+
JsonObject objEth = obj["ethernet"];
23852397
// This is an ethernet connection so if anything changes we need to reboot.
2386-
reboot = true;
2398+
if(settings.connType == conn_types::ethernet || settings.connType == conn_types::ethernetpref)
2399+
reboot = true;
2400+
settings.Ethernet.fromJSON(objEth);
2401+
settings.Ethernet.save();
23872402
}
2388-
JsonObject objWifi = obj["wifi"];
2389-
JsonObject objEth = obj["ethernet"];
2390-
settings.WIFI.fromJSON(objWifi);
2391-
settings.Ethernet.fromJSON(objEth);
2392-
2393-
settings.WIFI.save();
2394-
settings.Ethernet.save();
23952403
if (reboot) {
23962404
Serial.println("Rebooting ESP for new Network settings...");
23972405
rebootDelay.reboot = true;

0 commit comments

Comments
 (0)