adds #1137 connect retries

setConnectRetries()
This commit is contained in:
tablatronix
2020-11-19 14:25:12 -06:00
parent 9e29835ac8
commit a7fb6d5caf
3 changed files with 30 additions and 6 deletions
+19 -2
View File
@@ -755,7 +755,7 @@ bool WiFiManager::shutdownConfigPortal(){
// clean up, flow is convoluted, and causes bugs
uint8_t WiFiManager::connectWifi(String ssid, String pass) {
DEBUG_WM(DEBUG_VERBOSE,F("Connecting as wifi client..."));
uint8_t retry = 1;
uint8_t connRes = (uint8_t)WL_NO_SSID_AVAIL;
setSTAConfig();
@@ -766,6 +766,10 @@ uint8_t WiFiManager::connectWifi(String ssid, String pass) {
if(_cleanConnect) WiFi_Disconnect(); // disconnect before begin, in case anything is hung, this causes a 2 seconds delay for connect
// @todo find out what status is when this is needed, can we detect it and handle it, say in between states or idle_status
while(retry <= _connectRetries){
if(_connectRetries > 1){
DEBUG_WM(F("Connect Wifi, ATTEMPT #"),(String)retry+" of "+(String)_connectRetries);
}
// if ssid argument provided connect to that
if (ssid != "") {
wifiConnectNew(ssid,pass);
@@ -783,11 +787,13 @@ uint8_t WiFiManager::connectWifi(String ssid, String pass) {
connRes = waitForConnectResult();
}
else {
DEBUG_WM(F("No wifi save required, skipping"));
DEBUG_WM(F("No wifi saved, skipping"));
}
}
DEBUG_WM(DEBUG_VERBOSE,F("Connection result:"),getWLStatusString(connRes));
retry++;
}
// WPS enabled? https://github.com/esp8266/Arduino/pull/4889
#ifdef NO_EXTRA_4K_HEAP
@@ -2057,6 +2063,17 @@ void WiFiManager::setConfigPortalTimeout(unsigned long seconds) {
void WiFiManager::setConnectTimeout(unsigned long seconds) {
_connectTimeout = seconds * 1000;
}
/**
* [setConnectRetries description]
* @access public
* @param {[type]} uint8_t numRetries [description]
*/
void WiFiManager::setConnectRetries(uint8_t numRetries){
_connectRetries = numRetries;
if(_connectRetries > 10) numRetries = 10; // some cap
}
/**
* toggle _cleanconnect, always disconnect before connecting
* @param {[type]} bool enable [description]
+6 -1
View File
@@ -226,6 +226,9 @@ class WiFiManager
//sets timeout for which to attempt connecting, useful if you get a lot of failed connects
void setConnectTimeout(unsigned long seconds);
// sets number of retries for autoconnect, force retry after wait failure exit
void setConnectRetries(uint8_t numRetries); // default 1
//sets timeout for which to attempt connecting on saves, useful if there are bugs in esp waitforconnectloop
void setSaveConnectTimeout(unsigned long seconds);
@@ -403,7 +406,9 @@ class WiFiManager
bool _channelSync = false; // use same wifi sta channel when starting ap
int32_t _apChannel = 0; // channel to use for ap
bool _apHidden = false; // store softap hidden value
uint16_t _httpPort = 80; // port for webserver
uint16_t _httpPort = 80; // port for webserver
// uint8_t _retryCount = 0; // counter for retries, probably not needed if synchronous
uint8_t _connectRetries = 1; // number of sta connect retries, force reconnect, wait loop (connectimeout) does not always work and first disconnect bails
#ifdef ESP32
wifi_event_id_t wm_event_id;
@@ -162,7 +162,10 @@ void setup() {
// set connection timeout
// wm.setConnectTimeout(20);
// set wifi connect retries
// wm.setConnectRetries(2);
// show static ip fields
// wm.setShowStaticFields(true);
@@ -171,8 +174,7 @@ void setup() {
// This is sometimes necessary, it is still unknown when and why this is needed but it may solve some race condition or bug in esp SDK/lib
// wm.setCleanConnect(true); // disconnect before connect, clean connect
//
wm.setBreakAfterConfig(true);
wm.setBreakAfterConfig(true); // needed to use saveWifiCallback
// set custom webserver port, automatic captive portal does not work with custom ports!
// wm.setHttpPort(8080);