Skip to content

Simplify "device name" across app #1242

Open

Description

@tablatronix

I do not know if the server description is anything special but it would be nice to use a single Device ID
to set the following

  • Server name (title)
  • mdns hostname
  • apname

Would make config easier

Activity

  1. huggy-d1 commented on Oct 13, 2020

    @huggy-d1
    Contributor

    I have been wondering why there are 3 options for a name.
    Maybe a checkbox [ ] Single host / ap / server name where "Single" gets replaced by "Different" once it is selected. The implication is the default setting is [Single] rather than [Different] Host / AP / Server Names

  2. tablatronix commented on Oct 14, 2020

    @tablatronix
    Author

    Or just a checkbox for each input [use device name] for apname and hostname fields

  3. mateuszdrab commented on Dec 24, 2024

    @mateuszdrab

    I actually would like to keep the Server Name separate from the DNS/DHCP hostname.
    Having the MDNS and DNS/DHCP names identical (without the .local of course) makes more sense.
    The Server Name which could be seen as a Friendly Name is usually longer and gets truncated badly in a character limited DNS name.
    AP name could be inherited from either, probably from the Server Name.

  4. netmindz commented on Dec 26, 2024

    @netmindz
    Member

    You definitely want to retain the ability for the AP name to have the mac address based suffix

  5. mateuszdrab commented on Dec 26, 2024

    @mateuszdrab

    You definitely want to retain the ability for the AP name to have the mac address based suffix

    Yup, same with hostname, all I need is that name to be equal to the mdns hostname.

  6. blazoncek commented on Jun 6, 2025

    @blazoncek
    Contributor

    Let me revive tis issue (in regards to #4586).

    I was looking at the code for mDNS and hostname and did find it a bit odd.

    if (strcmp(cmDNS, DEFAULT_MDNS_NAME) == 0) sprintf_P(cmDNS, PSTR("wled-%*s"), 6, escapedMac.c_str() + 6);

    WLED/wled00/wled.cpp

    Lines 658 to 670 in a87b562

    // convert the "serverDescription" into a valid DNS hostname (alphanumeric)
    char hostname[25];
    prepareHostname(hostname);
    WiFi.begin(multiWiFi[selectedWiFi].clientSSID, multiWiFi[selectedWiFi].clientPass); // no harm if called multiple times
    #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

    WLED/wled00/cfg.cpp

    Lines 59 to 61 in a87b562

    JsonObject id = doc["id"];
    getStringFromJson(cmDNS, id[F("mdns")], 33);
    getStringFromJson(serverDescription, id[F("name")], 33);

    I would assume most people would want 2 names: mDNS/DHCP/hostname and device name/UI name.

    Currently WLED also stores 2 names cmDNS and serverDescription but their use is a bit odd. cmDNS is used for mDNS but not hostname (which is associated with DHCP registration). Instead serverDescription is used as hostname/DHCP registration and UI device name. It is also used in Improv and E1.31.

    I think cmDNs (a field in WiFi settings) should be used for hostname and have serverDescription(field in UI settings) used only for UI/Improv/E1.31.

    Another oddity is how current hostname is constructed from serverDescription.

    WLED/wled00/util.cpp

    Lines 118 to 139 in a87b562

    void prepareHostname(char* hostname)
    {
    sprintf_P(hostname, PSTR("wled-%*s"), 6, escapedMac.c_str() + 6);
    const char *pC = serverDescription;
    unsigned pos = 5; // keep "wled-"
    while (*pC && pos < 24) { // while !null and not over length
    if (isalnum(*pC)) { // if the current char is alpha-numeric append it to the hostname
    hostname[pos] = *pC;
    pos++;
    } else if (*pC == ' ' || *pC == '_' || *pC == '-' || *pC == '+' || *pC == '!' || *pC == '?' || *pC == '*') {
    hostname[pos] = '-';
    pos++;
    }
    // else do nothing - no leading hyphens and do not include hyphens for all other characters.
    pC++;
    }
    //last character must not be hyphen
    if (pos > 5) {
    while (pos > 4 && hostname[pos -1] == '-') pos--;
    hostname[pos] = '\0'; // terminate string (leave at least "wled")
    }
    }

    As for AP name, IMO that should be independent from anything else.

    Should we change how hostname/mDNS/DHCP name is constructed?

  7. willmmiles commented on Jun 6, 2025

    @willmmiles
    Member

    I was looking at the code for mDNS and hostname and did find it a bit odd.
    Should we change how hostname/mDNS/DHCP name is constructed?

    Oh good, it's not just me - I was definitely surprised that the mDNS name and DHCP hostname were different!

    I agree that we should have 3 settings:

    • hostname (mDNS and DHCP; character set restricted to what's allowable for DNS)
    • UI display name
    • AP name

    In a clean-sheet design I would suggest:

    • If unset, hostname defaults to WLED-abcdef, where abcdef are the last three bytes of the MAC address;
    • If unset, UI display name defaults to the hostname;
    • If unset, AP name defaults to either hostname or WLED-AP (compile time option)

    ..and for the latter two cases, we store "unset" explicitly (perhaps as an empty string), so it will functionally track changes to the hostname.

    I'd also suggest putting the hostname box at the top of the network settings page, before the wifi SSIDs. During initial setup, it makes it easy to see (or change) how you'll refer to your device before we lose network connectivity to it. (I'll admit that I have one long-lived WLED board on my test network with no LEDs actually connected that I principally use as a way to find new boards via the nodes list after I join them to the wifi!)

    This would let us leverage the hostname as a "device name" in the default/common state.

  8. blazoncek commented on Jun 6, 2025

    @blazoncek
    Contributor

    Like so?

    // prepare a unique hostname based on the last 6 digits of the MAC address
    // if no mDNS name or serverDescription is set, otherwise use cmDNS or serverDescription
    // the hostname will be at most 24 characters long, starting with "wled-"
    // and containing only alphanumeric characters and hyphens
    // the hostname will not end with a hyphen and will be null-terminated
    void prepareHostname(char* hostname)
    {
      // create a unique hostname based on the last 6 digits of the MAC address if no mDNS name or serverDescription is set
      sprintf_P(hostname, PSTR("wled-%*s"), 6, escapedMac.c_str() + 6);
      const char *pC = cmDNS;   // use cmDNS as hostname if set
      if (strlen(pC) == 0) pC = serverDescription;  // else use serverDescription
      unsigned pos = 5;         // keep "wled-" from unique name
      while (*pC && pos < 24) { // while !null and not over length
        if (isalnum(*pC)) {     // if the current char is alpha-numeric append it to the hostname
          hostname[pos++] = *pC;
        } else if (*pC == ' ' || *pC == '_' || *pC == '-' || *pC == '+' || *pC == '!' || *pC == '?' || *pC == '*') {
          hostname[pos++] = '-';
        }
        // else do nothing - no leading hyphens and do not include hyphens for all other characters.
        pC++;
      }
      // last character must not be hyphen
      while (pos > 4 && hostname[pos-1] == '-') pos--;
      hostname[pos] = '\0'; // terminate string (leave at least "wled")
    }
  9. willmmiles commented on Jun 6, 2025

    @willmmiles
    Member

    I guess I was proposing scrapping the use of serverDescription in hostnames altogether. I was thinking something more like:

    void prepareHostname(char* hostname, size_t max_len)
    {
        if (strlen(cmDNS)) {
            strncpy(hostname, max_len, cmDNS);
        } else {
            snprintf_P(hostname, max_len, PSTR("wled-%*s"), 6, escapedMac.c_str() + 6);
        }
    }
  10. blazoncek commented on Jun 7, 2025

    @blazoncek
    Contributor

    cmDNS would still need cleaning which is not done currently.

  11. willmmiles commented on Jun 7, 2025

    @willmmiles
    Member

    Good point; I'd assumed that would be done in deserialzeJson().

  12. added a commit that references this issue on Jun 27, 2025
    e2edd38
  13. added a commit that references this issue on Aug 29, 2025
    2fecfb7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions