WiFiManager icon indicating copy to clipboard operation
WiFiManager copied to clipboard

setHostname does not set the host name

Open DonatelloX opened this issue 2 years ago • 52 comments

Basic Infos

Hardware

WiFimanager Branch/Release: Master v2.0.11-beta ESP32 dev module

Description

setHostname does not set the host name.

Settings in IDE

Arduino IDE 1.8.16 ESP32 Core 1.0.6 WiFimanager Branch/Release: Master v2.0.11-beta

Sketch

#include <WiFiManager.h>

void setup() {
  Serial.begin(115200);
  
   WiFi.mode(WIFI_STA);
   
  WiFiManager wm;
  std::vector<const char *> menu = {"wifi", "exit"};
  wm.setMenu(menu);
  wm.setHostname("MyTestWiFi");
  wm.setWiFiAutoReconnect(true);
  wm.setConfigPortalTimeout(120);
  wm.startConfigPortal();

  if(!wm.autoConnect("MyTestWiFi")) {
    Serial.println("WIFImanager: failed to connect :(");
  } else {
    Serial.println("WIFImanager: connected to " + wm.getWiFiSSID());
  }
}

void loop() {

}

Debug Messages

*wm:[2] Starting Config Portal 
*wm:[2] Disabling STA 
*wm:[2] Enabling AP 
*wm:[1] StartAP with SSID:  ESP32_1BA4AE30
*wm:[2] AP has anonymous access! 
*wm:[1] AP IP address: 192.168.4.1
*wm:[2] setting softAP Hostname: MyTestWiFi
*wm:[1] Starting Web Portal 
*wm:[2] HTTP server started 
*wm:[2] Config Portal Running, blocking, waiting for clients... 
*wm:[2] Portal Timeout In 120 seconds
*wm:[2] Portal Timeout In 97 seconds
*wm:[2] Portal Timeout In 67 seconds
[...]

The host name is ESP32_1BA4AE30.

DonatelloX avatar Apr 29 '22 11:04 DonatelloX

Exactly the same problem here. The same code on ESP8266 sets the hostname correctly. But on ESP32 the hostname is "esp32-8071E0"

Same problem and possible solution ? https://github.com/prampec/IotWebConf/pull/254

olonsoft avatar Jun 23 '22 08:06 olonsoft

Thats the SSID, not the hostname

tablatronix avatar Jun 23 '22 13:06 tablatronix

Thank you @tablatronix for reply. How can I set the SSID name?

DonatelloX avatar Jun 23 '22 13:06 DonatelloX

startConfigPortal(SSID)

also why are you starting cp AND using autoconnect?

tablatronix avatar Jun 23 '22 14:06 tablatronix

@tablatronix

Thats the SSID, not the hostname

There must be a misunderstanding here. The issue is about the hostname. As you see in @DonatelloX code, he sets hostname wm.setHostname("MyTestWiFi");. When the ESP connects to wifi successfully, the router should show MyTestWiFi as a connected client. But is shows esp32-8071E0 in my case. This only happens on ESP32. The same code on ESP8266 shows at router MyTestWiFi.

olonsoft avatar Jun 24 '22 06:06 olonsoft

the code is a bit confusing, so i might have misread it, but can you try calling setupHostname(restart=False) after setHostname ? it looks like this is missing in the ap start sequence for ESP32

(also with restart=True .. just in case)

mindforger avatar Jun 24 '22 06:06 mindforger

bool setupHostname(bool restart); is a private function. Unless you mean to modify the source code.

olonsoft avatar Jun 24 '22 07:06 olonsoft

yeah, please try that (making it public temporale). I am curious and i don't have a esp32 before weekend to test myself

mindforger avatar Jun 24 '22 07:06 mindforger

yeah, please try that (making it public temporale). I am curious and i don't have a esp32 before weekend to test myself

I 've tried what you suggest setupHostname(true/false). Same result.

olonsoft avatar Jun 24 '22 09:06 olonsoft

even the debug output the same? that sounds odd! from the debug output posted earlier and reading the code i figured that this call might be missing in the esp32 defines

mindforger avatar Jun 24 '22 10:06 mindforger

No. The code is not missing.

Same code for ESP32 and ESP8266.

ESP32:

*wm:[1] AutoConnect
*wm:[2] ESP32 event handler enabled
*wm:[2] Setting Hostnames:  My-IoT-App
*wm:[2] Setting WiFi hostname
*wm:[2] Connecting as wifi client...
*wm:[2] setSTAConfig static ip not set, skipping
*wm:[1] Connecting to SAVED AP: MyTestAP
*wm:[1] connectTimeout not set, ESP waitForConnectResult...
*wm:[2] Connection result: WL_CONNECTED
*wm:[1] AutoConnect: SUCCESS
*wm:[2] Connected in 827 ms
*wm:[1] STA IP Address: 192.168.1.231

ROUTER:

MAC-Address	        Host
30:AE:A4:80:71:E0	esp32-8071E0 (192.168.1.231)

ESP8266:

*wm:[1] AutoConnect
*wm:[2] Setting Hostnames:  My-IoT-App
*wm:[2] Setting WiFi hostname 
*wm:[2] Connecting as wifi client...
*wm:[2] setSTAConfig static ip not set, skipping
*wm:[1] Connecting to SAVED AP: MyTestAP
*wm:[1] connectTimeout not set, ESP waitForConnectResult... 
*wm:[2] Connection result: WL_CONNECTED
*wm:[1] AutoConnect: SUCCESS
*wm:[2] Connected in 6422 ms
*wm:[1] STA IP Address: 192.168.1.241

Router:

MAC-Address	        Host
18:FE:34:D5:3F:AF	My-IoT-App (192.168.1.241)

olonsoft avatar Jun 24 '22 11:06 olonsoft

Looking at the logs, I suspect that the problem might be with the autoconnect feature of ESP32. I see that the connect time of ESP32 is only 827ms. I am thinking that as soon as the ESP32 restarts, it automatically connects to WiFi, BEFORE my code reaches the sethostname(xxx) function. My code blinks a led for a second on boot before calling wm.autoconnect(ap,psw) and that time might be enough for ESP to connect before any other call to wifi functions. I'll investigate it further.

Update 1: I 've tried wm.setCleanConnect(true); and WiFi.disconnect(false,false); WiFi.setAutoConnect(false); at the begining of the setup without success. I see now that in function setupHostname(bool restart) there is a code to disconnect WiFi if it is already connected. So the problem might be elsewhere.

olonsoft avatar Jun 24 '22 16:06 olonsoft

I swear I test this every other week.. Must be a esp issue, cause I just went through this in another issue, let me check again

tablatronix avatar Jun 24 '22 23:06 tablatronix

yup definitely an issue, I think there is some issue with when this is called or a race condition. testing now

tablatronix avatar Jun 24 '22 23:06 tablatronix

yup ok so there seems to be something going on across versions. My notes say sethostname MUST be set after sta starts, BUT another issue hinted and I just tested it, that it must be set BEFORE sta starts. SOOOOO

I need to find out if this changed or what

*wm:[1] ESP SDK version: v4.4-beta1-189-ga79dc75f0a

tablatronix avatar Jun 24 '22 23:06 tablatronix

https://github.com/tzapu/WiFiManager/issues/1400#issuecomment-1111682560

What version sdk you all on ?

tablatronix avatar Jun 25 '22 00:06 tablatronix

I pushed a test fix, needing test esp8266 and see if it broke, and older esp32 libs

  • [ ] check hostname works on esp8266
  • [ ] check hostname works on older esp32 < 2.0.3

tablatronix avatar Jun 25 '22 01:06 tablatronix

I am on v4.4.1-1-gb8050b365e I can't test it before Monday. I'll keep you informed.

olonsoft avatar Jun 25 '22 06:06 olonsoft

It works now for both ESP8266 and ESP32. BUT I had to remove the line WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP from the setup() as suggested in most of the examples. With that line included, the hostname does not set.

olonsoft avatar Jun 27 '22 07:06 olonsoft

Yeah i am looking into that also , it seems hostname MUST be set before wifi is enabled, used to be connected. I need to find out why this changed in the IDF or if there is just some bug in esp we can fix. If not I can find another wya, I just have ot be careful to not stop and start stuff if it causes even a 500ms delay in connections.

tablatronix avatar Jun 27 '22 15:06 tablatronix

I have the same issue with inability to set the Hostname. The Hostname seems to be the AP Hostname when the configuration was saved.

Removing the WiFi.mode( WIFI_STA ); had no effect.

Otherwise, WiFiManager has been wonderful and saved me a lot of time.

PeterHouseJr avatar Jul 29 '22 17:07 PeterHouseJr

hmm, still not working? I pushed a fix for this, are you using latest git?

tablatronix avatar Jul 29 '22 20:07 tablatronix

I did not know there was a fix and am using 2.0.9. I will update and test and let you know . . .

Thank you.

PeterHouseJr avatar Aug 01 '22 13:08 PeterHouseJr

sorry ill try to cut a new beta

tablatronix avatar Aug 01 '22 14:08 tablatronix

I will wait for your new beta.

PeterHouseJr avatar Aug 01 '22 14:08 PeterHouseJr

I tried the 2.0.11beta firmware and still no success setting the Hostname. After calling the setHostname function the getHostname function returns the correct setHostname string but the actual Hostname on the wire is the original AP Hostname.

I tried the following code at the end of Setup() and still no success. ` Serial.println( "Setup(): set Hostname and Restart" ); WiFi.setHostname( Hostname ); Serial.print( " Set AP Hostname Set to: " ); Serial.println( Hostname ); Serial.print( " Get AP Hostname: " ); Serial.println( WiFi.getHostname() );

Serial.println( " AP soft Restart" ); WiFi.reconnect(); `

I also cleared the lease from the router to make sure the router is not caching the Hostname and also confirmed using ping Hostname from the command line.

PeterHouseJr avatar Aug 02 '22 18:08 PeterHouseJr

Ill try to do it tonight

tablatronix avatar Aug 03 '22 02:08 tablatronix

Do you have an idea what or where the problem is located? Is it in the WiFiManager library or is the problem deeper in the WiFi library? How can I help?

PeterHouseJr avatar Aug 09 '22 02:08 PeterHouseJr

Sorry I got pretty sick this week, I will make a beta tomorrow, the only change was moving the hostname call in autoconnect, you can see the commit above

tablatronix avatar Aug 09 '22 02:08 tablatronix

I am still having a Hostname issue.

When I start the device from an unconfigured state (AP Mode):

Hostname = 'esp32s2-69A284' SSID = 'esp32-34B47269A284' IP = 192.168.4.1

After entering the local WiFi info (STA Mode):

Hostname = 'esp32s2-69A284' Hostname should be 'ELC-69A284' IP = 192.168.99.106

Nothing I can do will change the Hostname.

I guess the Hostname, esp32s2-69A284, is from somewhere in the firmware. It is certainly not a string that appears anywhere in my code.

PeterHouseJr avatar Aug 16 '22 20:08 PeterHouseJr