diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 1125192..bff08a1 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -275,6 +275,12 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { _startconn = millis(); _begin(); + // sethostname before wifi ready + // https://github.com/tzapu/WiFiManager/issues/1403 + if(_hostname != ""){ + setupHostname(true); + } + // attempt to connect using saved settings, on fail fallback to AP config portal if(!WiFi.enableSTA(true)){ // handle failure mode Brownout detector etc. @@ -298,9 +304,9 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { WiFi_autoReconnect(); // set hostname before stating - if(_hostname != ""){ - setupHostname(true); - } + // if(_hostname != ""){ + // setupHostname(true); + // } // if already connected, or try stored connect // @note @todo ESP32 has no autoconnect, so connectwifi will always be called unless user called begin etc before @@ -386,7 +392,7 @@ bool WiFiManager::setupHostname(bool restart){ #endif #elif defined(ESP32) // @note hostname must be set after STA_START - delay(200); // do not remove, give time for STA_START + // @note, this may have changed at some point, now it wont work, I have to set it before. #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Setting WiFi hostname")); diff --git a/examples/Super/OnDemandConfigPortal/OnDemandConfigPortal.ino b/examples/Super/OnDemandConfigPortal/OnDemandConfigPortal.ino index 362533c..f7d4b52 100644 --- a/examples/Super/OnDemandConfigPortal/OnDemandConfigPortal.ino +++ b/examples/Super/OnDemandConfigPortal/OnDemandConfigPortal.ino @@ -23,7 +23,7 @@ WiFiManager wm; // TEST OPTION FLAGS -bool TEST_CP = true; // always start the configportal, even if ap found +bool TEST_CP = false; // 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) @@ -34,6 +34,8 @@ bool WMISBLOCKING = true; // use blocking or non blocking mode, non global pa // char ssid[] = "*************"; // your network SSID (name) // char pass[] = "********"; // your network password + +//callbacks // called after AP mode and config portal has started // setAPCallback( std::function func ); // called after webserver has started @@ -220,7 +222,8 @@ void setup() { // set Hostname - wm.setHostname(("WM_"+wm.getDefaultAPName()).c_str()); + wm.setHostname(("WM_"+wm.getDefaultAPName()).c_str()); + // wm.setHostname("WM_RANDO_1234"); // set custom channel // wm.setWiFiAPChannel(13); @@ -310,6 +313,7 @@ void wifiInfo(){ Serial.println("[WIFI] SAVED: " + (String)(wm.getWiFiIsSaved() ? "YES" : "NO")); Serial.println("[WIFI] SSID: " + (String)wm.getWiFiSSID()); Serial.println("[WIFI] PASS: " + (String)wm.getWiFiPass()); + Serial.println("[WIFI] HOSTNAME: " + (String)WiFi.getHostname()); } void loop() {