-
Notifications
You must be signed in to change notification settings - Fork 7.6k
WiFi.setHostname() doesn't always work #6700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @mrengineer7777, thanks for reporting an issue and precise description. We will take a look on this. |
You are thinking of another "hostname". The one that you need is provided by the MDNS library (and NBNS for Windows). This hostname here is for communicating with the DHCP server. |
@me-no-dev I'm not talking about MDNS (netbios), though discovery via name might be nice to add to our product. I'm referring to the name registered with the DHCP (and DNS?) server, as set with WiFi.setHostname(). As I noted above the behavior is inconsistent. This is not is a high priority issue, but it would be nice if it worked. I even provided a workaround which should give a clue as to why it doesn't always work. |
And what name does it show? For Maybe tcpdump the DHCP traffic to confirm |
@me-no-dev I'm not here to argue with you. I'm just trying to make arduino-esp32 better. Sniffing WiFi traffic is difficult as it requires me to build a custom network. I've done it before. What I can do fairly easily though is setup a simple project to replicate the behavior I'm seeing. I'll get back with you. |
@me-no-dev Here is a sample project replicating the issue: https://github.com/mrengineer7777/test_sethostname You can set your WiFi name and password in mEprom.cpp Unfortunately I don't know the details of our DHCP and DNS servers on the corporate side. |
The code that actually sets the hostname is below. It only fires if WiFi mode changes, which is a problem for our sample because the WiFi scan sets the mode, but doesn't actually connect to WiFi.
|
you really need to provide minimal example of what you are doing. That project is way too much of a code. Then as I tried to explain to you.... name resolution and reverse resolutions are handled by a DNS server, which has nothing to do with your ESP devices. I also showed you how the hostname I set to my chip her (your github username) did show right away in my DHCP server (where it is supposed to show). Figure out your infrastructure and look for the problem there ;) |
Fine. I cut it down to one file. It still demonstrates the issue. https://github.com/mrengineer7777/test_sethostname Uncomment this line to see the difference: If you really need to see the DHCP server entry I can get a screenshot later. But I can tell you now the issue is real. |
Try calling so... see why I asked for short example? |
Yes, that's exactly my point. The scan initializes WiFi, which causes setHostname() to not set. The behavior is very confusing. It would be helpful to log a debug message to the console, or even better have WiFi.mode() set hostname consistently. Digging into the IDF: esp_netif_lwip.c
Sets pointer to (const char *) hostname. Thus it is safe to call esp_netif_set_hostname() more than once. A solution is to set hostname even if the mode is the same. This moves
If that is an acceptable solution I can create a PR for it. |
Testing: code change above does not fix issue. I give up. |
@me-no-dev FYI setting the hostname before the scan does work. |
do you have debug enabled and also |
It's remarkable how fiddly something as trivial as setting hostname is on ESP32! The misery in this area already started in 2019 (see #2537) and there seems to be regression in ESP32 Arduino 2.0.x now. I can confirm that with ESP32 Arduino 2.0.3
In my case, I am not explicitly doing anything that initializes WIFI, but still it doesn't work unless I call Note that this changed between ESP32 Arduino 1.0.x and 2.0.x and it is also no longer consistent with ESP8266 Arduino, where setting host name can be done after |
FYI: I have a library which can be used on both ESP32 and ESP8266. The code to initialize STA now looks as follows (note it now supports ESP8266, ESP32 SDK 1.0x and ESP32 SDK 2.0.x)
|
Sounds like this maybe? |
Board
ESP32-WROOM-32UE
Device Description
Custom hardware, but this is not a hardware issue.
FYI we need an option for "Version" below: arduino-esp32 v2.0.3.
Hardware Configuration
Interfaces with 2 UARTs.
Version
latest master (checkout manually)
IDE Name
VSCode with PIO
Operating System
Win10
Flash frequency
?
PSRAM enabled
no
Upload speed
460800
Description
WiFi.setHostname() doesn't always set the hostname. After looking at #6086, #6278, and studying WiFiSTA, WiFiGeneric, I can see the issue.
setHostname() just sets a string (cstr) in memory. It is not applied until WiFiGenericClass::mode() is called, which happens when WiFi.config(), WiFi.begin(), WiFi.enableSTA(), etc are called.
The issue is that if the WiFi.mode() is already STA, then the hostname will not be set even if WiFi is not connected. I would expect hostname to be set in WiFi.begin(), or WiFi.mode() to handle setting it when STA is enabled but not connected.
In our case we scan WiFi networks on startup, which apparently enables STA mode, so setHostname() is never applied.
Impact: It would be nice if it worked.
Workaround: If I stop wifi [
WiFi.mode(WIFI_MODE_NULL);
] before setting the hostname, then it works.Sketch
Debug Message
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
Edit: removed comment in code example about DNS and Netbios, as that is confusing the issue. Use MDNS for Netbios lookups.
The text was updated successfully, but these errors were encountered: