ESP32 Setting a Custom Hostname (Arduino IDE)

By default, the hostname of the ESP32 is espressif. In this guide, you’ll learn how to set a custom hostname for your board.

To set a custom hostname for your board, call WiFi.setHostname(YOUR_NEW_HOSTNAME); before WiFi.begin();

ESP32 Setting a Custom Hostname Arduino IDE

Setting an ESP32 Hostname

The default ESP32 hostname is espressif.

There is a method provided by the WiFi.h library that allows you to set a custom hostname.

First, start by defining your new hostname. For example:

String hostname = "ESP32 Node Temperature";

Then, call the WiFi.setHostname() function before calling WiFi.begin(). You also need to call WiFi.config() as shown below:

WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.setHostname(hostname.c_str()); //define hostname

You can copy the complete example below:

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-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 <WiFi.h>

// Replace with your network credentials (STATION)
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

String hostname = "ESP32 Node Temperature";

void initWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
  WiFi.setHostname(hostname.c_str()); //define hostname
  //wifi_station_set_hostname( hostname.c_str() );
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);
  initWiFi();
  Serial.print("RRSI: ");
  Serial.println(WiFi.RSSI());
}

void loop() {
  // put your main code here, to run repeatedly:
}

View raw code

You can use this previous snippet of code in your projects to set a custom hostname for the ESP32.

Important: you may need to restart your router for the changes to take effect.

After this, if you go to your router settings, you’ll see the ESP32 with the custom hostname.

ESP32 Custom Hostname setting Arduino IDE

Wrapping Up

In this tutorial, you’ve learned how to set up a custom hostname for your ESP32. This can be useful to identify the devices connected to your network easily. For example, if you have multiple ESP32 boards connected simultaneously, it will be easier to identify them if they have a custom hostname.

For more Wi-Fi related functions, we recommend reading the following tutorial:

We hope you’ve found this tutorial useful.

Learn more about the ESP32 with our resources:



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 »

Enjoyed this project? Stay updated by subscribing our newsletter!

10 thoughts on “ESP32 Setting a Custom Hostname (Arduino IDE)”

  1. Good idea to set a nice host name. But spaces are not a normally valid character and smaller names are better, so “esp32temp” or “officetemp” are better names. Also, skip the overly complex c++ and just use plain C like in the SSID:
    const char* hostname = “esp32temp”;
    WiFi.setHostname(hostname);

    The valid letters for hostname are a-z 0-9 and -.

    Reply
  2. I was hoping to access the esp32 via browser using the hostname

    In this case I set my hostname to “esp32test”.

    I’m able to see that the device connects to the router, and the router shows the LAN IP as well as the hostname above.

    I was hoping to access the server via browser at http://esp32test or http://esp32test.local or possibly http://esp32test.home.

    None of these work. http://<Esp32'sLanIp&gt; works fine.

    Is possible access the device using hostname?

    Reply
  3. Update: For a brief moment I got positive results from using
    WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
    WiFi.setHostname(“CustomHostname”);
    But not in the way described.

    After giving up on WiFi.setHostname() entirely, connecting without it to a static IP, a day later I noticed the ESP was accessible by both the static LAN IP and the customized hostname I’d given it.

    Haven’t been able to repeat it since, but it suggests ESP32 remembers a prior WiFi.setHostname attempt such that a subsequent WiFi.config with definitive values (as opposed to INADDR_NONE) gives you the ability to completely control your IP addresses and your hostname, both.

    Reply
  4. at least in the latest libs (2.0.3), WiFi.setHostname() needs to be called before WiFi.mode(), it is not sufficient to call it before just before WiFi.begin().

    Reply
  5. I use the following code to get a unique Host Name in the familiar form of “ESP_” plus the last 6 hex digits of the MAC address.

    Global Variable:
    char Host_Name[11];

    Local:
    byte mac[6];
    WiFi.macAddress(mac);
    sprintf(Host_Name, “ESP_%02X%02X%02X”, mac[3], mac[4], mac[5]);
    WiFi.setHostname(Host_Name);

    Reply
  6. With an ESP32-S the above mentioned solution did not work.
    However when changing the order of of code to this:
    const char* newHostname = “ESP32-Test”;
    WiFi.setHostname(newHostname); //define hostname
    WiFi.mode(WIFI_STA); // station mode
    WiFi.begin(ssid, password);

    the newHostname was accepted and reacable via ping ESP32-Test

    Reply
  7. I tried Heinz’s adaptation and this also had no effect — DHCP request still hardcodes “ESP-C54490”.

    I’m using Arduino IDE 1.8.19 with the ESP board support 2.0 (I also tried 3.0-alpha)

    Could someone post a complete sourcecode listing that works?
    Ideally I am also trying to set MAC address (MAC change works, and would negate need to restart the router). For troubleshooting purposes I just run what’s in this blog post.. and another try using code modified per Heinz’s comment.

    I could fill a page of text with all the links I found in Google about how this was broken in Espressif libraries, and then “fixed”, with people saying the problem has come back, but the Espressif Git issues are locked closed..

    Without DHCP control, I will have to substitute Raspberry Pis instead.

    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.