diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 6482ec8..08e61ec 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -486,7 +486,9 @@ void WiFiManager::setupConfigPortal() { // setup dns and web servers dnsServer.reset(new DNSServer()); - server.reset(new WM_WebServer(80)); + server.reset(new WM_WebServer(_httpPort)); + + if(_httpPort != 80) DEBUG_WM(DEBUG_VERBOSE,"http server started with custom port: ",_httpPort); // @todo not showing ip /* Setup the DNS server redirecting all the domains to the apIP */ dnsServer->setErrorReplyCode(DNSReplyCode::NoError); @@ -1828,11 +1830,16 @@ void WiFiManager::handleNotFound() { boolean WiFiManager::captivePortal() { DEBUG_WM(DEBUG_DEV,"-> " + server->hostHeader()); - if(!_enableCaptivePortal) return false; // skip redirections - - if (!isIp(server->hostHeader())) { + if(!_enableCaptivePortal) return false; // skip redirections, @todo maybe allow redirection even when no cp ? might be useful + + String serverLoc = toStringIp(server->client().localIP()); + if(_httpPort != 80) serverLoc += ":" + (String)_httpPort; // add port if not default + bool doredirect = serverLoc != server->hostHeader(); // redirect if hostheader not server ip, prevent redirect loops + // doredirect = !isIp(server->hostHeader()) // old check + + if (doredirect) { DEBUG_WM(DEBUG_VERBOSE,F("<- Request redirected to captive portal")); - server->sendHeader(F("Location"), (String)F("http://") + toStringIp(server->client().localIP()), true); + server->sendHeader(F("Location"), (String)F("http://") + serverLoc, true); server->send ( 302, FPSTR(HTTP_HEAD_CT2), ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. server->client().stop(); // Stop is needed because we sent no content length return true; @@ -2476,6 +2483,10 @@ void WiFiManager::setClass(String str){ _bodyClass = str; } +void WiFiManager::setHttpPort(uint16_t port){ + _httpPort = port; +} + // HELPERS /** diff --git a/WiFiManager.h b/WiFiManager.h index 8b84d5d..87e136e 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -297,6 +297,9 @@ class WiFiManager void setClass(String str); String getDefaultAPName(); + // set port of webserver + void setHttpPort(uint16_t port); + std::unique_ptr dnsServer; #if defined(ESP32) && defined(WM_WEBSERVERSHIM) @@ -349,6 +352,7 @@ 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 #ifdef ESP32 static uint8_t _lastconxresulttmp; // tmp var for esp32 callback diff --git a/examples/DEV/OnDemandConfigPortal/OnDemandConfigPortal.ino b/examples/DEV/OnDemandConfigPortal/OnDemandConfigPortal.ino index 9579882..b969cc5 100644 --- a/examples/DEV/OnDemandConfigPortal/OnDemandConfigPortal.ino +++ b/examples/DEV/OnDemandConfigPortal/OnDemandConfigPortal.ino @@ -14,11 +14,11 @@ #include #endif -#define TRIGGER_PIN 0 const char* modes[] = { "NULL", "STA", "AP", "STA+AP" }; unsigned long mtime = 0; +// OLED TEST , untested // // #define MYOLED // #include @@ -69,10 +69,14 @@ void print_oled(String str,uint8_t size){ WiFiManager wm; -// OPTION FLAGS -bool TEST_CP = true; // always start the configportal, even if ap found -bool TEST_NET = true; // do a network test after connect, (gets ntp time) -bool ALLOWONDEMAND = true; +// TEST OPTION FLAGS +bool TEST_CP = true; // always start the configportal, even if ap found +int TESP_CP_TIMEOUT = 90; // test cp timeout + +bool TEST_NET = true; // do a network test after connect, (gets ntp time) +bool ALLOWONDEMAND = true; // enable on demand +int ONDDEMANDPIN = 0; // gpio for button + // char ssid[] = "*************"; // your network SSID (name) // char pass[] = "********"; // your network password @@ -194,7 +198,7 @@ void setup() { //sets timeout until configuration portal gets turned off //useful to make it all retry or go to sleep in seconds - wm.setConfigPortalTimeout(120); + wm.setConfigPortalTimeout(120); // wm.setConnectTimeout(20); // wm.setShowStaticFields(true); @@ -205,6 +209,9 @@ void setup() { wm.setBreakAfterConfig(true); + // set custom webserver port, automatic captive portal does not work with custom ports! + // wm.setHttpPort(8080); + //fetches ssid and pass and tries to connect //if it does not connect it starts an access point with the specified name //here "AutoConnectAP" @@ -219,7 +226,7 @@ void setup() { delay(1000); Serial.println("TEST_CP ENABLED"); // start configportal always - wm.setConfigPortalTimeout(60); + wm.setConfigPortalTimeout(TESP_CP_TIMEOUT); wm.startConfigPortal("WM_ConnectAP"); } else { @@ -229,7 +236,7 @@ void setup() { } wifiInfo(); - pinMode(TRIGGER_PIN, INPUT_PULLUP); + pinMode(ONDDEMANDPIN, INPUT_PULLUP); #ifdef USEOTA ArduinoOTA.begin(); @@ -249,9 +256,9 @@ void loop() { ArduinoOTA.handle(); #endif // is configuration portal requested? - if (ALLOWONDEMAND && digitalRead(TRIGGER_PIN) == LOW ) { + if (ALLOWONDEMAND && digitalRead(ONDDEMANDPIN) == LOW ) { delay(100); - if ( digitalRead(TRIGGER_PIN) == LOW ){ + if ( digitalRead(ONDDEMANDPIN) == LOW ){ Serial.println("BUTTON PRESSED"); wm.setConfigPortalTimeout(140); wm.setParamsPage(false); // move params to seperate page, not wifi, do not combine with setmenu!