From 4592914fadbb5f3f672e9ed7e564121c7ae59fa9 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Mon, 18 Sep 2017 08:42:09 -0500 Subject: [PATCH] test ondemand webportal --- WiFiManager.cpp | 33 ++++++++++++++++++++++++++------- WiFiManager.h | 3 +++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 00ce317..bbef620 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -169,15 +169,13 @@ boolean WiFiManager::configPortalHasTimeout(){ } void WiFiManager::setupConfigPortal() { + + DEBUG_WM("Starting Web Portal"); + // setup dns and web servers dnsServer.reset(new DNSServer()); server.reset(new ESP8266WebServer(80)); - DEBUG_WM(F("setupConfigPortal")); - _configPortalStart = millis(); - - if(!startAP()) DEBUG_WM("There was an error stating the AP"); // @bug startAP returns unreliable success status - /* Setup the DNS server redirecting all the domains to the apIP */ dnsServer->setErrorReplyCode(DNSReplyCode::NoError); dnsServer->start(DNS_PORT, "*", WiFi.softAPIP()); @@ -196,6 +194,19 @@ void WiFiManager::setupConfigPortal() { DEBUG_WM(F("HTTP server started")); } +void WiFiManager::startWebPortal() { + if(configPortalActive || webPortalActive) return; + setupConfigPortal(); + webPortalActive = true; +} + +void WiFiManager::stopWebPortal() { + if(!configPortalActive && !webPortalActive) return; + DEBUG_WM("Stopping Web Portal"); + webPortalActive = false; + stopConfigPortal(); +} + boolean WiFiManager::startConfigPortal() { String ssid = "ESP" + String(ESP.getChipId()); return startConfigPortal(ssid.c_str(), NULL); @@ -219,6 +230,10 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo configPortalActive = true; connect = false; // global + + DEBUG_WM(F("setupConfigPortal")); + _configPortalStart = millis(); + if(!startAP()) DEBUG_WM("There was an error stating the AP"); // @bug startAP returns unreliable success status // init configportal setupConfigPortal(); @@ -253,7 +268,7 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo } boolean WiFiManager::process(){ - if(configPortalActive && !_configPortalIsBlocking){ + if(webPortalActive || (configPortalActive && !_configPortalIsBlocking)){ uint8_t state = handleConfigPortal(); return state == WL_CONNECTED; } @@ -293,7 +308,9 @@ uint8_t WiFiManager::handleConfigPortal(){ } boolean WiFiManager::stopConfigPortal(){ - // do save callback + if(webPortalActive) return false; + + // do save callback, @todo move out of here if ( _savecallback != NULL) { //todo: confirm or verify data was saved _savecallback(); @@ -308,6 +325,8 @@ boolean WiFiManager::stopConfigPortal(){ server.reset(); dnsServer.reset(); + if(!configPortalActive) return false; + // turn off AP DEBUG_WM(F("disconnect configportal")); bool ret = WiFi.softAPdisconnect(false); diff --git a/WiFiManager.h b/WiFiManager.h index 7d74506..bc69443 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -105,6 +105,8 @@ class WiFiManager boolean startConfigPortal(); boolean startConfigPortal(char const *apName, char const *apPassword = NULL); boolean process(); + void startWebPortal(); + void stopWebPortal(); // get the AP name of the config portal, so it can be used in the callback String getConfigPortalSSID(); @@ -237,6 +239,7 @@ class WiFiManager boolean connect; boolean configPortalActive = false; + boolean webPortalActive = false; boolean _debug = true; void (*_apcallback)(WiFiManager*) = NULL;