Skip to content

Commit

Permalink
Fix hostname #1403
Browse files Browse the repository at this point in the history
  • Loading branch information
tablatronix committed Jun 25, 2022
1 parent 9e408e4 commit afd328b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
14 changes: 10 additions & 4 deletions WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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"));
Expand Down
8 changes: 6 additions & 2 deletions examples/Super/OnDemandConfigPortal/OnDemandConfigPortal.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<void(WiFiManager*)> func );
// called after webserver has started
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit afd328b

Please sign in to comment.