Skip to content

Commit 632dd39

Browse files
author
Robert Strouse
committed
Change the soft AP behavior so it does not start as fast on ethernet #271
1 parent 2a3d7aa commit 632dd39

File tree

6 files changed

+43
-16
lines changed

6 files changed

+43
-16
lines changed

Network.cpp

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <Arduino.h>
21
#include <ETH.h>
32
#include <WiFi.h>
43
#include <ESPmDNS.h>
@@ -40,18 +39,21 @@ bool Network::setup() {
4039
}
4140
void Network::loop() {
4241
if(millis() - this->lastEmit > 1500) {
43-
while(!this->connect()) {
44-
// If we lost our connenction
45-
connectRetries++;
46-
if(connectRetries > 100) {
47-
this->openSoftAP();
48-
break;
42+
this->lastEmit = millis();
43+
if(!this->softAPOpened) {
44+
while(!this->connect()) {
45+
// If we lost our connection
46+
connectRetries++;
47+
if(connectRetries > 100) {
48+
if(!this->connected()) this->openSoftAP();
49+
break;
50+
}
51+
sockEmit.loop();
4952
}
50-
sockEmit.loop();
53+
connectRetries = 0;
5154
}
52-
connectRetries = 0;
53-
this->lastEmit = millis();
5455
this->emitSockets();
56+
this->lastEmit = millis();
5557
if(!this->connected()) return;
5658
}
5759
if(this->connected() && millis() - this->lastMDNS > 60000) {
@@ -113,6 +115,21 @@ void Network::emitSockets(uint8_t num) {
113115
void Network::setConnected(conn_types connType) {
114116
this->connType = connType;
115117
this->connectTime = millis();
118+
connectRetries = 0;
119+
if(this->connType == conn_types::wifi) {
120+
if(this->softAPOpened) {
121+
WiFi.softAPdisconnect(true);
122+
WiFi.mode(WIFI_STA);
123+
}
124+
}
125+
else if(this->connType == conn_types::ethernet) {
126+
if(this->softAPOpened) {
127+
Serial.println("Disonnecting from SoftAP");
128+
WiFi.softAPdisconnect(true);
129+
WiFi.mode(WIFI_OFF);
130+
}
131+
this->wifiFallback = false;
132+
}
116133
if(this->connectAttempts == 1) {
117134
Serial.println();
118135
if(this->connType == conn_types::wifi) {
@@ -217,8 +234,10 @@ void Network::setConnected(conn_types connType) {
217234
settings.printAvailHeap();
218235
}
219236
bool Network::connectWired() {
220-
if(this->connType == conn_types::ethernet) {
237+
//if(this->connType == conn_types::ethernet && ETH.linkUp()) {
238+
if(ETH.linkUp()) {
221239
this->disconnected = 0;
240+
this->wifiFallback = false;
222241
return true;
223242
}
224243
if(this->connectAttempts > 0) {
@@ -254,7 +273,7 @@ bool Network::connectWired() {
254273
ETH.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
255274

256275
uint32_t wait = millis();
257-
while(millis() - wait < 7000) {
276+
while(millis() - wait < 14000) {
258277
if(this->connected()) return true;
259278
delay(500);
260279
}
@@ -377,8 +396,13 @@ bool Network::connectWiFi() {
377396
return false;
378397
}
379398
bool Network::connect() {
380-
if(settings.connType != conn_types::wifi && settings.connType != conn_types::unset && !this->wifiFallback)
381-
return this->connectWired();
399+
if(settings.connType == conn_types::unset) return true;
400+
else if(settings.connType == conn_types::ethernet || (settings.connType == conn_types::ethernetpref)) {
401+
bool bConnected = this->connectWired();
402+
if(!bConnected && settings.connType == conn_types::ethernetpref && settings.WIFI.ssid[0] != '\0')
403+
bConnected = this->connectWiFi();
404+
return bConnected;
405+
}
382406
return this->connectWiFi();
383407
}
384408
int Network::getStrengthByMac(const char *macAddr) {
@@ -444,7 +468,7 @@ bool Network::openSoftAP() {
444468
long startTime = millis();
445469
int c = 0;
446470

447-
while ((WiFi.status() != WL_CONNECTED))
471+
while (!this->connected())
448472
{
449473
int clients = WiFi.softAPgetStationNum();
450474
webServer.loop();
@@ -531,9 +555,11 @@ void Network::networkEvent(WiFiEvent_t event) {
531555
break;
532556
case ARDUINO_EVENT_WIFI_AP_STOP:
533557
Serial.println("WiFi AP Stopped");
558+
net.softAPOpened = false;
534559
break;
535560
case ARDUINO_EVENT_WIFI_AP_START:
536561
Serial.println("WiFi AP Started");
562+
net.softAPOpened = true;
537563
break;
538564
case ARDUINO_EVENT_WIFI_STA_START:
539565
if(settings.hostname[0] != '\0') WiFi.setHostname(settings.hostname);

Network.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Network {
1212
bool ethStarted = false;
1313
public:
1414
bool wifiFallback = false;
15+
bool softAPOpened = false;
1516
conn_types connType = conn_types::unset;
1617
bool connected();
1718
String ssid;

SomfyController.ino.esp32.bin

272 Bytes
Binary file not shown.

SomfyController.ino.esp32s3.bin

192 Bytes
Binary file not shown.

SomfyController.littlefs.bin

0 Bytes
Binary file not shown.

data/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2762,7 +2762,7 @@ class Somfy {
27622762
pinMaps = [
27632763
{ 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] },
27642764
{ 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]},
2765-
{ 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] },
2765+
{ 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] },
27662766
{ name: 'c3', maxPins: 21, inputs: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], outputs: [11, 12, 13, 14, 15, 16, 17, 21] }
27672767
];
27682768

0 commit comments

Comments
 (0)