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
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
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]