Description
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
Extracted pieces of my code:
void setup(void) {
WDT_Start(); //Reboot if our main loop stops responding
Serial.begin(115200);
Serial.println("Booting..."); // USB won't be connected yet, so we will only see this on a reboot
delay(2000); // Delay for debugging. Allows time for USB to connect to computer.
WiFiCtrl.StartEventHandler();
WiFiCtrl.StartPingSvc(); // Make sure SoftAP or WiFi are up
StartScanAsync(); // Start scan before starting WiFi as scan will fail if WiFi isn't connected. Completion of WiFi scan starts WiFi.
}
void loop {
WDT_KeepAlive();
delay(100); // Allow other tasks to run
DispCtrl_ReadEvents(); // Process any events from display (currently only factory reset msg)
}
//NOTE: WiFiCtrl class is a wrapper I wrote to extend the WiFi class. It basically handles events.
//Our order of operation is that we run a WiFi Scan on boot, which we save for later use. After the scan is complete, we connect to WiFi or setup a SoftAP (not shown).
void WiFiCtrlClass::StartEventHandler(void) {
if(eventhandler_id) return;
eventhandler_id = WiFi.onEvent(WiFiEvent);
}
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
....
//WiFi
else if(event==ARDUINO_EVENT_WIFI_SCAN_DONE) {
Serial.printf("%d APs found\r\n", info.wifi_scan_done.number);
//Serial.println("--Start WiFi--"); //Start WiFi after scan completes
WiFiCtrl.ConfigWiFi();
}
}
void WiFiCtrlClass::ConfigWiFi(void)
{
WiFi.persistent(false); //Base library should NOT save SSID/pwd to NVS. This also prevents wifi autostart.
WiFi.setAutoReconnect(true);
//WiFi.mode(WIFI_MODE_NULL); //Bugfix: hostname won't be set if STA mode is already active, so shut down WiFi.
//WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE); //Clearing IP config could be necessary to get WiFi hostname to work
log_d("Setting WiFi hostname to '%s'", MF.DeviceName);
WiFi.setHostname(MF.DeviceName); //Hostname isn't set until WiFi.mode() is called, and only if STA mode wasn't active.
MacAddress oBssid = MacAddress(netconf.wlan.bssidval);
log_v("Start STA BSSID='%s' SSID='%s', PASS='%s'",
oBssid.Value() ? oBssid.toString().c_str() : "",
netconf.wlan.ssid,
netconf.wlan.password);//TODO test this
if(!oBssid.Value()) {
WiFi.begin(netconf.wlan.ssid, netconf.wlan.password);
}
else {
uint8_t bssid[6];
oBssid.toBytes(bssid);
WiFi.begin(netconf.wlan.ssid, netconf.wlan.password, 0, bssid, true);//TODO test this
}
}
Debug Message
Discovering device name using Fing (Android App) or Win10 Command prompt "nslookup 192.168.0.xxx" shows the wrong hostname.
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
- I confirm I have checked existing issues, online documentation and Troubleshooting guide.
Edit: removed comment in code example about DNS and Netbios, as that is confusing the issue. Use MDNS for Netbios lookups.