Setting station hostname

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Setting station hostname

Post by chrisgp » Sun Oct 30, 2016 5:18 pm

I was looking a bit at DNS with the ESP8266 and came across this post requesting the ability to change the device's hostname. The work involved didn't look too bad so I went ahead and implemented something (see this commit in my fork).

I wanted to get feedback to see if others thought this might be worthy of opening a pull request for.

It exposes it as a dhcp_hostname parameter in network.config, so network.WLAN(network.STA_IF).config('dhcp_hostname') returns the current hostname and network.config(hostname='my-custom-hostname') will set it. Unlike the other parameters it isn't persisted across boots, but as far as I could tell this is just due to how the ESP SDK call works. Another downside is that setting the hostname has to happen before a DHCP address is requested (makes sense), so you have to reset the station connection after changing it.

Here's an example terminal session demoing how it works:

Code: Select all

>>> import network
>>> 
>>> sta = network.WLAN(network.STA_IF)
>>> 
>>> sta.config('dhcp_hostname')
'ESP_F4B4B3'
>>> 
>>> 
>>> sta.config(dhcp_hostname="my-custom-hostname")
>>> sta.config('dhcp_hostname')
'my-custom-hostname'
>>>
>>> ap = network.WLAN(network.AP_IF)
>>>
>>> ap.config('dhcp_hostname')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: STA required
>>> ap.config(dhcp_hostname='xyz')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: STA required
The device should show up in your router's client list with the specified hostname (before and after pictures):
Screen Shot 2016-10-30 at 11.52.20 AM.png
Screen Shot 2016-10-30 at 11.52.20 AM.png (30.14 KiB) Viewed 3392 times
Screen Shot 2016-10-30 at 11.53.33 AM.png
Screen Shot 2016-10-30 at 11.53.33 AM.png (31.28 KiB) Viewed 3392 times
and if you're using your router as a DNS Server it will probably support name resolution as mine does:

Code: Select all

$ ping my-custom-hostname
PING my-custom-hostname (192.168.2.119): 56 data bytes
64 bytes from 192.168.2.119: icmp_seq=0 ttl=255 time=86.867 ms
64 bytes from 192.168.2.119: icmp_seq=1 ttl=255 time=2.482 ms
64 bytes from 192.168.2.119: icmp_seq=2 ttl=255 time=19.910 ms
And some additional reference material from investigating it:
Docs showing how the same functionality works with Node MCU (Lua) firmware on ESP8266: gethostname, sethostname
Source code for Node MCU of relevant functions: gethostname, sethostname

[Update: Edited to reflect the parameter changed from hostname to dhcp_hostname]
Last edited by chrisgp on Tue Nov 08, 2016 9:40 pm, edited 2 times in total.

User avatar
ernitron
Posts: 84
Joined: Fri Jun 03, 2016 5:53 pm
Location: The Netherlands

Re: Setting station hostname

Post by ernitron » Sun Oct 30, 2016 7:53 pm

Yes. Definitely in favor!

chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Re: Setting station hostname

Post by chrisgp » Mon Oct 31, 2016 5:42 pm

I opened a pull request and there is currently a request for any feedback on the desired name of the parameter (if it should stay as hostname or switch to something indicating that it is specifically referring to the DHCP hostname). If you have any feedback feel free to add a comment to the pull request.

joehunt588
Posts: 26
Joined: Wed Jul 27, 2016 5:06 am

Re: RE: Setting station hostname

Post by joehunt588 » Sun Nov 06, 2016 8:57 am

chrisgp wrote:I was looking a bit at DNS with the ESP8266 and came across this post requesting the ability to change the device's hostname. The work involved didn't look too bad so I went ahead and implemented something (see this commit in my fork).

I wanted to get feedback to see if others thought this might be worthy of opening a pull request for.

It exposes it as a hostname parameter in network.config, so network.WLAN(network.STA_IF).config('hostname') returns the current hostname and network.config(hostname='my-custom-hostname') will set it. Unlike the other parameters it isn't persisted across boots, but as far as I could tell this is just due to how the ESP SDK call works. Another downside is that setting the hostname has to happen before a DHCP address is requested (makes sense), so you have to reset the station connection after changing it.

Here's an example terminal session demoing how it works:

Code: Select all

>>> import network
>>> 
>>> sta = network.WLAN(network.STA_IF)
>>> 
>>> sta.config('hostname')
'ESP_F4B4B3'
>>> 
>>> 
>>> sta.config(hostname="my-custom-hostname")
>>> sta.config('hostname')
'my-custom-hostname'
>>>
>>> ap = network.WLAN(network.AP_IF)
>>>
>>> ap.config('hostname')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: STA required
>>> ap.config(hostname='xyz')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: STA required
The device should show up in your router's client list with the specified hostname (before and after pictures):
Screen Shot 2016-10-30 at 11.52.20 AM.png
Screen Shot 2016-10-30 at 11.53.33 AM.png
and if you're using your router as a DNS Server it will probably support name resolution as mine does:

Code: Select all

$ ping my-custom-hostname
PING my-custom-hostname (192.168.2.119): 56 data bytes
64 bytes from 192.168.2.119: icmp_seq=0 ttl=255 time=86.867 ms
64 bytes from 192.168.2.119: icmp_seq=1 ttl=255 time=2.482 ms
64 bytes from 192.168.2.119: icmp_seq=2 ttl=255 time=19.910 ms
And some additional reference material from investigating it:
Docs showing how the same functionality works with Node MCU (Lua) firmware on ESP8266: gethostname, sethostname
Source code for Node MCU of relevant functions: gethostname, sethostname
Hai chrisgp ,how you change the hostname,? I even follow u method call sta.config('hostname') i got error unknown config param

Sent from my ASUS_T00J using Tapatalk

chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Re: RE: Setting station hostname

Post by chrisgp » Sun Nov 06, 2016 6:18 pm

joehunt588 wrote:Hai chrisgp ,how you change the hostname,? I even follow u method call sta.config('hostname') i got error unknown config param
The changes aren't in the main firmware build yet. I opened a request to bring in the changes but that is currently pending so it's not in the master branch yet. Unless there is additional feedback it looks like it might be available in the 1.8.6 version of the firmware.

joehunt588
Posts: 26
Joined: Wed Jul 27, 2016 5:06 am

Re: RE: Setting station hostname

Post by joehunt588 » Mon Nov 07, 2016 5:42 am

The changes aren't in the main firmware build yet. I opened a request to bring in the changes but that is currently pending so it's not in the master branch yet. Unless there is additional feedback it looks like it might be available in the 1.8.6 version of the firmware.
thanks ,really need that function :D

chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Re: RE: Setting station hostname

Post by chrisgp » Tue Nov 08, 2016 9:39 pm

joehunt588 wrote:thanks ,really need that function :D
This has been merged in for the next release. The parameter has been changed from hostname to dhcp_hostname but otherwise everything else is the same.

Post Reply