Set ESP32 Custom Hostname with Arduino IDE

By default, the hostname of ESP32 based development boards is espressif. But we can change the default hostname by using the WiFi.setHostname() function. In this tutorial, we will learn to set the custom hostname for the ESP32 board using Arduino IDE. 

ESP32 Setting a Custom Hostname (Arduino IDE)

In Arduino IDE, we can change the hostname of ESP32 by calling WiFi.setHostname(YOUR_NEW_HOSTNAME); before WiFi.begin();

Changing ESP32 Hostname 

The default hostname of ESP32 is expressif. But in case you are working with multiple ESP32 devices and at some point you want to differentiate devices by their name while using them in soft access point mode. 

For example, you have multiple nodes in your IoT-based project such as temperature, humidity, pressure, Devices control, light control node, etc. You can change the hostname of these nodes. 

As you know, the Arduino WiFi.h library for ESP32 provides all methods to configure boards in station and access point mode. 

The WiFi.h library also provides a function or method to to set custom hostname of ESP32 devices. 

Create Custom Hostname ESP32 

First define a custom name for your node using a string as follows. 

String hostname= "ESP32 Humidity Node";

Let’s say we want to set the name of ESP32 as “ESP32 Humidity Node”. Therefore, we create a string variable (hostname_hum). A variable name can be any name of your choice. 

The WiFi.setHostname() function is used to set a custom hostname. You should call this function before wiFi.begin()

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

Arduino Sketch

Copy the following sketch to Arduino and upload it to ESP32.

#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 Humidity Node";

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

Upload the above example code which will change the Hostname of ESP32 to “ESP32 Humidity Node”. After uploading code, restart your ESP32 board and click on the reset button as shown:

ESP32 enable reset button

Now open your router settings page and see the hostname assigned to ESP32. It will be changed to “ESP32 Humidity Node”.

Note: If hostname is not changing for you, in that case, you should restart your WiFi router.

If you don’t know how to check your router network settings page, you can install a Fing android application to view the hostname of your ESP32 device.

Conclusion

In short, in this tutorial, we have learned how to change the default hostname of ESP32 devices. Changing default hostname becomes handly in IoT base projects when we have multiple nodes in our IoT project and we want to identify each node with its unique hostname.

You may also like to read:

Leave a Comment