Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Add wifi connection timeout parameter (#99)
Browse files Browse the repository at this point in the history
* Add timeout parameter for customising wifi connection timeout

* Update docs, cleanup code
  • Loading branch information
jarrodbell authored Apr 29, 2021
1 parent 9aec5a2 commit 903c27b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/wifi-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions src/WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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"));
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/WiFiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ class WifiManager
bool reconnect = false;
bool inCaptivePortal = false;
char const *captivePortalName;
unsigned long timeout = 60000;

void startCaptivePortal(char const *apName);
void stopCaptivePortal();
void connectNewWifi(String newSSID, String newPass);
void storeToEEPROM();

public :
void begin(char const *apName);
void begin(char const *apName, unsigned long newTimeout = 60000);
void loop();
void forget();
bool isCaptivePortal();
Expand Down

0 comments on commit 903c27b

Please sign in to comment.