[GH-ISSUE #527] Request: Configure hostname #439

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

Originally created by @gepd on GitHub (Feb 18, 2018).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/527

From #513
Current workaround (I've only test it on esp32)

void WiFiEvent(WiFiEvent_t event){
    switch(event) {
        case SYSTEM_EVENT_AP_START:
            WiFi.softAPsetHostname(HOSTNAME);
            break;
        case SYSTEM_EVENT_STA_START:
            WiFi.setHostname(HOSTNAME);
            break;
        default:
            break;
    }
}

void setup(){
    WiFi.disconnect(true);
    WiFi.onEvent(WiFiEvent);
    WiFi.begin();

// checks connection and perform a call to the config portal if it's neccessary
}
Originally created by @gepd on GitHub (Feb 18, 2018). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/527 From #513 Current workaround (I've only test it on esp32) ```cpp void WiFiEvent(WiFiEvent_t event){ switch(event) { case SYSTEM_EVENT_AP_START: WiFi.softAPsetHostname(HOSTNAME); break; case SYSTEM_EVENT_STA_START: WiFi.setHostname(HOSTNAME); break; default: break; } } void setup(){ WiFi.disconnect(true); WiFi.onEvent(WiFiEvent); WiFi.begin(); // checks connection and perform a call to the config portal if it's neccessary } ```
kerem 2026-02-28 01:25:17 +03:00
Author
Owner

@tablatronix commented on GitHub (Feb 18, 2018):

I don't really understand how hostname on esp works, is this mdns hostnames?
Does it use local.* ?

<!-- gh-comment-id:366529099 --> @tablatronix commented on GitHub (Feb 18, 2018): I don't really understand how hostname on esp works, is this mdns hostnames? Does it use local.* ?
Author
Owner

@tablatronix commented on GitHub (Feb 18, 2018):

Could you set softaphostname in the _apcallback and sta after autoconnect success also?
Does this need to be in core?

<!-- gh-comment-id:366530705 --> @tablatronix commented on GitHub (Feb 18, 2018): Could you set softaphostname in the _apcallback and sta after autoconnect success also? Does this need to be in core?
Author
Owner

@tablatronix commented on GitHub (Feb 18, 2018):

so these seem to be dhcp client ids maybe ?

I still cannot figure out when to set these, some says before , some says after ap and sta are enabled.

<!-- gh-comment-id:366532084 --> @tablatronix commented on GitHub (Feb 18, 2018): so these seem to be dhcp client ids maybe ? I still cannot figure out when to set these, some says before , some says after ap and sta are enabled.
Author
Owner

@tablatronix commented on GitHub (Feb 18, 2018):

I am setting before and after for sta, well see which ones work for which chip, and adjust as needed.
Obviously we can only set after if already connected etc. ( I added a reconnect to handle this scenario)

Any tips on how to test this ?
my router shows my esp32 dhcp client name correctly, so that works.

  • test esp32 ap hostname
  • test esp32 sta hostname
  • test esp8266 sta hostname
  • test sta static ip
  • test ap static ip

Must also test with wifi.config since it stop and start dhcp, make sure hostname still works with static ips

Using the same host name for both, not sure if we need to differentiate at this point

<!-- gh-comment-id:366536409 --> @tablatronix commented on GitHub (Feb 18, 2018): I am setting before and after for sta, well see which ones work for which chip, and adjust as needed. Obviously we can only set after if already connected etc. ( _I added a reconnect to handle this scenario_) Any tips on how to test this ? my router shows my esp32 dhcp client name correctly, so that works. - [ ] test esp32 ap hostname - [ ] test esp32 sta hostname - [ ] test esp8266 sta hostname - [ ] test sta static ip - [ ] test ap static ip **Must also test with wifi.config since it stop and start dhcp, make sure hostname still works with static ips** Using the same host name for both, not sure if we need to differentiate at this point
Author
Owner

@gepd commented on GitHub (Feb 18, 2018):

Yes, it's dhcp client ids

I've tried:

WiFi.begin();
WiFi.setHostname("name");

But it makes the device crash, but if you reverse it, it works

WiFi.setHostname("name");
WiFi.begin();

I took that piece of code from this example, it sets AP and STA:
https://github.com/copercini/esp32-iot-examples/blob/master/WiFi_nonBlocking/WiFi_nonBlocking.ino

Could you set softaphostname in the _apcallback and sta after autoconnect success also?
Does this need to be in core?

I'm not sure. Normally it need to be done before WiFi.begin But the flag SYSTEM_EVENT_AP_START make me doubt where it's performed

<!-- gh-comment-id:366537110 --> @gepd commented on GitHub (Feb 18, 2018): Yes, it's dhcp client ids I've tried: ```cpp WiFi.begin(); WiFi.setHostname("name"); ``` But it makes the device crash, but if you reverse it, it works ```cpp WiFi.setHostname("name"); WiFi.begin(); ``` I took that piece of code from this example, it sets AP and STA: https://github.com/copercini/esp32-iot-examples/blob/master/WiFi_nonBlocking/WiFi_nonBlocking.ino > Could you set softaphostname in the _apcallback and sta after autoconnect success also? Does this need to be in core? I'm not sure. Normally it need to be done before `WiFi.begin` But the flag `SYSTEM_EVENT_AP_START` make me doubt where it's performed
Author
Owner

@tablatronix commented on GitHub (Feb 18, 2018):

I guess we could just use events also, but thats one more thing that could introduce bugs if its not needed. Well see

events do not work unless we can set them up before sta is enabled, which is going to be near impossible with autoconnect, and even then we need to know hostname very early, so store it, then we need to read it before start_sta, right now this is ok because autostart is not implemented yet, but it will be, I think setting it and restarting is the best most stable solution for now, although this might create issues with routers that cache the names, we will have to see, and events are also a pain , because we need to have access to the class in the event call backs to get the hostnames

<!-- gh-comment-id:366542199 --> @tablatronix commented on GitHub (Feb 18, 2018): I guess we could just use events also, but thats one more thing that could introduce bugs if its not needed. Well see _events do not work unless we can set them up before sta is enabled, which is going to be near impossible with autoconnect, and even then we need to know hostname very early, so store it, then we need to read it before start_sta, right now this is ok because autostart is not implemented yet, but it will be, I think setting it and restarting is the best most stable solution for now, although this might create issues with routers that cache the names, we will have to see, and events are also a pain , because we need to have access to the class in the event call backs to get the hostnames_
Author
Owner

@gepd commented on GitHub (Feb 19, 2018):

I've tested AP and STA in esp32 and both cases are working

Just note if you are in STA mode, STA hostname will be what you had set, and STA will be espressif and if you are in AP will be the opposite.

You can check this from the Info section

<!-- gh-comment-id:366567279 --> @gepd commented on GitHub (Feb 19, 2018): I've tested AP and STA in esp32 and both cases are working Just note if you are in STA mode, STA hostname will be what you had set, and STA will be `espressif` and if you are in AP will be the opposite. You can check this from the `Info` section
Author
Owner

@tablatronix commented on GitHub (Feb 19, 2018):

ok I think i have a decent working solution now

<!-- gh-comment-id:366573100 --> @tablatronix commented on GitHub (Feb 19, 2018): ok I think i have a decent working solution now
Author
Owner

@tablatronix commented on GitHub (Feb 19, 2018):

I am just setting hostname, and reconnecting for now if already connected, else it works fine normally.
Alternative is to restart the dhcp server, but I do not think that is exposed at all.

sta hostnames confirmed with dhcp wireshark
ip.src == 0.0.0.0 dhcp hostname as column

<!-- gh-comment-id:366573904 --> @tablatronix commented on GitHub (Feb 19, 2018): I am just setting hostname, and reconnecting for now if already connected, else it works fine normally. Alternative is to restart the dhcp server, but I do not think that is exposed at all. sta hostnames confirmed with dhcp wireshark `ip.src == 0.0.0.0` dhcp hostname as column
Author
Owner

@gepd commented on GitHub (Feb 19, 2018):

I've just check it and there is not problem in STA (both names are the same) but not in AP

<!-- gh-comment-id:366577828 --> @gepd commented on GitHub (Feb 19, 2018): I've just check it and there is not problem in STA (both names are the same) but not in AP
Author
Owner

@tablatronix commented on GitHub (Feb 19, 2018):

Esp8266 has no apname config
I have not tested esp32 aphostnames yet.

It is nor working at all?

<!-- gh-comment-id:366682817 --> @tablatronix commented on GitHub (Feb 19, 2018): Esp8266 has no apname config I have not tested esp32 aphostnames yet. It is nor working at all?
Author
Owner

@gepd commented on GitHub (Feb 19, 2018):

When you are in STA
STA Hostname: newname
AP Hostname: newname

In AP:
AP hostname: newname
STA hostname: espressif

is not a big deal, but if it's fixed would be good

<!-- gh-comment-id:366696125 --> @gepd commented on GitHub (Feb 19, 2018): When you are in STA STA Hostname: `newname` AP Hostname: `newname` In AP: AP hostname: `newname` STA hostname: `espressif` is not a big deal, but if it's fixed would be good
Author
Owner

@tablatronix commented on GitHub (Feb 19, 2018):

which board?

<!-- gh-comment-id:366700175 --> @tablatronix commented on GitHub (Feb 19, 2018): which board?
Author
Owner

@tablatronix commented on GitHub (Feb 19, 2018):

how do you test ap hostname ? I dont seem to see it coming across in wirreshark, says "server hostname not given"

<!-- gh-comment-id:366720027 --> @tablatronix commented on GitHub (Feb 19, 2018): how do you test ap hostname ? I dont seem to see it coming across in wirreshark, says "server hostname not given"
Author
Owner

@tablatronix commented on GitHub (Feb 19, 2018):

Might want to give it a try again.

On esp32 ap, info shows both newname, I have to confirm via network of course.

<!-- gh-comment-id:366738150 --> @tablatronix commented on GitHub (Feb 19, 2018): Might want to give it a try again. On esp32 ap, info shows both `newname`, I have to confirm via network of course.
Author
Owner

@gepd commented on GitHub (Feb 19, 2018):

I'm working with a ESP32

Now both cases are working.
I checked that from my router, and also from the Info page of WM

First I start the portal and check the Info page (No WiFi connection) then I connect the device to a network and check it again.

Now it's working as spected.
I don't have an esp8266 here to test it

<!-- gh-comment-id:366759568 --> @gepd commented on GitHub (Feb 19, 2018): I'm working with a ESP32 Now both cases are working. I checked that from my router, and also from the `Info` page of WM First I start the portal and check the Info page (No WiFi connection) then I connect the device to a network and check it again. Now it's working as spected. I don't have an esp8266 here to test it
Author
Owner

@tablatronix commented on GitHub (Feb 19, 2018):

I still cannot figure out what the aphostname actually does. Shrug,

<!-- gh-comment-id:366769703 --> @tablatronix commented on GitHub (Feb 19, 2018): I still cannot figure out what the aphostname actually does. Shrug,
Author
Owner

@gepd commented on GitHub (Feb 19, 2018):

I don't know either, for me it has not sense to change it, but as it's displayed in the info page, is a little weird to read two hostnames with different names

<!-- gh-comment-id:366776725 --> @gepd commented on GitHub (Feb 19, 2018): I don't know either, for me it has not sense to change it, but as it's displayed in the info page, is a little weird to read two hostnames with different names
Author
Owner

@bitboy85 commented on GitHub (Jun 22, 2019):

I would also like to have this feature. Makes it more easy to open webserver by name instead of ip address. As far as i know hostname is only in station mode supported and if i remember correctly there were some issues with hostname and the esp8266 arduino library.
Can you please check with an updated one?

<!-- gh-comment-id:504620436 --> @bitboy85 commented on GitHub (Jun 22, 2019): I would also like to have this feature. Makes it more easy to open webserver by name instead of ip address. As far as i know hostname is only in station mode supported and if i remember correctly there were some issues with hostname and the esp8266 arduino library. Can you please check with an updated one?
Author
Owner

@tablatronix commented on GitHub (Jun 22, 2019):

This feature already exists.. Did you read this?

<!-- gh-comment-id:504665229 --> @tablatronix commented on GitHub (Jun 22, 2019): This feature already exists.. Did you read this?
Author
Owner

@bitboy85 commented on GitHub (Jun 22, 2019):

What i had in mind is if using autoconfiguration that you can set a hostname in the "Configure Wifi" menu. Which is also saved in flash and restored during power up.
Currently its only working if hardcoded or did i miss something?

<!-- gh-comment-id:504671293 --> @bitboy85 commented on GitHub (Jun 22, 2019): What i had in mind is if using autoconfiguration that you can set a hostname in the "Configure Wifi" menu. Which is also saved in flash and restored during power up. Currently its only working if hardcoded or did i miss something?
Author
Owner

@tablatronix commented on GitHub (Jun 22, 2019):

yes you are correct, anything that uses flash or spiffs to store stuff has to be written by users on their own using params, wifimanager does not write to flash memory on its own by design

<!-- gh-comment-id:504701163 --> @tablatronix commented on GitHub (Jun 22, 2019): yes you are correct, anything that uses flash or spiffs to store stuff has to be written by users on their own using params, wifimanager does not write to flash memory on its own by design
Author
Owner

@markg85 commented on GitHub (Jul 8, 2019):

I too ended up here after having hacked this very feature in my local WiFiManager along with mDSN support.

Just to figure out that the feature is there all along, just in the development branch... Which you obviously don't get when using PlatformIO..

Could you consider making a new release, please? These two features alone would already be welcome enough for a release.

<!-- gh-comment-id:509354942 --> @markg85 commented on GitHub (Jul 8, 2019): I too ended up here after having hacked this very feature in my local WiFiManager along with mDSN support. Just to figure out that the feature is there all along, just in the development branch... Which you obviously don't get when using PlatformIO.. Could you consider making a new release, please? These two features alone would already be welcome enough for a release.
Author
Owner

@tablatronix commented on GitHub (Jul 8, 2019):

I will accept PR for examples on development if anyone want to write them though.

I hope to release it (beta) soon, sorry about that you can use it on platformio, you can install anything on platformio using git urls

<!-- gh-comment-id:509371837 --> @tablatronix commented on GitHub (Jul 8, 2019): I will accept PR for examples on development if anyone want to write them though. I hope to release it (beta) soon, sorry about that you can use it on platformio, you can install anything on platformio using git urls
Author
Owner

@markg85 commented on GitHub (Jul 8, 2019):

I will accept PR for examples on development if anyone want to write them though.

I hope to release it (beta) soon, sorry about that you can use it on platformio, you can install anything on platformio using git urls

Yes, that will work. But then i lose the update feature... This is only a good solution as long as you haven't made a release. As soon as you do, it requires me (and all who used git clone) to modify it again.

<!-- gh-comment-id:509375824 --> @markg85 commented on GitHub (Jul 8, 2019): > I will accept PR for examples on development if anyone want to write them though. > > I hope to release it (beta) soon, sorry about that you can use it on platformio, you can install anything on platformio using git urls Yes, that will work. But then i lose the update feature... This is only a good solution as long as you haven't made a release. As soon as you do, it requires me (and all who used git clone) to modify it again.
Author
Owner

@tablatronix commented on GitHub (Jul 9, 2019):

Can you not override libraries in platformio.ini? I have not tried yet

<!-- gh-comment-id:509442798 --> @tablatronix commented on GitHub (Jul 9, 2019): Can you not override libraries in platformio.ini? I have not tried yet
Author
Owner

@tablatronix commented on GitHub (Jul 9, 2019):

Yeah you just add lib deps and use the custom branch
https://docs.platformio.org/en/latest/librarymanager/quickstart.html#project-dependencies

<!-- gh-comment-id:509455405 --> @tablatronix commented on GitHub (Jul 9, 2019): Yeah you just add lib deps and use the custom branch https://docs.platformio.org/en/latest/librarymanager/quickstart.html#project-dependencies
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#439
No description provided.