From 903c27bf8efb9fe2c02c9ea8bfe2a77916c32fcf Mon Sep 17 00:00:00 2001 From: Jarrod Bell Date: Thu, 29 Apr 2021 18:10:43 +1000 Subject: [PATCH] Add wifi connection timeout parameter (#99) * Add timeout parameter for customising wifi connection timeout * Update docs, cleanup code --- docs/wifi-manager.md | 4 ++-- src/WiFiManager.cpp | 11 ++++++----- src/WiFiManager.h | 3 ++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/docs/wifi-manager.md b/docs/wifi-manager.md index 2a74251..7f40e4c 100644 --- a/docs/wifi-manager.md +++ b/docs/wifi-manager.md @@ -6,9 +6,9 @@ The function of the WiFi manager is to help the user connect to a WiFi network. #### begin ```c++ -void begin(char const *apName); +void begin(char const *apName, unsigned long newTimeout = 60000); ``` -This method must be called from the setup of the application. The mandatory argument is the SSID name that will be used in case a captive portal is started. The WiFi manager will connect to the stored WiFi details. If no details are stored, or if this fails, a captive portal will be started from 192.168.4.1. +This method must be called from the setup of the application. The mandatory argument is the SSID name that will be used in case a captive portal is started. The optional timeout value allows you to set how long (in milliseconds) the stored WiFi connection will try for before returning to captive portal mode. The WiFi manager will connect to the stored WiFi details. If no details are stored, or if this fails, a captive portal will be started from 192.168.4.1. #### loop diff --git a/src/WiFiManager.cpp b/src/WiFiManager.cpp index 724b4bb..2e15d01 100644 --- a/src/WiFiManager.cpp +++ b/src/WiFiManager.cpp @@ -11,9 +11,10 @@ WifiManager WiFiManager; //function to call in setup -void WifiManager::begin(char const *apName) -{ +void WifiManager::begin(char const *apName, unsigned long newTimeout) +{ captivePortalName = apName; + timeout = newTimeout; WiFi.mode(WIFI_STA); @@ -38,7 +39,7 @@ void WifiManager::begin(char const *apName) WiFi.begin(); } - if (WiFi.waitForConnectResult() == WL_CONNECTED) + if (WiFi.waitForConnectResult(timeout) == WL_CONNECTED) { //connected Serial.println(PSTR("Connected to stored WiFi details")); @@ -118,14 +119,14 @@ void WifiManager::connectNewWifi(String newSSID, String newPass) WiFi.begin(newSSID.c_str(), newPass.c_str(), 0, NULL, true); delay(2000); - if (WiFi.waitForConnectResult() != WL_CONNECTED) + if (WiFi.waitForConnectResult(timeout) != WL_CONNECTED) { Serial.println(PSTR("New connection unsuccessful")); if (!inCaptivePortal) { WiFi.begin(oldSSID, oldPSK, 0, NULL, true); - if (WiFi.waitForConnectResult() != WL_CONNECTED) + if (WiFi.waitForConnectResult(timeout) != WL_CONNECTED) { Serial.println(PSTR("Reconnection failed too")); startCaptivePortal(captivePortalName); diff --git a/src/WiFiManager.h b/src/WiFiManager.h index b873249..3beaa17 100644 --- a/src/WiFiManager.h +++ b/src/WiFiManager.h @@ -19,6 +19,7 @@ class WifiManager bool reconnect = false; bool inCaptivePortal = false; char const *captivePortalName; + unsigned long timeout = 60000; void startCaptivePortal(char const *apName); void stopCaptivePortal(); @@ -26,7 +27,7 @@ class WifiManager void storeToEEPROM(); public : - void begin(char const *apName); + void begin(char const *apName, unsigned long newTimeout = 60000); void loop(); void forget(); bool isCaptivePortal();