From 77bd201d138f28dcfd5721274168144516374470 Mon Sep 17 00:00:00 2001 From: tablatronix Date: Mon, 22 Feb 2021 16:04:37 -0600 Subject: [PATCH 1/2] fixes #1210 Moves dns server out of webportal setup as it is only needed for softap and captive portal, still enabled for portal so cp can be toggled on and off --- WiFiManager.cpp | 57 +++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index fba3b2b..6ae745c 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -564,21 +564,8 @@ boolean WiFiManager::configPortalHasTimeout(){ return false; } -void WiFiManager::setupConfigPortal() { - - #ifdef WM_DEBUG_LEVEL - DEBUG_WM(F("Starting Web Portal")); - #endif - - // setup dns and web servers +void WiFiManager::setupDNSD(){ dnsServer.reset(new DNSServer()); - server.reset(new WM_WebServer(_httpPort)); - - if(_httpPort != 80) { - #ifdef WM_DEBUG_LEVEL - DEBUG_WM(DEBUG_VERBOSE,F("http server started with custom port: "),_httpPort); // @todo not showing ip - #endif - } /* Setup the DNS server redirecting all the domains to the apIP */ dnsServer->setErrorReplyCode(DNSReplyCode::NoError); @@ -587,12 +574,27 @@ void WiFiManager::setupConfigPortal() { DEBUG_WM(DEBUG_DEV,F("dns server started with ip: "),WiFi.softAPIP()); // @todo not showing ip #endif dnsServer->start(DNS_PORT, F("*"), WiFi.softAPIP()); +} - // @todo new callback, webserver started, callback cannot override handlers, but can grab them first +void WiFiManager::setupConfigPortal() { + + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("Starting Web Portal")); + #endif + + // setup dns and web servers + server.reset(new WM_WebServer(_httpPort)); + + if(_httpPort != 80) { + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("http server started with custom port: "),_httpPort); // @todo not showing ip + #endif + } if ( _webservercallback != NULL) { _webservercallback(); } + // @todo add a new callback maybe, after webserver started, callback cannot override handlers, but can grab them first /* Setup httpd callbacks, web pages: root, wifi config pages, SO captive portal detectors and not found. */ @@ -688,6 +690,12 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo #endif setupConfigPortal(); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_DEV,F("setupDNSD")); + #endif + setupDNSD(); + + if(!_configPortalIsBlocking){ #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Config Portal Running, non blocking/processing")); @@ -748,8 +756,11 @@ boolean WiFiManager::process(){ //using esp enums returns for now, should be fine uint8_t WiFiManager::processConfigPortal(){ - //DNS handler - dnsServer->processNextRequest(); + if(configPortalActive){ + //DNS handler + dnsServer->processNextRequest(); + } + //HTTP handler server->handleClient(); @@ -826,21 +837,25 @@ uint8_t WiFiManager::processConfigPortal(){ bool WiFiManager::shutdownConfigPortal(){ if(webPortalActive) return false; - //DNS handler - dnsServer->processNextRequest(); + if(configPortalActive){ + //DNS handler + dnsServer->processNextRequest(); + } + //HTTP handler server->handleClient(); // @todo what is the proper way to shutdown and free the server up server->stop(); server.reset(); - dnsServer->stop(); // free heap ? - dnsServer.reset(); WiFi.scanDelete(); // free wifi scan results if(!configPortalActive) return false; + dnsServer->stop(); // free heap ? + dnsServer.reset(); + // turn off AP // @todo bug workaround // https://github.com/esp8266/Arduino/issues/3793 From 8aca0beccb006bf73df25271cc1838a13ca5b688 Mon Sep 17 00:00:00 2001 From: tablatronix Date: Mon, 22 Feb 2021 16:04:56 -0600 Subject: [PATCH 2/2] fixup some debugs for portal timeout --- WiFiManager.cpp | 36 +++++++++++++++++++++--------------- WiFiManager.h | 3 ++- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 6ae745c..992ba7e 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -529,9 +529,12 @@ void WiFiManager::stopWebPortal() { } boolean WiFiManager::configPortalHasTimeout(){ + uint16_t logintvl = 30000; // how often to emit timeing out counter logging + // handle timeout portal client check if(_configPortalTimeout == 0 || (_apClientCheck && (WiFi_softap_num_stations() > 0))){ - if(millis() - timer > 30000){ + // debug num clients every 30s + if(millis() - timer > logintvl){ timer = millis(); #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("NUM CLIENTS: "),(String)WiFi_softap_num_stations()); @@ -540,24 +543,24 @@ boolean WiFiManager::configPortalHasTimeout(){ _configPortalStart = millis(); // kludge, bump configportal start time to skew timeouts return false; } - // handle timeout + + // handle timeout webclient check if(_webClientCheck && (_webPortalAccessed>_configPortalStart)>0) _configPortalStart = _webPortalAccessed; + // handle timed out if(millis() > _configPortalStart + _configPortalTimeout){ #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("config portal has timed out")); #endif - return true; - } else if(_debugLevel > 0) { - // log timeout - if(_debug){ - uint16_t logintvl = 30000; // how often to emit timeing out counter logging - if((millis() - timer) > logintvl){ - timer = millis(); - #ifdef WM_DEBUG_LEVEL - DEBUG_WM(DEBUG_VERBOSE,F("Portal Timeout In"),(String)((_configPortalStart + _configPortalTimeout-millis())/1000) + (String)F(" seconds")); - #endif - } + return true; // timeout bail, else do debug logging + } + else if(_debug && _debugLevel > 0) { + // log timeout time remaining every 30s + if((millis() - timer) > logintvl){ + timer = millis(); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("Portal Timeout In"),(String)((_configPortalStart + _configPortalTimeout-millis())/1000) + (String)F(" seconds")); + #endif } } @@ -698,14 +701,17 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo if(!_configPortalIsBlocking){ #ifdef WM_DEBUG_LEVEL - DEBUG_WM(DEBUG_VERBOSE,F("Config Portal Running, non blocking/processing")); + DEBUG_WM(DEBUG_VERBOSE,F("Config Portal Running, non blocking/processing")); + if(_configPortalTimeout > 0) DEBUG_WM(DEBUG_VERBOSE,F("Portal Timeout In"),(String)(_configPortalTimeout/1000) + (String)F(" seconds")); #endif return result; } #ifdef WM_DEBUG_LEVEL - DEBUG_WM(DEBUG_VERBOSE,F("Config Portal Running, blocking, waiting for clients...")); + DEBUG_WM(DEBUG_VERBOSE,F("Config Portal Running, blocking, waiting for clients...")); + if(_configPortalTimeout > 0) DEBUG_WM(DEBUG_VERBOSE,F("Portal Timeout In"),(String)(_configPortalTimeout/1000) + (String)F(" seconds")); #endif + // blocking loop waiting for config while(1){ diff --git a/WiFiManager.h b/WiFiManager.h index 507d112..5003af1 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -497,6 +497,7 @@ class WiFiManager #endif bool startAP(); + void setupDNSD(); uint8_t connectWifi(String ssid, String pass); bool setSTAConfig(); @@ -581,7 +582,7 @@ class WiFiManager boolean portalTimeoutResult = false; boolean portalAbortResult = false; boolean storeSTAmode = true; // option store persistent STA mode in connectwifi - int timer = 0; + int timer = 0; // timer for debug throttle for numclients, and portal timeout messages // WiFiManagerParameter int _paramsCount = 0;