Remove autoReconnect function from core #410 #407

This commit is contained in:
Robert Strouse 2024-07-10 21:40:55 -07:00
parent 0b985c0880
commit 75928b4ac8
7 changed files with 152 additions and 73 deletions

View file

@ -252,6 +252,7 @@ void GitRepo::toJSON(JsonResponse &json) {
#define ERR_DOWNLOAD_CONNECTION -42 #define ERR_DOWNLOAD_CONNECTION -42
void GitUpdater::loop() { void GitUpdater::loop() {
if(!net.connected()) return;
if(this->status == GIT_STATUS_READY) { if(this->status == GIT_STATUS_READY) {
if(settings.checkForUpdate && if(settings.checkForUpdate &&
(millis() > net.connectTime + 60000) && // Wait a minute before checking after connection. (millis() > net.connectTime + 60000) && // Wait a minute before checking after connection.

View file

@ -35,8 +35,11 @@ void MQTTClass::reset() {
this->connect(); this->connect();
} }
bool MQTTClass::loop() { bool MQTTClass::loop() {
if(settings.MQTT.enabled && !rebootDelay.reboot && !this->suspended && !mqttClient.connected()) if(settings.MQTT.enabled && !rebootDelay.reboot && !this->suspended && !mqttClient.connected()) {
this->connect(); esp_task_wdt_reset();
if(!net.connected()) this->connect();
}
esp_task_wdt_reset();
if(settings.MQTT.enabled) mqttClient.loop(); if(settings.MQTT.enabled) mqttClient.loop();
return true; return true;
} }

View file

@ -34,7 +34,9 @@ bool Network::setup() {
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
WiFi.persistent(false); WiFi.persistent(false);
WiFi.setAutoReconnect(false);
WiFi.onEvent(this->networkEvent); WiFi.onEvent(this->networkEvent);
this->disconnectTime = millis();
if(WiFi.status() == WL_CONNECTED) WiFi.disconnect(true, true); if(WiFi.status() == WL_CONNECTED) WiFi.disconnect(true, true);
if(settings.connType == conn_types_t::wifi || settings.connType == conn_types_t::unset) { if(settings.connType == conn_types_t::wifi || settings.connType == conn_types_t::unset) {
WiFi.persistent(false); WiFi.persistent(false);
@ -42,7 +44,6 @@ bool Network::setup() {
Serial.print("WiFi Mode: "); Serial.print("WiFi Mode: ");
Serial.println(WiFi.getMode()); Serial.println(WiFi.getMode());
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
//settings.WIFI.printNetworks();
} }
sockEmit.begin(); sockEmit.begin();
return true; return true;
@ -50,13 +51,14 @@ bool Network::setup() {
conn_types_t Network::preferredConnType() { conn_types_t Network::preferredConnType() {
switch(settings.connType) { switch(settings.connType) {
case conn_types_t::wifi: case conn_types_t::wifi:
return settings.WIFI.ssid[0] != '\0' ? conn_types_t::wifi : conn_types_t::ap;
case conn_types_t::unset: case conn_types_t::unset:
case conn_types_t::ap: case conn_types_t::ap:
return strlen(settings.WIFI.ssid) > 0 ? conn_types_t::wifi : conn_types_t::ap; return conn_types_t::ap;
case conn_types_t::ethernetpref: case conn_types_t::ethernetpref:
return strlen(settings.WIFI.ssid) > 0 && !ETH.linkUp() ? conn_types_t::wifi : conn_types_t::ethernet; return settings.WIFI.ssid[0] != '\0' && (!ETH.linkUp() && this->ethStarted) ? conn_types_t::wifi : conn_types_t::ethernet;
case conn_types_t::ethernet: case conn_types_t::ethernet:
return ETH.linkUp() ? conn_types_t::ethernet : conn_types_t::ap; return ETH.linkUp() || !this->ethStarted ? conn_types_t::ethernet : conn_types_t::ap;
default: default:
return settings.connType; return settings.connType;
} }
@ -77,51 +79,62 @@ void Network::loop() {
// b. WiFi: Perform synchronous scan for APs related to the SSID. If the SSID can be found then perform // b. WiFi: Perform synchronous scan for APs related to the SSID. If the SSID can be found then perform
// the connection process for the WiFi connection. // the connection process for the WiFi connection.
// c. SoftAP: This condition retains the Soft AP because no other connection method is available. // c. SoftAP: This condition retains the Soft AP because no other connection method is available.
this->connect(); // Connection timeout handled in connect function as well as the opening of the Soft AP if needed.
if(this->connecting()) return; // If we are currently attempting to connect to something then we need to bail here.
conn_types_t ctype = this->preferredConnType(); conn_types_t ctype = this->preferredConnType();
if(this->softAPOpened && WiFi.softAPgetStationNum() == 0) { this->connect(ctype); // Connection timeout handled in connect function as well as the opening of the Soft AP if needed.
// If the Soft AP is opened and there are no clients connected then we need to scan for an AP. If if(this->connecting()) return; // If we are currently attempting to connect to something then we need to bail here.
// our target exists we will exit out of the Soft AP and start that connection.
if(ctype == conn_types_t::wifi) {
// Scan for an AP.
if(!_apScanning && WiFi.scanNetworks(true, false, true, 300, 0, settings.WIFI.ssid) == -1) {
_apScanning = true;
}
}
}
else if(this->connected() && ctype == conn_types_t::wifi && settings.WIFI.roaming) {
// Periodically look for an AP.
if(millis() > SSID_SCAN_INTERVAL + this->lastWifiScan) {
//Serial.println("Started scan for access points");
if(!_apScanning && WiFi.scanNetworks(true, false, true, 300, 0, settings.WIFI.ssid) == -1) {
_apScanning = true;
this->lastWifiScan = millis();
}
}
}
if(_apScanning) { if(_apScanning) {
if(!settings.WIFI.roaming || settings.connType != conn_types_t::wifi || (this->softAPOpened && WiFi.softAPgetStationNum() != 0)) _apScanning = false; if((this->connected() && !settings.WIFI.roaming) || // We are already connected and should not be roaming.
(this->softAPOpened && WiFi.softAPgetStationNum() != 0) || // The Soft AP is open and a user is connected.
(ctype != conn_types_t::wifi)) { // The Ethernet link is up so we should ignore this scan.
Serial.println("Cancelling WiFi STA Scan...");
_apScanning = false;
WiFi.scanDelete();
}
else { else {
uint16_t n = WiFi.scanComplete(); int16_t n = WiFi.scanComplete();
if( n > 0) { if( n >= 0) { // If the scan is complete but the WiFi isn't ready this can return 0.
uint8_t bssid[6]; uint8_t bssid[6];
int32_t channel = 0; int32_t channel = 0;
if(this->getStrongestAP(settings.WIFI.ssid, bssid, &channel)) { if(this->getStrongestAP(settings.WIFI.ssid, bssid, &channel)) {
if(!WiFi.BSSID() || memcmp(bssid, WiFi.BSSID(), sizeof(bssid)) != 0) { if(!WiFi.BSSID() || memcmp(bssid, WiFi.BSSID(), sizeof(bssid)) != 0) {
Serial.printf("Found stronger AP %d %02X:%02X:%02X:%02X:%02X:%02X\n", channel, bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]); if(!this->connected()) {
if(this->softAPOpened) { Serial.printf("Connecting to AP %02X:%02X:%02X:%02X:%02X:%02X CH: %d\n", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], channel);
WiFi.softAPdisconnect(true); this->connectWiFi(bssid, channel);
WiFi.mode(WIFI_STA); }
else {
Serial.printf("Found stronger AP %02X:%02X:%02X:%02X:%02X:%02X CH: %d\n", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], channel);
this->changeAP(bssid, channel);
} }
this->changeAP(bssid, channel);
} }
} }
_apScanning = false; _apScanning = false;
} }
} }
} }
if(!this->connecting()) {
if((this->softAPOpened && WiFi.softAPgetStationNum() == 0) ||
(!this->connected() && ctype == conn_types_t::wifi)) {
// If the Soft AP is opened and there are no clients connected then we need to scan for an AP. If
// our target exists we will exit out of the Soft AP and start that connection. We are also
// going to continuously scan when there is no connection and our preferred connection is wifi.
if(ctype == conn_types_t::wifi) {
// Scan for an AP but only if we are not already scanning.
if(!_apScanning && WiFi.scanNetworks(true, false, true, 300, 0, settings.WIFI.ssid) == -1) {
_apScanning = true;
}
}
}
else if(this->connected() && ctype == conn_types_t::wifi && settings.WIFI.roaming) {
// Periodically look for a roaming AP.
if(millis() > SSID_SCAN_INTERVAL + this->lastWifiScan) {
//Serial.println("Started scan for access points");
if(!_apScanning && WiFi.scanNetworks(true, false, true, 300, 0, settings.WIFI.ssid) == -1) {
_apScanning = true;
this->lastWifiScan = millis();
}
}
}
}
if(millis() - this->lastEmit > 1500) { if(millis() - this->lastEmit > 1500) {
// Post our connection status if needed. // Post our connection status if needed.
this->lastEmit = millis(); this->lastEmit = millis();
@ -131,14 +144,14 @@ void Network::loop() {
} }
esp_task_wdt_reset(); // Make sure we do not reboot here. esp_task_wdt_reset(); // Make sure we do not reboot here.
} }
sockEmit.loop(); sockEmit.loop();
mqtt.loop(); mqtt.loop();
if(settings.ssdpBroadcast) { if(settings.ssdpBroadcast && this->connected()) {
if(!SSDP.isStarted) SSDP.begin(); if(!SSDP.isStarted) SSDP.begin();
if(SSDP.isStarted) SSDP.loop(); if(SSDP.isStarted) SSDP.loop();
} }
else if(!settings.ssdpBroadcast && SSDP.isStarted) SSDP.end(); else if(!settings.ssdpBroadcast && SSDP.isStarted) SSDP.end();
/* /*
// --------------------------- // ---------------------------
@ -215,6 +228,9 @@ bool Network::changeAP(const uint8_t *bssid, const int32_t channel) {
mqtt.disconnect(); mqtt.disconnect();
//sockEmit.end(); //sockEmit.end();
WiFi.disconnect(false, true); WiFi.disconnect(false, true);
this->connType = conn_types_t::unset;
this->_connecting = true;
this->connectStart = millis();
WiFi.begin(settings.WIFI.ssid, settings.WIFI.passphrase, channel, bssid); WiFi.begin(settings.WIFI.ssid, settings.WIFI.passphrase, channel, bssid);
this->connectStart = millis(); this->connectStart = millis();
return false; return false;
@ -299,9 +315,11 @@ void Network::emitSockets(uint8_t num) {
this->emitHeap(num); this->emitHeap(num);
} }
void Network::setConnected(conn_types_t connType) { void Network::setConnected(conn_types_t connType) {
esp_task_wdt_reset();
this->connType = connType; this->connType = connType;
this->connectTime = millis(); this->connectTime = millis();
connectRetries = 0; connectRetries = 0;
Serial.println("Setting connected...");
if(this->connType == conn_types_t::wifi) { if(this->connType == conn_types_t::wifi) {
if(this->softAPOpened && WiFi.softAPgetStationNum() == 0) { if(this->softAPOpened && WiFi.softAPgetStationNum() == 0) {
WiFi.softAPdisconnect(true); WiFi.softAPdisconnect(true);
@ -326,6 +344,8 @@ void Network::setConnected(conn_types_t connType) {
} }
// NET: Begin this in the startup. // NET: Begin this in the startup.
//sockEmit.begin(); //sockEmit.begin();
esp_task_wdt_reset();
if(this->connectAttempts == 1) { if(this->connectAttempts == 1) {
Serial.println(); Serial.println();
if(this->connType == conn_types_t::wifi) { if(this->connType == conn_types_t::wifi) {
@ -358,6 +378,7 @@ void Network::setConnected(conn_types_t connType) {
settings.IP.dns1 = ETH.dnsIP(0); settings.IP.dns1 = ETH.dnsIP(0);
settings.IP.dns2 = ETH.dnsIP(1); settings.IP.dns2 = ETH.dnsIP(1);
} }
esp_task_wdt_reset();
JsonSockEvent *json = sockEmit.beginEmit("ethernet"); JsonSockEvent *json = sockEmit.beginEmit("ethernet");
json->beginObject(); json->beginObject();
json->addElem("connected", this->connected()); json->addElem("connected", this->connected());
@ -365,6 +386,7 @@ void Network::setConnected(conn_types_t connType) {
json->addElem("fullduplex", ETH.fullDuplex()); json->addElem("fullduplex", ETH.fullDuplex());
json->endObject(); json->endObject();
sockEmit.endEmit(); sockEmit.endEmit();
esp_task_wdt_reset();
} }
} }
else { else {
@ -415,6 +437,7 @@ void Network::setConnected(conn_types_t connType) {
SSDP.setManufacturerURL(0, "https://github.com/rstrouse"); SSDP.setManufacturerURL(0, "https://github.com/rstrouse");
SSDP.setURL(0, "/"); SSDP.setURL(0, "/");
SSDP.setActive(0, true); SSDP.setActive(0, true);
esp_task_wdt_reset();
if(MDNS.begin(settings.hostname)) { if(MDNS.begin(settings.hostname)) {
Serial.printf("MDNS Responder Started: serverId=%s\n", settings.serverId); Serial.printf("MDNS Responder Started: serverId=%s\n", settings.serverId);
MDNS.addService("http", "tcp", 80); MDNS.addService("http", "tcp", 80);
@ -427,9 +450,11 @@ void Network::setConnected(conn_types_t connType) {
MDNS.addServiceTxt("espsomfy_rts", "tcp", "version", String(settings.fwVersion.name)); MDNS.addServiceTxt("espsomfy_rts", "tcp", "version", String(settings.fwVersion.name));
} }
if(settings.ssdpBroadcast) { if(settings.ssdpBroadcast) {
esp_task_wdt_reset();
SSDP.begin(); SSDP.begin();
} }
else if(SSDP.isStarted) SSDP.end(); else if(SSDP.isStarted) SSDP.end();
esp_task_wdt_reset();
this->emitSockets(); this->emitSockets();
settings.printAvailHeap(); settings.printAvailHeap();
this->needsBroadcast = true; this->needsBroadcast = true;
@ -509,7 +534,7 @@ void Network::updateHostname() {
} }
} }
} }
bool Network::connectWiFi() { bool Network::connectWiFi(const uint8_t *bssid, const int32_t channel) {
if(this->softAPOpened && WiFi.softAPgetStationNum() > 0) { if(this->softAPOpened && WiFi.softAPgetStationNum() > 0) {
// There is a client connected to the soft AP. We do not want to close out the connection. While both the // There is a client connected to the soft AP. We do not want to close out the connection. While both the
// Soft AP and a wifi connection can coexist on ESP32 the performance is abysmal. // Soft AP and a wifi connection can coexist on ESP32 the performance is abysmal.
@ -518,11 +543,34 @@ bool Network::connectWiFi() {
this->connType = conn_types_t::unset; this->connType = conn_types_t::unset;
return true; return true;
} }
WiFi.setSleep(false);
if(settings.WIFI.ssid[0] != '\0') { if(!settings.IP.dhcp) {
if(!WiFi.config(settings.IP.ip, settings.IP.gateway, settings.IP.subnet, settings.IP.dns1, settings.IP.dns2))
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
}
else
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
if(settings.hostname[0] != '\0') WiFi.setHostname(settings.hostname);
delay(100);
if(bssid && channel > 0) {
if(WiFi.status() == WL_CONNECTED && WiFi.SSID().compareTo(settings.WIFI.ssid) == 0
&& WiFi.channel() == channel) {
this->disconnected = 0;
return true;
}
this->connTarget = conn_types_t::wifi;
this->connType = conn_types_t::unset;
Serial.println("WiFi begin...");
this->_connecting = true;
WiFi.begin(settings.WIFI.ssid, settings.WIFI.passphrase, channel, bssid);
this->connectStart = millis();
}
else if(settings.WIFI.ssid[0] != '\0') {
if(WiFi.status() == WL_CONNECTED && WiFi.SSID().compareTo(settings.WIFI.ssid) == 0) { if(WiFi.status() == WL_CONNECTED && WiFi.SSID().compareTo(settings.WIFI.ssid) == 0) {
// If we are connected to the target SSID then just return. // If we are connected to the target SSID then just return.
this->disconnected = 0; this->disconnected = 0;
this->_connecting = true;
return true; return true;
} }
if(this->_connecting) return true; if(this->_connecting) return true;
@ -539,14 +587,6 @@ bool Network::connectWiFi() {
Serial.println("dbm) "); Serial.println("dbm) ");
} }
else Serial.println("Connecting to AP"); else Serial.println("Connecting to AP");
WiFi.setSleep(false);
WiFi.disconnect(false);
if(!settings.IP.dhcp) {
if(!WiFi.config(settings.IP.ip, settings.IP.gateway, settings.IP.subnet, settings.IP.dns1, settings.IP.dns2))
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
}
else
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
delay(100); delay(100);
// There is also another method simply called hostname() but this is legacy for esp8266. // There is also another method simply called hostname() but this is legacy for esp8266.
if(settings.hostname[0] != '\0') WiFi.setHostname(settings.hostname); if(settings.hostname[0] != '\0') WiFi.setHostname(settings.hostname);
@ -554,11 +594,11 @@ bool Network::connectWiFi() {
Serial.println(WiFi.getHostname()); Serial.println(WiFi.getHostname());
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
uint8_t bssid[6]; uint8_t _bssid[6];
int32_t channel = 0; int32_t _channel = 0;
if(this->getStrongestAP(settings.WIFI.ssid, bssid, &channel)) { if(this->getStrongestAP(settings.WIFI.ssid, _bssid, &_channel)) {
Serial.printf("Found strongest AP %d %02X:%02X:%02X:%02X:%02X:%02X\n", channel, bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]); Serial.printf("Found strongest AP %02X:%02X:%02X:%02X:%02X:%02X CH:%d\n", _bssid[0], _bssid[1], _bssid[2], _bssid[3], _bssid[4], _bssid[5], _channel);
WiFi.begin(settings.WIFI.ssid, settings.WIFI.passphrase, channel, bssid); WiFi.begin(settings.WIFI.ssid, settings.WIFI.passphrase, _channel, _bssid);
} }
else else
WiFi.begin(settings.WIFI.ssid, settings.WIFI.passphrase); WiFi.begin(settings.WIFI.ssid, settings.WIFI.passphrase);
@ -566,8 +606,30 @@ bool Network::connectWiFi() {
this->connectStart = millis(); this->connectStart = millis();
return true; return true;
} }
bool Network::connect() { bool Network::connect(conn_types_t ctype) {
esp_task_wdt_reset(); esp_task_wdt_reset();
if(this->connecting()) return true;
if(this->disconnectTime == 0) this->disconnectTime = millis();
if(ctype == conn_types_t::ethernet && this->connType != conn_types_t::ethernet) {
// Here we need to call the connect to ethernet.
this->connectWired();
}
else if(ctype == conn_types_t::ap || (!this->connected() && millis() > this->disconnectTime + CONNECT_TIMEOUT)) {
if(!this->softAPOpened && !this->openingSoftAP) {
this->disconnectTime = millis();
this->openSoftAP();
}
}
return true;
/*
if(this->connecting()) {
// If we are currently connecting it matters whether this is a wifi target or if it is an ethernet target. The preferred
// connection type make the determination for us.
}
if(this->connecting()) { if(this->connecting()) {
// CHECK FOR CONNECTION TIMEOUT // CHECK FOR CONNECTION TIMEOUT
// ------------------------------------- // -------------------------------------
@ -602,7 +664,7 @@ bool Network::connect() {
else if(this->softAPOpened) { else if(this->softAPOpened) {
// If the Soft AP is currently open then we will let the passive scanning or Ethernet link layer // If the Soft AP is currently open then we will let the passive scanning or Ethernet link layer
// do its thing to connect or reconnect. // do its thing to connect or reconnect.
return false; this->connType = conn_types_t::unset;
} }
else if(settings.connType == conn_types_t::ethernet || settings.connType == conn_types_t::ethernetpref) else if(settings.connType == conn_types_t::ethernet || settings.connType == conn_types_t::ethernetpref)
this->connectWired(); this->connectWired();
@ -617,7 +679,9 @@ bool Network::connect() {
WiFi.softAPdisconnect(true); WiFi.softAPdisconnect(true);
if(this->connType == conn_types_t::wifi) WiFi.mode(WIFI_STA); if(this->connType == conn_types_t::wifi) WiFi.mode(WIFI_STA);
} }
if(this->connecting() && millis() > this->connectStart + CONNECT_TIMEOUT + 100) this->_connecting = false;
return true; return true;
*/
} }
uint32_t Network::getChipId() { uint32_t Network::getChipId() {
uint32_t chipId = 0; uint32_t chipId = 0;
@ -633,9 +697,10 @@ bool Network::getStrongestAP(const char *ssid, uint8_t *bssid, int32_t *channel)
int32_t chan = -1; int32_t chan = -1;
memset(bssid, 0x00, 6); memset(bssid, 0x00, 6);
esp_task_wdt_delete(NULL); esp_task_wdt_delete(NULL);
uint8_t n = this->connected() ? WiFi.scanComplete() : WiFi.scanNetworks(false, false, false, 300, 0, ssid); int16_t n = WiFi.scanComplete();
//int16_t n = this->connected() ? WiFi.scanComplete() : WiFi.scanNetworks(false, false, false, 300, 0, ssid);
esp_task_wdt_add(NULL); esp_task_wdt_add(NULL);
for(uint8_t i = 0; i < n; i++) { for(int16_t i = 0; i < n; i++) {
if(WiFi.SSID(i).compareTo(ssid) == 0) { if(WiFi.SSID(i).compareTo(ssid) == 0) {
if(WiFi.RSSI(i) > strength) { if(WiFi.RSSI(i) > strength) {
strength = WiFi.RSSI(i); strength = WiFi.RSSI(i);
@ -649,13 +714,12 @@ bool Network::getStrongestAP(const char *ssid, uint8_t *bssid, int32_t *channel)
} }
bool Network::openSoftAP() { bool Network::openSoftAP() {
if(this->softAPOpened || this->openingSoftAP) return true; if(this->softAPOpened || this->openingSoftAP) return true;
WiFi.disconnect(false); if(this->connected()) WiFi.disconnect(false);
this->openingSoftAP = true; this->openingSoftAP = true;
Serial.println(); Serial.println();
Serial.println("Turning the HotSpot On"); Serial.println("Turning the HotSpot On");
esp_task_wdt_reset(); // Make sure we do not reboot here. esp_task_wdt_reset(); // Make sure we do not reboot here.
WiFi.softAP(strlen(settings.hostname) > 0 ? settings.hostname : "ESPSomfy RTS", ""); WiFi.softAP(strlen(settings.hostname) > 0 ? settings.hostname : "ESPSomfy RTS", "");
Serial.println("Initializing AP for credentials modification");
delay(200); delay(200);
return true; return true;
} }
@ -668,11 +732,13 @@ bool Network::connected() {
return false; return false;
} }
bool Network::connecting() { return this->_connecting; } bool Network::connecting() { return this->_connecting; }
void Network::clearConnecting() { this->_connecting = false; }
void Network::networkEvent(WiFiEvent_t event) { void Network::networkEvent(WiFiEvent_t event) {
switch(event) { switch(event) {
case ARDUINO_EVENT_WIFI_READY: Serial.println("(evt) WiFi interface ready"); break; case ARDUINO_EVENT_WIFI_READY: Serial.println("(evt) WiFi interface ready"); break;
case ARDUINO_EVENT_WIFI_SCAN_DONE: case ARDUINO_EVENT_WIFI_SCAN_DONE:
Serial.println("(evt) Completed scan for access points"); Serial.printf("(evt) Completed scan for access points (%d)\n", WiFi.scanComplete());
//Serial.println("(evt) Completed scan for access points");
net.lastWifiScan = millis(); net.lastWifiScan = millis();
break; break;
case ARDUINO_EVENT_WIFI_STA_START: case ARDUINO_EVENT_WIFI_STA_START:
@ -680,14 +746,20 @@ void Network::networkEvent(WiFiEvent_t event) {
if(settings.hostname[0] != '\0') WiFi.setHostname(settings.hostname); if(settings.hostname[0] != '\0') WiFi.setHostname(settings.hostname);
break; break;
case ARDUINO_EVENT_WIFI_STA_STOP: Serial.println("(evt) WiFi clients stopped"); break; case ARDUINO_EVENT_WIFI_STA_STOP: Serial.println("(evt) WiFi clients stopped"); break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED: Serial.println("(evt) Connected to access point"); break; case ARDUINO_EVENT_WIFI_STA_CONNECTED: Serial.println("(evt) Connected to WiFi STA access point"); break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: Serial.println("(evt) Disconnected from WiFi access point"); break; case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE: Serial.println("(evt) Authentication mode of access point has changed"); break; Serial.printf("(evt) Disconnected from WiFi STA access point. Connecting: %d\n", net.connecting());
net.connType = conn_types_t::unset;
net.disconnectTime = millis();
net.clearConnecting();
break;
case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE: Serial.println("(evt) Authentication mode of STA access point has changed"); break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP: case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.print("Got WiFi IP: "); Serial.print("(evt) Got WiFi STA IP: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
net.connType = conn_types_t::wifi; net.connType = conn_types_t::wifi;
net.connectTime = millis(); net.connectTime = millis();
net.setConnected(conn_types_t::wifi);
break; break;
case ARDUINO_EVENT_WIFI_STA_LOST_IP: Serial.println("Lost IP address and IP address is reset to 0"); break; case ARDUINO_EVENT_WIFI_STA_LOST_IP: Serial.println("Lost IP address and IP address is reset to 0"); break;
case ARDUINO_EVENT_ETH_GOT_IP: case ARDUINO_EVENT_ETH_GOT_IP:
@ -703,7 +775,8 @@ void Network::networkEvent(WiFiEvent_t event) {
settings.IP.gateway = ETH.gatewayIP(); settings.IP.gateway = ETH.gatewayIP();
settings.IP.dns1 = ETH.dnsIP(0); settings.IP.dns1 = ETH.dnsIP(0);
settings.IP.dns2 = ETH.dnsIP(1); settings.IP.dns2 = ETH.dnsIP(1);
} }
net.setConnected(conn_types_t::ethernet);
break; break;
case ARDUINO_EVENT_ETH_CONNECTED: case ARDUINO_EVENT_ETH_CONNECTED:
Serial.print("(evt) Ethernet Connected "); Serial.print("(evt) Ethernet Connected ");
@ -711,6 +784,8 @@ void Network::networkEvent(WiFiEvent_t event) {
case ARDUINO_EVENT_ETH_DISCONNECTED: case ARDUINO_EVENT_ETH_DISCONNECTED:
Serial.println("(evt) Ethernet Disconnected"); Serial.println("(evt) Ethernet Disconnected");
net.connType = conn_types_t::unset; net.connType = conn_types_t::unset;
net.disconnectTime = millis();
net.clearConnecting();
break; break;
case ARDUINO_EVENT_ETH_START: case ARDUINO_EVENT_ETH_START:
Serial.println("(evt) Ethernet Started"); Serial.println("(evt) Ethernet Started");
@ -729,7 +804,6 @@ void Network::networkEvent(WiFiEvent_t event) {
break; break;
case ARDUINO_EVENT_WIFI_AP_STOP: case ARDUINO_EVENT_WIFI_AP_STOP:
if(!net.openingSoftAP) Serial.println("(evt) WiFi SoftAP Stopped"); if(!net.openingSoftAP) Serial.println("(evt) WiFi SoftAP Stopped");
//if(net.softAPOpened) net.openingSoftAP = false;
net.softAPOpened = false; net.softAPOpened = false;
break; break;
default: default:

View file

@ -26,6 +26,7 @@ class Network {
conn_types_t connTarget = conn_types_t::unset; conn_types_t connTarget = conn_types_t::unset;
bool connected(); bool connected();
bool connecting(); bool connecting();
void clearConnecting();
conn_types_t preferredConnType(); conn_types_t preferredConnType();
String ssid; String ssid;
String mac; String mac;
@ -33,11 +34,12 @@ class Network {
int strength; int strength;
int disconnected = 0; int disconnected = 0;
int connectAttempts = 0; int connectAttempts = 0;
uint32_t disconnectTime = 0;
uint32_t connectStart = 0; uint32_t connectStart = 0;
uint32_t connectTime = 0; uint32_t connectTime = 0;
bool openSoftAP(); bool openSoftAP();
bool connect(); bool connect(conn_types_t ctype);
bool connectWiFi(); bool connectWiFi(const uint8_t *bssid = nullptr, const int32_t channel = -1);
bool connectWired(); bool connectWired();
void setConnected(conn_types_t connType); void setConnected(conn_types_t connType);
bool getStrongestAP(const char *ssid, uint8_t *bssid, int32_t *channel); bool getStrongestAP(const char *ssid, uint8_t *bssid, int32_t *channel);

View file

@ -161,7 +161,7 @@ void UPNPDeviceType::setChipId(uint32_t chipId) {
(uint16_t)chipId & 0xff); (uint16_t)chipId & 0xff);
} }
SSDPClass::SSDPClass():sendQueue{false, INADDR_NONE, 0, nullptr, false, 0, "", response_types_t::root} {} SSDPClass::SSDPClass():sendQueue{false, INADDR_NONE, 0, nullptr, false, 0, "", response_types_t::root} {}
SSDPClass::~SSDPClass() { end(); } SSDPClass::~SSDPClass() { end(); this->isStarted = false; }
bool SSDPClass::begin() { bool SSDPClass::begin() {
for(int i = 0; i < SSDP_QUEUE_SIZE; i++) { for(int i = 0; i < SSDP_QUEUE_SIZE; i++) {
this->sendQueue[i].waiting = false; this->sendQueue[i].waiting = false;
@ -209,6 +209,7 @@ void SSDPClass::end() {
if(this->_server.connected()) { if(this->_server.connected()) {
this->_sendByeBye(); this->_sendByeBye();
this->_server.close(); this->_server.close();
Serial.println("Disconnected from SSDP...");
} }
this->isStarted = false; this->isStarted = false;
// Clear out the last notified so if the user starts us up again it will notify // Clear out the last notified so if the user starts us up again it will notify
@ -216,8 +217,6 @@ void SSDPClass::end() {
for(uint8_t i = 0; i < this->m_cdeviceTypes; i++) { for(uint8_t i = 0; i < this->m_cdeviceTypes; i++) {
this->deviceTypes[i].lastNotified = 0; this->deviceTypes[i].lastNotified = 0;
} }
Serial.println("Disconnected from SSDP...");
} }
UPNPDeviceType* SSDPClass::getDeviceType(uint8_t ndx) { if(ndx < this->m_cdeviceTypes) return &this->deviceTypes[ndx]; return nullptr; } UPNPDeviceType* SSDPClass::getDeviceType(uint8_t ndx) { if(ndx < this->m_cdeviceTypes) return &this->deviceTypes[ndx]; return nullptr; }
UPNPDeviceType* SSDPClass::findDeviceByType(char *devType) { UPNPDeviceType* SSDPClass::findDeviceByType(char *devType) {

Binary file not shown.

Binary file not shown.