From 32075b32cd3dca17422a62d75f111b7a2e1ba6b1 Mon Sep 17 00:00:00 2001 From: tablatronix Date: Mon, 11 May 2020 12:39:50 -0500 Subject: [PATCH 1/6] fixes #1040, adds http port `setHttpPort` --- WiFiManager.cpp | 21 +++++++++++---- WiFiManager.h | 4 +++ .../OnDemandConfigPortal.ino | 27 ++++++++++++------- 3 files changed, 37 insertions(+), 15 deletions(-) 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! From 6515190124d181a2be5140f530d987d845c5aa51 Mon Sep 17 00:00:00 2001 From: tablatronix Date: Mon, 11 May 2020 17:23:20 -0500 Subject: [PATCH 2/6] fixes #1046 --- WiFiManager.cpp | 12 +++++++----- WiFiManager.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 08e61ec..b2e97eb 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -206,9 +206,10 @@ WiFiManager::~WiFiManager() { } // @todo remove event - // #ifdef ESP32 - // WiFi.removeEvent(std::bind(&WiFiManager::WiFiEvent,this)); - // #endif + // WiFi.onEvent(std::bind(&WiFiManager::WiFiEvent,this,_1,_2)); + #ifdef ESP32 + WiFi.removeEvent(wm_event_id); + #endif DEBUG_WM(DEBUG_DEV,F("unloading")); } @@ -2898,8 +2899,9 @@ String WiFiManager::WiFi_psk(bool persistent) const { void WiFiManager::WiFiEvent(WiFiEvent_t event,system_event_info_t info){ if(!_hasBegun){ // DEBUG_WM(DEBUG_VERBOSE,"[ERROR] WiFiEvent, not ready"); + Serial.println("[ERROR] wm not ready"); return; - } + } // DEBUG_WM(DEBUG_VERBOSE,"[EVENT]",event); if(event == SYSTEM_EVENT_STA_DISCONNECTED){ DEBUG_WM(DEBUG_VERBOSE,"[EVENT] WIFI_REASON:",info.disconnected.reason); @@ -2927,7 +2929,7 @@ void WiFiManager::WiFi_autoReconnect(){ // @todo move to seperate method, used for event listener now DEBUG_WM(DEBUG_VERBOSE,"ESP32 event handler enabled"); using namespace std::placeholders; - WiFi.onEvent(std::bind(&WiFiManager::WiFiEvent,this,_1,_2)); + wm_event_id = WiFi.onEvent(std::bind(&WiFiManager::WiFiEvent,this,_1,_2)); // } #endif } diff --git a/WiFiManager.h b/WiFiManager.h index 87e136e..b5189af 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -134,7 +134,6 @@ class WiFiManagerParameter { int _length; int _labelPlacement; const char *_customHTML; - friend class WiFiManager; }; @@ -355,6 +354,7 @@ class WiFiManager uint16_t _httpPort = 80; // port for webserver #ifdef ESP32 + wifi_event_id_t wm_event_id; static uint8_t _lastconxresulttmp; // tmp var for esp32 callback #endif From 135e872caa513ccfeab100e98e97ac471a9bdd20 Mon Sep 17 00:00:00 2001 From: tablatronix Date: Mon, 11 May 2020 21:29:46 -0500 Subject: [PATCH 3/6] remove debug --- WiFiManager.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index b2e97eb..bd14602 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -2387,7 +2387,7 @@ void WiFiManager::setMenu(const char * menu[], uint8_t size){ } } } - DEBUG_WM(getMenuOut()); + // DEBUG_WM(getMenuOut()); } /** @@ -2410,7 +2410,7 @@ void WiFiManager::setMenu(std::vector& menu){ } } } - DEBUG_WM(getMenuOut()); + // DEBUG_WM(getMenuOut()); } From 7b52e709db13cecf2adbfaf20437c55019059ada Mon Sep 17 00:00:00 2001 From: "doncov.eugene" Date: Thu, 14 May 2020 15:50:39 +0300 Subject: [PATCH 4/6] Readme: fill DNS server if setSTAStaticIPConfig --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 6acf010..34f19cb 100644 --- a/README.md +++ b/README.md @@ -292,6 +292,8 @@ wifiManager.setSTAStaticIPConfig(IPAddress(192,168,0,99), IPAddress(192,168,0,1) ``` There are a couple of examples in the examples folder that show you how to set a static IP and even how to configure it through the web configuration portal. +NOTE: You should fill DNS server if you have HTTP requests with hostnames or syncronize time (NTP). It's the same as gateway ip or a popular (Google DNS: 8.8.8.8). + #### Custom HTML, CSS, Javascript There are various ways in which you can inject custom HTML, CSS or Javascript into the configuration portal. The options are: From 3e9f9a103e3555de9961c8c7a32c2fcb03ad45a9 Mon Sep 17 00:00:00 2001 From: tablatronix Date: Thu, 14 May 2020 21:55:47 -0500 Subject: [PATCH 5/6] #1050 prevent copy assignment of param constructor --- WiFiManager.cpp | 7 +++++++ WiFiManager.h | 2 ++ 2 files changed, 9 insertions(+) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index bd14602..69d07cd 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -68,6 +68,12 @@ WiFiManagerParameter::~WiFiManagerParameter() { _length=0; // setting length 0, ideally the entire parameter should be removed, or added to wifimanager scope so it follows } +// WiFiManagerParameter& WiFiManagerParameter::operator=(const WiFiManagerParameter& rhs){ +// Serial.println("copy assignment op called"); +// (*this->_value) = (*rhs._value); +// return *this; +// } + // @note debug is not available in wmparameter class void WiFiManagerParameter::setValue(const char *defaultValue, int length) { if(!_id){ @@ -89,6 +95,7 @@ void WiFiManagerParameter::setValue(const char *defaultValue, int length) { } } const char* WiFiManagerParameter::getValue() { + // Serial.println(printf("Address of _value is %p\n", (void *)_value)); return _value; } const char* WiFiManagerParameter::getID() { diff --git a/WiFiManager.h b/WiFiManager.h index b5189af..4fa1e6e 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -114,6 +114,7 @@ class WiFiManagerParameter { WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length, const char *custom); WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length, const char *custom, int labelPlacement); ~WiFiManagerParameter(); + // WiFiManagerParameter& operator=(const WiFiManagerParameter& rhs); const char *getID(); const char *getValue(); @@ -128,6 +129,7 @@ class WiFiManagerParameter { void init(const char *id, const char *label, const char *defaultValue, int length, const char *custom, int labelPlacement); private: + WiFiManagerParameter& operator=(const WiFiManagerParameter&); const char *_id; const char *_label; char *_value; From 0486034c6a4e027c3a84bd03287a130a81305391 Mon Sep 17 00:00:00 2001 From: tablatronix Date: Thu, 14 May 2020 21:56:33 -0500 Subject: [PATCH 6/6] some comments etc --- WiFiManager.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 69d07cd..cfd9060 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -290,6 +290,8 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { connected = true; DEBUG_WM(F("AutoConnect: ESP Already Connected")); setSTAConfig(); + // @todo not sure if this check makes sense, causes dup setSTAConfig in connectwifi, + // and we have no idea WHAT we are connected to } if(connected || connectWifi("", "") == WL_CONNECTED){ @@ -305,12 +307,13 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { DEBUG_WM(DEBUG_DEV,"hostname: STA",WiFi.getHostname()); #endif } - return true; + + return true; // connected success } // possibly skip the config portal if (!_enableConfigPortal) { - return false; + return false; // not connected and not cp } DEBUG_WM(F("AutoConnect: FAILED")); @@ -2618,8 +2621,16 @@ void WiFiManager::debugPlatformInfo(){ DEBUG_WM(F("getFreeHeap(): "),(String)ESP.getFreeHeap()); #elif defined(ESP32) size_t freeHeap = heap_caps_get_free_size(MALLOC_CAP_8BIT); - DEBUG_WM("Free heap: ", freeHeap); - DEBUG_WM("ESP-IDF version: ", esp_get_idf_version()); + DEBUG_WM("Free heap: ", ESP.getFreeHeap()); + DEBUG_WM("ESP SDK version: ", ESP.getSdkVersion()); + // esp_chip_info_t chipInfo; + // esp_chip_info(&chipInfo); + // DEBUG_WM("Chip Info: Model: ",chipInfo.model); + // DEBUG_WM("Chip Info: Cores: ",chipInfo.cores); + // DEBUG_WM("Chip Info: Rev: ",chipInfo.revision); + // DEBUG_WM(printf("Chip Info: Model: %d, cores: %d, revision: %d", chipInfo.model.c_str(), chipInfo.cores, chipInfo.revision)); + // DEBUG_WM("Chip Rev: ",(String)ESP.getChipRevision()); + // core version is not avail #endif }