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:



Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our newsletter!

4 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

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.