Change the soft AP behavior so it does not start as fast on ethernet #271

This commit is contained in:
Robert Strouse 2024-02-19 17:21:41 -08:00
parent 2a3d7aa7e7
commit 632dd3900b
6 changed files with 44 additions and 17 deletions

View file

@ -1,4 +1,3 @@
#include <Arduino.h>
#include <ETH.h>
#include <WiFi.h>
#include <ESPmDNS.h>
@ -40,18 +39,21 @@ bool Network::setup() {
}
void Network::loop() {
if(millis() - this->lastEmit > 1500) {
while(!this->connect()) {
// If we lost our connenction
connectRetries++;
if(connectRetries > 100) {
this->openSoftAP();
break;
}
sockEmit.loop();
}
connectRetries = 0;
this->lastEmit = millis();
if(!this->softAPOpened) {
while(!this->connect()) {
// If we lost our connection
connectRetries++;
if(connectRetries > 100) {
if(!this->connected()) this->openSoftAP();
break;
}
sockEmit.loop();
}
connectRetries = 0;
}
this->emitSockets();
this->lastEmit = millis();
if(!this->connected()) return;
}
if(this->connected() && millis() - this->lastMDNS > 60000) {
@ -113,6 +115,21 @@ void Network::emitSockets(uint8_t num) {
void Network::setConnected(conn_types connType) {
this->connType = connType;
this->connectTime = millis();
connectRetries = 0;
if(this->connType == conn_types::wifi) {
if(this->softAPOpened) {
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_STA);
}
}
else if(this->connType == conn_types::ethernet) {
if(this->softAPOpened) {
Serial.println("Disonnecting from SoftAP");
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_OFF);
}
this->wifiFallback = false;
}
if(this->connectAttempts == 1) {
Serial.println();
if(this->connType == conn_types::wifi) {
@ -217,8 +234,10 @@ void Network::setConnected(conn_types connType) {
settings.printAvailHeap();
}
bool Network::connectWired() {
if(this->connType == conn_types::ethernet) {
//if(this->connType == conn_types::ethernet && ETH.linkUp()) {
if(ETH.linkUp()) {
this->disconnected = 0;
this->wifiFallback = false;
return true;
}
if(this->connectAttempts > 0) {
@ -254,7 +273,7 @@ bool Network::connectWired() {
ETH.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
uint32_t wait = millis();
while(millis() - wait < 7000) {
while(millis() - wait < 14000) {
if(this->connected()) return true;
delay(500);
}
@ -377,8 +396,13 @@ bool Network::connectWiFi() {
return false;
}
bool Network::connect() {
if(settings.connType != conn_types::wifi && settings.connType != conn_types::unset && !this->wifiFallback)
return this->connectWired();
if(settings.connType == conn_types::unset) return true;
else if(settings.connType == conn_types::ethernet || (settings.connType == conn_types::ethernetpref)) {
bool bConnected = this->connectWired();
if(!bConnected && settings.connType == conn_types::ethernetpref && settings.WIFI.ssid[0] != '\0')
bConnected = this->connectWiFi();
return bConnected;
}
return this->connectWiFi();
}
int Network::getStrengthByMac(const char *macAddr) {
@ -444,7 +468,7 @@ bool Network::openSoftAP() {
long startTime = millis();
int c = 0;
while ((WiFi.status() != WL_CONNECTED))
while (!this->connected())
{
int clients = WiFi.softAPgetStationNum();
webServer.loop();
@ -531,9 +555,11 @@ void Network::networkEvent(WiFiEvent_t event) {
break;
case ARDUINO_EVENT_WIFI_AP_STOP:
Serial.println("WiFi AP Stopped");
net.softAPOpened = false;
break;
case ARDUINO_EVENT_WIFI_AP_START:
Serial.println("WiFi AP Started");
net.softAPOpened = true;
break;
case ARDUINO_EVENT_WIFI_STA_START:
if(settings.hostname[0] != '\0') WiFi.setHostname(settings.hostname);

View file

@ -12,6 +12,7 @@ class Network {
bool ethStarted = false;
public:
bool wifiFallback = false;
bool softAPOpened = false;
conn_types connType = conn_types::unset;
bool connected();
String ssid;

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -2762,7 +2762,7 @@ class Somfy {
pinMaps = [
{ name: '', maxPins: 39, inputs: [0, 1, 6, 7, 8, 9, 10, 11, 37, 38], outputs: [3, 6, 7, 8, 9, 10, 11, 34, 35, 36, 37, 38, 39] },
{ name: 's2', maxPins: 46, inputs: [0, 15, 16, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 45], outputs: [0, 15, 16, 19, 20, 26, 27, 28, 29, 30, 31, 32, 45, 46]},
{ name: 's3', maxPins: 48, inputs: [0, 3, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 39, 40, 41, 42, 43, 44], outputs: [0, 3, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 39, 40, 41, 42, 43, 44] },
{ name: 's3', maxPins: 48, inputs: [0, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 39, 40, 41, 42, 43], outputs: [0, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 39, 40, 41, 42, 43] },
{ name: 'c3', maxPins: 21, inputs: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], outputs: [11, 12, 13, 14, 15, 16, 17, 21] }
];