Skip to content

Commit 0e33dc1

Browse files
playmielalcantemathieucarbou
authored
Control the delay before ESP restart in auto-restart mode and fix IPv6 support (#42)
* [1] -add delay before restart -stop ap after send response to user wait for restart is good * [1]v2 -add a modifiable delay before restart and readout of delay and remaining time -add two callbacks before restart * little correction * correction: add setter, and removing what is unnecessary * little correction * Some code cleanup and fixes * Fix IPv6 support and concurrency issue when restart mode is disabled --------- Co-authored-by: alcante <[email protected]> Co-authored-by: Mathieu Carbou <[email protected]>
1 parent f692a41 commit 0e33dc1

File tree

4 files changed

+64
-14
lines changed

4 files changed

+64
-14
lines changed

examples/NonBlockingCaptivePortal/NonBlockingCaptivePortal.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ void setup() {
5858
}
5959
});
6060

61-
espConnect.setAutoRestart(true);
61+
espConnect.setConnectTimeout(20); // 20 seconds
62+
espConnect.setCaptivePortalTimeout(30); // 30 seconds
63+
espConnect.setAutoRestart(false);
6264
espConnect.setBlocking(false);
6365

6466
Serial.println("====> Trying to connect to saved WiFi or will start portal in the background...");

platformio.ini

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
default_envs = arduino-3, arduino-3-latest, esp8266, esp32-poe-arduino-3, esp32-poe-arduino-3-latest, wt32-eth01-arduino-3, wt32-eth01-arduino-3-latest, lilygo-eth-lite-s3-arduino-3, lilygo-eth-lite-s3-arduino-3-latest
44
lib_dir = .
55
; src_dir = examples/BlockingCaptivePortal
6-
; src_dir = examples/NonBlockingCaptivePortal
6+
src_dir = examples/NonBlockingCaptivePortal
77
; src_dir = examples/AdvancedCaptivePortal
88
; src_dir = examples/TestWiFi8266
99
; src_dir = examples/WiFiStaticIP
10-
src_dir = examples/LoadSaveConfig
10+
; src_dir = examples/LoadSaveConfig
1111
; src_dir = examples/NoCaptivePortal
1212

1313
[env]
@@ -21,6 +21,8 @@ build_flags =
2121
; -D ESPCONNECT_NO_CAPTIVE_PORTAL
2222
; -D ESPCONNECT_NO_STD_STRING
2323
; -D ESPCONNECT_NO_LOGGING
24+
; -D ESPCONNECT_ETH_SUPPORT
25+
; -D ESPCONNECT_NO_MUTEX
2426
lib_compat_mode = strict
2527
lib_ldf_mode = chain
2628
lib_deps =

src/MycilaESPConnect.cpp

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ void Mycila::ESPConnect::loop() {
345345
if (_dnsServer != nullptr)
346346
_dnsServer->processNextRequest();
347347

348+
#ifndef ESPCONNECT_NO_MUTEX
349+
std::lock_guard<std::mutex> lock(_mutex);
350+
#endif
351+
348352
// first check if we have to enter AP mode
349353
if (_state == Mycila::ESPConnect::State::NETWORK_ENABLED && _config.apMode) {
350354
_startAP();
@@ -396,13 +400,34 @@ void Mycila::ESPConnect::loop() {
396400
_disableCaptivePortal();
397401
}
398402

399-
if (_state == Mycila::ESPConnect::State::PORTAL_COMPLETE || _state == Mycila::ESPConnect::State::PORTAL_TIMEOUT) {
403+
if (_state == Mycila::ESPConnect::State::PORTAL_TIMEOUT) {
404+
LOGW(TAG, "Portal timeout!");
400405
_stopAP();
401406
if (_autoRestart) {
402-
LOGW(TAG, "Auto Restart of ESP...");
407+
LOGW(TAG, "Restarting ESP...");
403408
ESP.restart();
404-
} else
409+
} else {
410+
// try to reconnect again with configured settings
405411
_setState(Mycila::ESPConnect::State::NETWORK_ENABLED);
412+
}
413+
}
414+
415+
if (_state == Mycila::ESPConnect::State::PORTAL_COMPLETE) {
416+
if (_autoRestart) {
417+
if (!_restartRequestTime) {
418+
// init _restartRequestTime if not already set to teh time when the portal completed
419+
_restartRequestTime = millis();
420+
} else if (millis() - _restartRequestTime >= _restartDelay) {
421+
// delay is over restart
422+
LOGW(TAG, "Auto Restart of ESP...");
423+
_stopAP();
424+
ESP.restart();
425+
}
426+
} else {
427+
_stopAP();
428+
// try to reconnect again with new configured settings
429+
_setState(Mycila::ESPConnect::State::NETWORK_ENABLED);
430+
}
406431
}
407432
}
408433

@@ -804,6 +829,10 @@ void Mycila::ESPConnect::_onWiFiEvent(WiFiEvent_t event) {
804829
if (_state == Mycila::ESPConnect::State::NETWORK_DISABLED)
805830
return;
806831

832+
#ifndef ESPCONNECT_NO_MUTEX
833+
std::lock_guard<std::mutex> lock(_mutex);
834+
#endif
835+
807836
switch (event) {
808837
#ifdef ESPCONNECT_ETH_SUPPORT
809838
case ARDUINO_EVENT_ETH_START:
@@ -836,21 +865,23 @@ void Mycila::ESPConnect::_onWiFiEvent(WiFiEvent_t event) {
836865

837866
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
838867
LOGD(TAG, "[%s] WiFiEvent: ARDUINO_EVENT_WIFI_STA_GOT_IP: %s", getStateName(), WiFi.localIP().toString().c_str());
839-
#ifndef ESPCONNECT_NO_MDNS
840-
MDNS.begin(_config.hostname.c_str());
841-
#endif
842868
if (_state == Mycila::ESPConnect::State::NETWORK_CONNECTING || _state == Mycila::ESPConnect::State::NETWORK_RECONNECTING) {
843869
_lastTime = -1;
844870
_setState(Mycila::ESPConnect::State::NETWORK_CONNECTED);
845871
}
872+
#ifndef ESPCONNECT_NO_MDNS
873+
MDNS.begin(_config.hostname.c_str());
874+
#endif
846875
break;
847876

848877
#ifndef ESP8266
849878
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
850-
LOGD(TAG, "[%s] WiFiEvent: ARDUINO_EVENT_WIFI_STA_GOT_IP6: %s", getStateName(), WiFi.globalIPv6().toString().c_str());
851-
if (_state == Mycila::ESPConnect::State::NETWORK_CONNECTING || _state == Mycila::ESPConnect::State::NETWORK_RECONNECTING) {
852-
_lastTime = -1;
853-
_setState(Mycila::ESPConnect::State::NETWORK_CONNECTED);
879+
if (WiFi.globalIPv6() != IN6ADDR_ANY) {
880+
LOGD(TAG, "[%s] WiFiEvent: ARDUINO_EVENT_WIFI_STA_GOT_IP6: %s", getStateName(), WiFi.globalIPv6().toString().c_str());
881+
if (_state == Mycila::ESPConnect::State::NETWORK_CONNECTING || _state == Mycila::ESPConnect::State::NETWORK_RECONNECTING) {
882+
_lastTime = -1;
883+
_setState(Mycila::ESPConnect::State::NETWORK_CONNECTED);
884+
}
854885
}
855886
break;
856887
#endif
@@ -867,7 +898,7 @@ void Mycila::ESPConnect::_onWiFiEvent(WiFiEvent_t event) {
867898
// we have to move to state disconnected only if we are not connected to ethernet
868899
#ifdef ESPCONNECT_ETH_SUPPORT
869900
if (ETH.linkUp() && ETH.localIP()[0] != 0)
870-
return;
901+
break;
871902
#endif
872903
_setState(Mycila::ESPConnect::State::NETWORK_DISCONNECTED);
873904
}

src/MycilaESPConnect.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <DNSServer.h>
88

99
#ifdef ESP8266
10+
#define ESPCONNECT_NO_MUTEX 1
1011
#include <ESP8266WiFi.h>
1112
#else
1213
#include <WiFi.h>
@@ -26,6 +27,10 @@
2627
#define ESPCONNECT_STRING std::string
2728
#endif
2829

30+
#ifndef ESPCONNECT_NO_MUTEX
31+
#include <mutex>
32+
#endif
33+
2934
#define ESPCONNECT_VERSION "10.0.1"
3035
#define ESPCONNECT_VERSION_MAJOR 10
3136
#define ESPCONNECT_VERSION_MINOR 0
@@ -223,6 +228,11 @@ namespace Mycila {
223228
// when using auto-load and save of configuration, this method can clear saved states.
224229
void clearConfiguration();
225230

231+
// Get the delay before a restart occurs
232+
uint32_t getRestartDelay() const { return _restartDelay; }
233+
// Set the delay before a restart occurs in ms
234+
void setRestartDelay(uint32_t delay) { _restartDelay = delay; }
235+
226236
#ifndef ESPCONNECT_NO_CAPTIVE_PORTAL
227237
void toJson(const JsonObject& root) const;
228238
#endif
@@ -270,5 +280,10 @@ namespace Mycila {
270280

271281
private:
272282
static int8_t _wifiSignalQuality(int32_t rssi);
283+
uint32_t _restartRequestTime = 0;
284+
uint32_t _restartDelay = 1000;
285+
#ifndef ESPCONNECT_NO_MUTEX
286+
std::mutex _mutex;
287+
#endif
273288
};
274289
} // namespace Mycila

0 commit comments

Comments
 (0)