Added additional checks for wifi dropoffs and disconnect from STA for AP scan.

This commit is contained in:
Robert Strouse 2024-06-02 10:58:51 -07:00
parent 2dc49a64e9
commit 473307b320
4 changed files with 31 additions and 16 deletions

View file

@ -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");

Binary file not shown.

Binary file not shown.

34
Web.cpp
View file

@ -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<String>().compareTo(settings.WIFI.ssid) != 0) reboot = true;
if(obj.containsKey("passphrase") && obj["passphrase"].as<String>().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<String>().compareTo(settings.WIFI.ssid) != 0) {
if(WiFi.softAPgetStationNum() == 0) reboot = true;
}
if(objWifi.containsKey("passphrase") && objWifi["passphrase"].as<String>().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;