1
-
1
+ # define WIFI_RECONNECT_WAIT 20000 // in milliSeconds
2
2
#define WIFI_AP_OFF_TIMER_DURATION 60000 // in milliSeconds
3
3
4
4
bool unprocessedWifiEvents () {
@@ -12,10 +12,9 @@ bool unprocessedWifiEvents() {
12
12
// ********************************************************************************
13
13
void processConnect () {
14
14
if (processedConnect) return ;
15
- processedConnect = true ;
16
15
delay (100 ); // FIXME TD-er: See https://github.com/letscontrolit/ESPEasy/issues/1987#issuecomment-451644424
17
16
++wifi_reconnects;
18
- if (wifiStatus < ESPEASY_WIFI_CONNECTED) return ;
17
+ if (( wifiStatus & ESPEASY_WIFI_CONNECTED) == 0 ) return ;
19
18
const long connect_duration = timeDiff (last_wifi_connect_attempt_moment, lastConnectMoment);
20
19
if (loglevelActiveFor (LOG_LEVEL_INFO)) {
21
20
String log = F (" WIFI : Connected! AP: " );
@@ -44,11 +43,11 @@ void processConnect() {
44
43
WiFi.setAutoConnect (true );
45
44
}
46
45
logConnectionStatus ();
46
+ processedConnect = true ;
47
47
}
48
48
49
49
void processDisconnect () {
50
50
if (processedDisconnect) return ;
51
- processedDisconnect = true ;
52
51
delay (100 ); // FIXME TD-er: See https://github.com/letscontrolit/ESPEasy/issues/1987#issuecomment-451644424
53
52
if (Settings.UseRules ) {
54
53
String event = F (" WiFi#Disconnected" );
@@ -64,11 +63,15 @@ void processDisconnect() {
64
63
}
65
64
addLog (LOG_LEVEL_INFO, log );
66
65
}
66
+ WiFiUDP::stopAll ();
67
+ WiFiClient::stopAll ();
68
+ mqtt_reconnect_count = 0 ;
67
69
if (Settings.WiFiRestart_connection_lost ()) {
68
- setWifiMode (WIFI_OFF );
69
- delay ( 100 );
70
+ WifiDisconnect ( );
71
+ // setWifiMode(WIFI_OFF );
70
72
}
71
73
logConnectionStatus ();
74
+ processedDisconnect = true ;
72
75
}
73
76
74
77
@@ -316,9 +319,9 @@ void WifiScanAsync() {
316
319
bool WifiIsAP (WiFiMode_t wifimode)
317
320
{
318
321
#if defined(ESP32)
319
- return (wifimode == WIFI_MODE_AP) || (wifimode == WIFI_MODE_APSTA );
322
+ return (( wifimode & WIFI_MODE_AP) != 0 );
320
323
#else
321
- return (wifimode == WIFI_AP) || (wifimode == WIFI_AP_STA );
324
+ return (( wifimode & WIFI_AP) != 0 );
322
325
#endif
323
326
}
324
327
@@ -356,6 +359,9 @@ void setSTA(bool enable) {
356
359
357
360
void setAP (bool enable) {
358
361
WiFiMode_t wifimode = WiFi.getMode ();
362
+ if (WifiIsAP (wifimode) == enable) {
363
+ return ;
364
+ }
359
365
switch (wifimode) {
360
366
case WIFI_OFF:
361
367
if (enable) setWifiMode (WIFI_AP);
@@ -370,23 +376,15 @@ void setAP(bool enable) {
370
376
if (!enable) setWifiMode (WIFI_STA);
371
377
break ;
372
378
default :
379
+ addLog (LOG_LEVEL_INFO, F (" WIFI : Unknown mode (setAP)" ));
373
380
break ;
374
381
}
375
- if (WifiIsAP (wifimode) && !enable) {
376
- String event = F (" WiFi#APmodeDisabled" );
377
- rulesProcessing (event);
378
- }
379
- if (WifiIsAP (wifimode) != enable) {
380
- // Mode has changed
381
- setAPinternal (enable);
382
- }
383
382
}
384
383
385
384
// Only internal scope
386
385
void setAPinternal (bool enable)
387
386
{
388
387
if (enable) {
389
- timerAPoff = millis () + WIFI_AP_OFF_TIMER_DURATION;
390
388
// create and store unique AP SSID/PW to prevent ESP from starting AP mode with default SSID and No password!
391
389
// setup ssid for AP Mode when needed
392
390
String softAPSSID=WifiGetAPssid ();
@@ -434,9 +432,14 @@ void setAPinternal(bool enable)
434
432
435
433
436
434
void setWifiMode (WiFiMode_t wifimode) {
437
- if (WiFi.getMode () == wifimode) {
435
+ const WiFiMode_t cur_mode = WiFi.getMode ();
436
+ if (cur_mode == wifimode) {
438
437
return ;
439
438
}
439
+ String log = F (" WIFI : Current mode " );
440
+ log += String (cur_mode);
441
+ addLog (LOG_LEVEL_INFO, log );
442
+
440
443
switch (wifimode) {
441
444
case WIFI_OFF:
442
445
addLog (LOG_LEVEL_INFO, F (" WIFI : Switch off WiFi" ));
@@ -451,11 +454,23 @@ void setWifiMode(WiFiMode_t wifimode) {
451
454
addLog (LOG_LEVEL_INFO, F (" WIFI : Set WiFi to AP+STA" ));
452
455
break ;
453
456
default :
457
+ addLog (LOG_LEVEL_INFO, F (" WIFI : Unknown mode" ));
454
458
break ;
455
459
}
456
- setUseStaticIP (useStaticIP ());
457
- WiFi.mode (wifimode);
460
+ if (!WiFi.mode (wifimode)) {
461
+ addLog (LOG_LEVEL_INFO, F (" WIFI : Cannot set mode!!!!!" ));
462
+ }
463
+ setupStaticIPconfig ();
458
464
delay (30 ); // Must allow for some time to init.
465
+ bool new_mode_AP_enabled = WifiIsAP (wifimode);
466
+ if (WifiIsAP (cur_mode) && !new_mode_AP_enabled) {
467
+ String event = F (" WiFi#APmodeDisabled" );
468
+ rulesProcessing (event);
469
+ }
470
+ if (WifiIsAP (cur_mode) != new_mode_AP_enabled) {
471
+ // Mode has changed
472
+ setAPinternal (new_mode_AP_enabled);
473
+ }
459
474
}
460
475
461
476
@@ -493,21 +508,30 @@ bool WiFiConnected() {
493
508
// For ESP82xx, do not rely on WiFi.status() with event based wifi.
494
509
if (wifiStatus == ESPEASY_WIFI_SERVICES_INITIALIZED) {
495
510
if (WiFi.RSSI () < 0 && WiFi.isConnected ()) {
511
+ timerAPstart = 0 ;
512
+ processDisableAPmode ();
496
513
STOP_TIMER (WIFI_ISCONNECTED_STATS);
497
514
return true ;
498
515
}
499
516
// else wifiStatus is no longer in sync.
500
517
addLog (LOG_LEVEL_INFO, F (" WIFI : WiFiConnected() out of sync" ));
501
518
resetWiFi ();
502
519
}
520
+ if (timerAPstart == 0 ) {
521
+ timerAPstart = millis () + WIFI_RECONNECT_WAIT;
522
+ }
523
+ if (timeOutReached (timerAPstart)) {
524
+ setAP (true );
525
+ }
503
526
delay (1 );
504
527
STOP_TIMER (WIFI_NOTCONNECTED_STATS);
505
528
return false ;
506
529
}
507
530
508
531
void WiFiConnectRelaxed () {
509
- if (WiFiConnected ())
532
+ if (WiFiConnected ()) {
510
533
return ; // already connected, need to disconnect first
534
+ }
511
535
if (prepareWiFi ()) {
512
536
if (selectValidWiFiSettings ()) {
513
537
tryConnectWiFi ();
@@ -633,7 +657,12 @@ void setConnectionSpeed() {
633
657
634
658
void setupStaticIPconfig () {
635
659
setUseStaticIP (useStaticIP ());
636
- if (!useStaticIP ()) return ;
660
+ if (!useStaticIP ()) {
661
+ // For newer core versions, simply setting to 0's will disable DHCPc
662
+ uint32_t zero_ip = 0u ;
663
+ WiFi.config (zero_ip, zero_ip, zero_ip, zero_ip);
664
+ return ;
665
+ }
637
666
const IPAddress ip = Settings.IP ;
638
667
const IPAddress gw = Settings.Gateway ;
639
668
const IPAddress subnet = Settings.Subnet ;
@@ -702,6 +731,7 @@ bool tryConnectWiFi() {
702
731
default :
703
732
WiFi.begin (ssid, passphrase);
704
733
}
734
+ delay (100 ); // FIXME TD-er: Not sure why, but a delay is really needed here or else you will run into timeouts.
705
735
++wifi_connect_attempt;
706
736
logConnectionStatus ();
707
737
switch (WiFi.status ()) {
@@ -903,8 +933,8 @@ void WifiCheck()
903
933
{
904
934
if (wifiSetup)
905
935
return ;
906
-
907
- processDisableAPmode ();
936
+ delay ( 0 );
937
+ // processDisableAPmode();
908
938
IPAddress ip = WiFi.localIP ();
909
939
if (WiFiConnected ()) { // let's just check for wifi, no matter if we have an IP or not...
910
940
if (!useStaticIP ()) {
@@ -921,11 +951,25 @@ void WifiCheck()
921
951
}
922
952
}
923
953
924
- if (wifiStatus != ESPEASY_WIFI_SERVICES_INITIALIZED) {
925
- if (timeOutReached (last_wifi_connect_attempt_moment + (1000 + wifi_connect_attempt * 200 ))) {
926
- WiFiConnectRelaxed ();
927
- }
954
+ if (WiFi.status () == WL_DISCONNECTED && timePassedSince (last_wifi_connect_attempt_moment) > 5000 ) {
955
+ WifiDisconnect ();
928
956
}
957
+
958
+ switch (wifiStatus) {
959
+ case ESPEASY_WIFI_SERVICES_INITIALIZED:
960
+ break ;
961
+ case ESPEASY_WIFI_DISCONNECTED:
962
+ if (lastDisconnectMoment == 0 || timeOutReached (lastDisconnectMoment + 500 )) {
963
+ WiFiConnectRelaxed ();
964
+ }
965
+ break ;
966
+ default :
967
+ if (timeOutReached (last_wifi_connect_attempt_moment + (10000 + wifi_connect_attempt * 200 ))) {
968
+ WifiDisconnect ();
969
+ }
970
+ break ;
971
+ }
972
+
929
973
if (mqtt_reconnect_count > 10 ) {
930
974
connectionCheckHandler ();
931
975
}
0 commit comments