Skip to content

Commit 1523636

Browse files
authored
Merge pull request #209 from OpenEVSE/jeremypoulter/issue205
Reset the WiFi state after receiving a disconnect event
2 parents f3c0f0e + 5c813d5 commit 1523636

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/emonesp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
#define WIFI_CLIENT_RETRY_TIMEOUT (5 * 60 * 1000)
5555
#endif
5656

57+
#ifndef WIFI_CLIENT_DISCONNECTS_BEFORE_AP
58+
#define WIFI_CLIENT_DISCONNECTS_BEFORE_AP 2
59+
#endif
60+
5761
// Used to change the ADC channel used for seeding the rndom number generator
5862
// Should be set to an unconnected pin
5963
#ifndef RANDOM_SEED_CHANNEL

src/net_manager.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,11 @@ static void net_wifi_onStationModeDisconnected(const WiFiEventStationModeDisconn
214214

215215
client_disconnects++;
216216

217-
if(net_wifi_mode_is_sta()) {
218-
startClient();
219-
}
217+
// Clear the WiFi state and tru to connect again
218+
WiFi.disconnect();
219+
WiFi.mode(WIFI_OFF);
220+
221+
startClient();
220222
}
221223

222224
static void net_wifi_onAPModeStationConnected(const WiFiEventSoftAPModeStationConnected &event) {
@@ -468,7 +470,7 @@ net_loop()
468470
if(isClientOnly && !WiFi.isConnected())
469471
{
470472
// If we have failed to connect turn on the AP
471-
if(client_disconnects > 2) {
473+
if(client_disconnects > WIFI_CLIENT_DISCONNECTS_BEFORE_AP) {
472474
startAP();
473475
client_retry = true;
474476
client_retry_time = millis() + WIFI_CLIENT_RETRY_TIMEOUT;
@@ -478,8 +480,7 @@ net_loop()
478480
// Remain in AP mode for 5 Minutes before resetting
479481
if(isApOnly && 0 == apClients && client_retry && millis() > client_retry_time) {
480482
DEBUG.println("client re-try, resetting");
481-
delay(50);
482-
ESPAL.reset();
483+
net_wifi_restart();
483484
}
484485

485486
if(dnsServerStarted) {

0 commit comments

Comments
 (0)