Change hostname.

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Posts: 62
Joined: Wed Jun 01, 2016 3:41 pm

Re: Change hostname.

Post by markxr » Thu Sep 08, 2016 5:35 pm

The obvious thing to do is to add a new keyword argument to sta.config(... ) - it already has keyword arguments for other parameters.

The function is esp_config in modnetwork.c. It's just a matter of adding a new case: in the argument checking and calling that function wifi_station_set_hostname. Seems easy enough.

User avatar
Posts: 1
Joined: Sun Apr 16, 2017 8:56 pm

Re: Change hostname.

Post by mogul » Sun Apr 16, 2017 9:04 pm

A year later I came across the same problem, and now it is straight forward to change hostname.


Posts: 23
Joined: Mon Jan 26, 2015 2:05 pm

Re: Change hostname.

Post by MCHobby » Mon Sep 18, 2017 8:33 pm

I'm using ESP8266 under MicroPython.
I had use the default ESP8266 hostname from WebRepl and find this really convenient when using the network DHCP. I'm using the modem-router from the Internet Provider.
Using "ws://ESP_xxxx:8266" with WebRepl is really a dream.

However, from time to time this Hostname purely disappears from the network :-(
What the hell to find the new IP for WebRepl(ing).

I have seen in this thread ... n-hostname explaining that ESP8266 reboot would not republish the hostname to the DHCP server (from the last SSID/Password stored in the ESP)
So I tried to connect a wrong network then back to the right network to resend the ESP hostname with DHCP register.
Unfortunately it doesn't work as expected.

I did identify the

Code: Select all

now available in MicroPython sysname='esp8266' release='2.0.0(5a875ba)', version='v1.9.1-8-g7213e78d on 2017-06-12' but cannot get it to push the hostname to the DHCP/DNS server.

Is there an appropriate sequence of instruction to follows ?
Is this depending on my own computer resolve names? (using Linux Mint/Ubuntu/Debian, he resolve properly the name of the other computer on the network).

Any suggestion would be welcome. I'm puzzled.

Posts: 11
Joined: Sat Aug 24, 2019 8:59 am

Re: Change hostname.

Post by GordanTrevis » Thu Sep 12, 2019 2:42 pm

I'm trying to archive similar a few years later.
on ESP32/ESP8266

i tried.
befor or after conecting via wlan.conect returns OSError: TCP/IP IF Not Ready

so how is it possible to add a Cname or hostname?

User avatar
Posts: 1441
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Change hostname.

Post by jimmo » Sun Sep 29, 2019 10:19 am

hi Gordan,

Interesting... I just tried this on my ESP32 and it seemed to work fine.

Code: Select all

>>> import network
>>> sta = network.WLAN(network.STA_IF)
>>> sta.config(dhcp_hostname='tinypico')
>>> sta.config('dhcp_hostname')
>>> sta.connect('----', '----')
>>> ...
I (108190) network: GOT_IP

Code: Select all

$ ping tinypico
PING tinypico.lan ( 56(84) bytes of data.
64 bytes from tinypico.lan ( icmp_seq=1 ttl=255 time=94.2 ms
What does your connection code look like?

You need to do this before wlan.connect(), but I don't get an error no matter when I do it (even before calling .active(True) ).

Which firmware version are you using?

Posts: 11
Joined: Sat Aug 24, 2019 8:59 am

Re: Change hostname.

Post by GordanTrevis » Thu Oct 03, 2019 11:40 am

Hey, Jimmo thanks for the answer as well in my duplicate Thread.
it took me a while to answer, due to massive network issues which came upon me :lol:.

Yes, it works!

My mistake was that I not issued "" before "sta.config()". (-> Due to missing Documentation at that time)

But as mentioned in another thread, The Documentation on the Network module since then got remarkably improved!
So thanks to whoever did that.

For Interested & future readers:
It also works with the ESP8266 which connects automatically to WIFI without entering credentials every time.
"ESP8266 Store network credentials on flash, to automatically connect to known Wifi Networks after booting."

But the "sta.config(dhcp_hostname='Hostname')" will not be saved like the credentials and will be set to the default hostname eg. ESP-45234 every time after a restart of the device.

I figured that the self-issued connection of the ESP8266 happens after the script, so just add:

import network
sta = network.WLAN(network.STA_IF)

inside your ESP8266 Boot file and it will always register as "Hostname" to the network. ;)

Post Reply