Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions wled00/wled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,17 @@ void WLED::handleConnection()
#endif
const bool wifiConfigured = WLED_WIFI_CONFIGURED;

// The DNS name of the ESP must be set before the first connection to the DHCP server. Otherwise, the esp default name, such as “esp32s3-267D0C,” will be used. GG
#ifdef ARDUINO_ARCH_ESP32
// Use cmDNS (this is the buffer that holds your name)
if (cmDNS && cmDNS[0] != '\0') {
WiFi.setHostname(cmDNS);
} else {
// Fallback to DEVICE_NAME if cmDNS is still empty
WiFi.setHostname(WLED_HOST_NAME);
}
#endif
Comment on lines +868 to +877
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Set DHCP hostname once, from a single source, before WiFi.begin().

This block sets cmDNS (or WLED_HOST_NAME) in handleConnection, but initConnection() later sets a different hostname derived from prepareHostname() after WiFi.begin(). Routers that cache the first DHCPDISCOVER will keep the earlier value, so the visible hostname can diverge from the configured device name. To avoid mismatches and repeated reconfiguration, set the hostname exactly once in initConnection() before WiFi.begin() using a single chosen source, then remove the later WiFi.setHostname() call.

🔧 Suggested fix (unify hostname source + move before WiFi.begin)
@@ void WLED::handleConnection()
-  `#ifdef` ARDUINO_ARCH_ESP32
-    // Use cmDNS (this is the buffer that holds your name)
-  if (cmDNS && cmDNS[0] != '\0') {
-    WiFi.setHostname(cmDNS);
-  } else {
-    // Fallback to DEVICE_NAME if cmDNS is still empty
-    WiFi.setHostname(WLED_HOST_NAME);
-  }
-  `#endif`
@@ void WLED::initConnection()
-    // convert the "serverDescription" into a valid DNS hostname (alphanumeric)
-    char hostname[25];
-    prepareHostname(hostname);
+    // convert the "serverDescription" into a valid DNS hostname (alphanumeric)
+    char hostname[25];
+    if (cmDNS && cmDNS[0] != '\0') {
+      strncpy(hostname, cmDNS, sizeof(hostname));
+      hostname[sizeof(hostname) - 1] = '\0';
+    } else {
+      prepareHostname(hostname);
+    }
+#ifdef ARDUINO_ARCH_ESP32
+    WiFi.setHostname(hostname); // set before WiFi.begin for DHCP Option 12
+#endif
@@
 `#ifdef` ARDUINO_ARCH_ESP32
     WiFi.setTxPower(wifi_power_t(txPower));
     WiFi.setSleep(!noWifiSleep);
-    WiFi.setHostname(hostname);
 `#else`
     wifi_set_sleep_type((noWifiSleep) ? NONE_SLEEP_T : MODEM_SLEEP_T);
     WiFi.hostname(hostname);
 `#endif`
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@wled00/wled.cpp` around lines 868 - 877, The DHCP hostname is being set in
handleConnection but initConnection later calls WiFi.begin() and sets a
different hostname from prepareHostname(), causing inconsistent hostnames; move
the hostname setup to initConnection before the call to WiFi.begin() and use a
single source (prefer cmDNS if non-empty else WLED_HOST_NAME or the result of
prepareHostname()) by replacing any WiFi.setHostname() in handleConnection with
a single WiFi.setHostname(...) in initConnection prior to WiFi.begin(), and
remove the redundant WiFi.setHostname calls so hostname is configured exactly
once (refer to symbols: initConnection, handleConnection, prepareHostname,
cmDNS, WLED_HOST_NAME, WiFi.begin, WiFi.setHostname).


// ignore connection handling if WiFi is configured and scan still running
// or within first 2s if WiFi is not configured or AP is always active
if ((wifiConfigured && multiWiFi.size() > 1 && WiFi.scanComplete() < 0) || (now < 2000 && (!wifiConfigured || apBehavior == AP_BEHAVIOR_ALWAYS)))
Expand Down
Loading