ESP8266 NodeMCU Setting a Custom Hostname (Arduino IDE)

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();

ESP8266 NodeMCU Setting a Custom Hostname Arduino IDE

Setting an ESP8266 NodeMCU Hostname

The default ESP8266 hostname is ESP-XXXXXX.

Setting ESP8266 Hostname

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:
}

View raw code

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.

ESP8266 Custom Hostname Serial Monitor Arduino IDE

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.

ESP8266 Custom Hostname Arduino IDE

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:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

11 thoughts on “ESP8266 NodeMCU Setting a Custom Hostname (Arduino IDE)”

  1. 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

    Reply
    • 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.

      Reply
  2. Doesn’t work with modem Archer VR1600v, tried reboot and clearing cookies. Connections name remains ‘Unknown’ but mac and ip are correct.

    Reply
  3. 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.

    Reply
  4. 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?

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.