Esp32

Set ESP32 Custom Hostname Using Arduino IDE

ESP32 comes with a WiFi module that can connect to different wireless networks. ESP32 opens the door to the IoT world and designs custom wireless-based projects. In wireless networks a hostname is used for identification of devices. Here in this guide, we will check the default hostname of ESP32 and write a code to assign a custom new hostname to ESP32.

Setting a Custom Hostname

Host name is a label that is given to a device when it is connected inside a network. Hostname helps to identify devices so it can be managed easily and differentiated other similar devices from it.

Once ESP32 is connected inside a wireless network such as a WiFi router access point it shows a label that helps to identify it among other devices. We can edit this host name inside the Arduino code.

Why We Need Custom Hostname

The reason we need a custom hostname is because when there are multiple similar devices connected to the same access point it will be difficult for finding a specific device as by default all of these have the same hostname. So, to differentiate between similar devices a custom hostname can be used.

Checking ESP32 Default Hostname

Before we assign a custom hostname first, we will check it using the ESP32 code.

Code

Connect ESP32 board with COM port of PC. Open Arduino IDE and upload the given code in ESP32.

#include "WiFi.h"    /*WiFi Library included*/
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
void setup(){
  Serial.begin(115200);   /*Serial communication baud rate defined*/
  WiFi.begin(ssid, password); /*WiFi begin*/
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println(WiFi.localIP());    /*Connected WiFi Network IP address*/
  Serial.println(WiFi.getHostname()); /*ESP32 Host name printed*/
}
void loop(){}

This code will connect ESP32 to a WiFi network and print the local IP address and current host name of ESP32 board.

Output

Once code is uploaded and ESP32 is connected to an access point we can see the IP address assigned to it by an access point on the serial monitor. After that code printed the current hostname which in our case is esp32-4B3B20.

We have successfully checked the current hostname of ESP32. Now we will assign a custom hostname. We have successfully checked the current hostname of esp32

Assigning a Custom Hostname to ESP32

To assign a custom host name to ESP32 we assign a new name to a string and then using the WiFi.setHostname() function assign the string value to the ESP32 board. For assigning a custom hostname to ESP32 it must be connected with an access point.

Code

Connect ESP32 with COM port and upload given code.

#include <WiFi.h>  /*WiFi library included*/
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
String hostname = "ESP32 Linuxhint.com";  /*New Host name defined*/
void initWiFi() {
  WiFi.mode(WIFI_STA);   /*ESP32 station mode defined*/
  WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
  WiFi.setHostname(hostname.c_str()); /*ESP32 hostname set*/
  WiFi.begin(ssid, password);  /*WiFi connection begin*/
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());   /*IP address printed*/
}
void setup() {
  Serial.begin(115200);
  initWiFi();
   Serial.print("ESP32 NEW HOSTNAME: ");
  Serial.println(WiFi.getHostname());   /*New Hostname printed*/
}
void loop() {
}

This code will first take an SSID and password to connect ESP32 with a wireless network. Next using the string hostname = “ESP32 Linuxhint.com” we assigned this name to ESP32.

WiFi mode function will enable the ESP32 WiFi. After that using the function   WiFi.setHostname(hostname.c_str()) a new hostname is assigned which is defined inside the string.

Once the new name is assigned code will print both the local IP address assigned by access point to ESP32 and the new host name.

Output

Output on the serial monitor shows us the new assigned hostname.

Conclusion

Hostname is a kind of identification name assigned to ESP32 inside a wireless network. It helps to identify a specific device from other similar devices. By default, most of the ESP32 with the same model version have the same hostname. So, it is difficult to differentiate between them when more than one ESP32 is connected in the same network. However, using a custom hostname, we can easily identify any of the ESP32 devices. Read more in this article.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.