By default, the hostname of an ESP8266 NodeMCU board is ESP-XXXXXX where the Xs represents the last six characters of its MAC address. In this tutorial, you’ll learn how to set a custom hostname for your board.
To set a custom hostname for your board, call WiFi.hostname(YOUR_NEW_HOSTNAME); before WiFi.begin();
Setting an ESP8266 NodeMCU Hostname
The default ESP8266 hostname is ESP-XXXXXX.
There is a method provided by the ESP8266WiFi.h library that allows you to set a custom hostname.
First, start by defining your new hostname. For example:
String newHostname = "ESP8266Node";
Then, call the WiFi.hostname() function before calling WiFi.begin().
WiFi.hostname(newHostname.c_str());
You can copy the complete example below:
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-set-custom-hostname-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <ESP8266WiFi.h>
// Replace with your network credentials (STATION)
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
String newHostname = "ESP8266Node";
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
//Get Current Hostname
Serial.printf("Default hostname: %s\n", WiFi.hostname().c_str());
//Set new hostname
WiFi.hostname(newHostname.c_str());
//Get Current Hostname
Serial.printf("New hostname: %s\n", WiFi.hostname().c_str());
//Init Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
}
void loop() {
// put your main code here, to run repeatedly:
}
After uploading the code to your board, open the Serial Monitor at a baud rate of 115200.
It should print the previous hostname and the new hostname.
You can use this previous snippet of code in your projects to set a custom hostname to the ESP8266 NodeMCU boards.
Important: you may need to restart your router for the changes to take effect on the router settings.
After this, if you go to your router settings, you’ll see the ESP8266 with the custom hostname.
Wrapping Up
In this tutorial, you’ve learned how to set up a custom hostname for your ESP8266 NodeMCU boards. This can be useful to identify the devices connected to your network easily. For example, if you have multiple boards connected simultaneously, it will be easier to identify them if they have a custom hostname.
We hope you’ve found this tutorial useful.
Learn more about the ESP8266 with our resources:
- Build ESP8266 Web Servers with Arduino IDE (eBook)
- Home Automation using ESP8266
- More ESP8266 Projects and Tutorials…
Thanks for reading.
Thanks for your useful tutorials
thank you for great tutorials
The device list in router interface may require removing browser cookie for the router to show the changes.
I copy pasted the code, tried it and it works, but when I scan the network there is a dot (.) added to the end of the hostname and I have to put that dot to access the ESP8266. Am I the only one with that issue and how can I solve it ?
Thank you
Did it work after restarting the router or just after uploading the code? I have many people working in an organization so cannot take risk to restart the router.
Doesn’t work with my ASUS router.
How can I set the hostname when using the ESP8266WiFiMulti.h library?
Doesn’t work with modem Archer VR1600v, tried reboot and clearing cookies. Connections name remains ‘Unknown’ but mac and ip are correct.
I tryed chance host name but code didnt work at my network.
I try , start wifi begin and connect network after change the network name it worked.
Thank you.
Many thanks for your useful tutorials, i learnt a lot from them.
What if i use the wifiManger library to manage the wifi connection, how can i set the custom hostname?
work like charm witch macchange also after