[GH-ISSUE #1014] Setting the hostname #862

Closed
opened 2026-02-28 01:27:23 +03:00 by kerem · 12 comments
Owner

Originally created by @fmarzocca on GitHub (Feb 26, 2020).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1014

I am wondering when is the right point to set the hostname ( WiFi.hostname(BASE_HOST_NAME);)of the esp8266 to avoid it show itself in the LAN with ESP_xxxxx?

WiFiManager wifiManager;
wifiManager.setConfigPortalTimeout(300);
if (!wifiManager.autoConnect(BASE_HOST_NAME)) {
		delay(9000);
		ESP.reset();
	}
if (WiFi.status() != WL_CONNECTED) {
		delay(3000);
		ESP.reset();
	}
Originally created by @fmarzocca on GitHub (Feb 26, 2020). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1014 I am wondering when is the right point to set the hostname ( `WiFi.hostname(BASE_HOST_NAME);`)of the esp8266 to avoid it show itself in the LAN with ESP_xxxxx? ``` WiFiManager wifiManager; wifiManager.setConfigPortalTimeout(300); if (!wifiManager.autoConnect(BASE_HOST_NAME)) { delay(9000); ESP.reset(); } if (WiFi.status() != WL_CONNECTED) { delay(3000); ESP.reset(); } ```
kerem 2026-02-28 01:27:23 +03:00
  • closed this issue
  • added the
    Question
    label
Author
Owner

@tablatronix commented on GitHub (Feb 27, 2020):

Well if you set the mode to OFF it will remember it and not auto start the ap. Thats an esp thing not a wm

<!-- gh-comment-id:591738306 --> @tablatronix commented on GitHub (Feb 27, 2020): Well if you set the mode to OFF it will remember it and not auto start the ap. Thats an esp thing not a wm
Author
Owner

@fmarzocca commented on GitHub (Feb 27, 2020):

@tablatronix , sorry but it wasn't clear to me. Which mode should I set to OFF? I need the AP the first time.... Where should I include WiFi.hostname(BASE_HOST_NAME) ??

<!-- gh-comment-id:591849689 --> @fmarzocca commented on GitHub (Feb 27, 2020): @tablatronix , sorry but it wasn't clear to me. Which mode should I set to OFF? I need the AP the first time.... Where should I include WiFi.hostname(BASE_HOST_NAME) ??
Author
Owner

@tablatronix commented on GitHub (Feb 27, 2020):

setup(){
WiFi.mode(WIFI_STA);
}

<!-- gh-comment-id:591948836 --> @tablatronix commented on GitHub (Feb 27, 2020): setup(){ WiFi.mode(WIFI_STA); }
Author
Owner

@fmarzocca commented on GitHub (Feb 27, 2020):

I am confused... how can WiFi.mode() be
able to set the device hostname?

  • Sent from my mobile phone. Sorry for typos -

Il Gio 27 Feb 2020, 13:37 Shawn A notifications@github.com ha scritto:

setup()
WiFi.mode(WIFI_OFF);


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/tzapu/WiFiManager/issues/1014?email_source=notifications&email_token=ACBCQ3U2WHLMLAVM2YORUMTRE6XXVA5CNFSM4K4JSGD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOENEGYJA#issuecomment-591948836,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ACBCQ3U27CX3GAYRUBFPX2TRE6XXVANCNFSM4K4JSGDQ
.

<!-- gh-comment-id:591953817 --> @fmarzocca commented on GitHub (Feb 27, 2020): I am confused... how can WiFi.mode() be able to set the device hostname? - Sent from my mobile phone. Sorry for typos - Il Gio 27 Feb 2020, 13:37 Shawn A <notifications@github.com> ha scritto: > setup() > WiFi.mode(WIFI_OFF); > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > <https://github.com/tzapu/WiFiManager/issues/1014?email_source=notifications&email_token=ACBCQ3U2WHLMLAVM2YORUMTRE6XXVA5CNFSM4K4JSGD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOENEGYJA#issuecomment-591948836>, > or unsubscribe > <https://github.com/notifications/unsubscribe-auth/ACBCQ3U27CX3GAYRUBFPX2TRE6XXVANCNFSM4K4JSGDQ> > . >
Author
Owner

@tablatronix commented on GitHub (Feb 27, 2020):

Your problem has nothing to do with hostname, esp starts ap by default

<!-- gh-comment-id:591963643 --> @tablatronix commented on GitHub (Feb 27, 2020): Your problem has nothing to do with hostname, esp starts ap by default
Author
Owner

@fmarzocca commented on GitHub (Feb 27, 2020):

Sorry, maybe I wasn't clear in my question.

My device is working very well with WifiManager. The first time it starts in AP mode, I set the credentials and then every time it starts again it connects to the WiFi router.
My problem is that at this point the device is present on the LAN with ESP-xxxxx hostname, while I'd like to assign it to a more specific hostname.

How to assign it the hostname and where in the sequence of setup() instructions?

<!-- gh-comment-id:591999553 --> @fmarzocca commented on GitHub (Feb 27, 2020): Sorry, maybe I wasn't clear in my question. My device is working very well with WifiManager. The first time it starts in AP mode, I set the credentials and then every time it starts again it connects to the WiFi router. My problem is that at this point the device is present on the LAN with ESP-xxxxx hostname, while I'd like to assign it to a more specific hostname. How to assign it the hostname and where in the sequence of setup() instructions?
Author
Owner

@tablatronix commented on GitHub (Feb 27, 2020):

oh are you talking about mdns hostnames? I thought you mean the ssid was showing up.
Sorry I misunderstood

If you are using autoconnect, you can set it before you call autoconnect, it doesn't get set until you call autoconnect, there have been some changes to mdns, I think you might have to service it in loop now, not sure.

<!-- gh-comment-id:592006108 --> @tablatronix commented on GitHub (Feb 27, 2020): oh are you talking about mdns hostnames? I thought you mean the ssid was showing up. Sorry I misunderstood If you are using autoconnect, you can set it before you call autoconnect, it doesn't get set until you call autoconnect, there have been some changes to mdns, I think you might have to service it in loop now, not sure.
Author
Owner

@fmarzocca commented on GitHub (Feb 27, 2020):

Ah, ok.
So this way:

WiFiManager wifiManager;
wifiManager.setConfigPortalTimeout(300);
WiFi.hostname(BASE_HOST_NAME);
if (!wifiManager.autoConnect(BASE_HOST_NAME)) {
		delay(9000);
		ESP.reset();
	}
<!-- gh-comment-id:592012394 --> @fmarzocca commented on GitHub (Feb 27, 2020): Ah, ok. So this way: ``` WiFiManager wifiManager; wifiManager.setConfigPortalTimeout(300); WiFi.hostname(BASE_HOST_NAME); if (!wifiManager.autoConnect(BASE_HOST_NAME)) { delay(9000); ESP.reset(); } ```
Author
Owner

@tablatronix commented on GitHub (Feb 27, 2020):

yup

WiFi.hostname(BASE_HOST_NAME); // <---- MDNS network hostname dhcp client name
if (!wifiManager.autoConnect(BASE_HOST_NAME)) { // <---- SOFT AP SSID

depending on the version of your libs, you probably have to add this to your loop to get hostname resolution to work properly

  // Allow MDNS processing
  MDNS.update();
<!-- gh-comment-id:592014678 --> @tablatronix commented on GitHub (Feb 27, 2020): yup WiFi.hostname(BASE_HOST_NAME); // <---- MDNS network hostname dhcp client name if (!wifiManager.autoConnect(BASE_HOST_NAME)) { // <---- SOFT AP SSID depending on the version of your libs, you probably have to add this to your loop to get hostname resolution to work properly ```C++ // Allow MDNS processing MDNS.update(); ```
Author
Owner

@fmarzocca commented on GitHub (Feb 27, 2020):

Thanks!

<!-- gh-comment-id:592016134 --> @fmarzocca commented on GitHub (Feb 27, 2020): Thanks!
Author
Owner

@tablatronix commented on GitHub (Feb 27, 2020):

Here is the code if you are curious or want to do it yourself in your code,

  if((String)_hostname != ""){
    DEBUG_WM(DEBUG_VERBOSE,"Setting hostname:",_hostname);
    bool res = true;
    #ifdef ESP8266
      res = WiFi.hostname(_hostname);
      #ifdef ESP8266MDNS_H
        DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname");
        if(MDNS.begin(_hostname)){
          MDNS.addService("http", "tcp", 80);
        }
      #endif
    #elif defined(ESP32)
      // @note hostname must be set after STA_START
      delay(200); // do not remove, give time for STA_START
      res = WiFi.setHostname(_hostname);
      #ifdef ESP32MDNS_H
        DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname");
        if(MDNS.begin(_hostname)){
          MDNS.addService("http", "tcp", 80);
        }
      #endif
    #endif
<!-- gh-comment-id:592027605 --> @tablatronix commented on GitHub (Feb 27, 2020): Here is the code if you are curious or want to do it yourself in your code, ```c++ if((String)_hostname != ""){ DEBUG_WM(DEBUG_VERBOSE,"Setting hostname:",_hostname); bool res = true; #ifdef ESP8266 res = WiFi.hostname(_hostname); #ifdef ESP8266MDNS_H DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname"); if(MDNS.begin(_hostname)){ MDNS.addService("http", "tcp", 80); } #endif #elif defined(ESP32) // @note hostname must be set after STA_START delay(200); // do not remove, give time for STA_START res = WiFi.setHostname(_hostname); #ifdef ESP32MDNS_H DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname"); if(MDNS.begin(_hostname)){ MDNS.addService("http", "tcp", 80); } #endif #endif ```
Author
Owner

@thijstriemstra commented on GitHub (Jan 22, 2025):

Suggestions above didn't work. For an ESP32 I only had to add wifiManager.setHostname("MyHostname"); in setup()

<!-- gh-comment-id:2608516285 --> @thijstriemstra commented on GitHub (Jan 22, 2025): Suggestions above didn't work. For an ESP32 I only had to add `wifiManager.setHostname("MyHostname");` in `setup()`
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#862
No description provided.