Accessing your ESP32 device on the local network using its IP address can be inconvenient, especially when the IP changes or when you have multiple ESPs. Thankfully, we can use mDNS to set a custom hostname for our devices and avoid annoying IP addresses. In this guide, I’ll show you how you can set up a custom hostname using ESP32 mDNS.
What is mDNS?
mDNS stands for Multicast DNS. It’s a protocol that allows devices to discover each other on a local network without needing a central DNS server.
Instead of typing an IP address, which can be hard to remember, you can use hostnames like simply esp32.local.
Most modern operating systems support mDNS out of the box. This makes it ideal for projects where you want to interact with your ESP32 through a browser or other network services without remembering IPs.
Configure ESP32 mDNS in Arduino IDE
The mDNS functionality is included in the ESP32 Arduino core via the ESPmDNS library. Below is an example sketch that lets your ESP32 connect to Wi-Fi and register an mDNS hostname.
#include <WiFi.h>
#include <ESPmDNS.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to Wifi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
if (!MDNS.begin("esp32")) { // will be available under esp32.local
Serial.println("Error starting mDNS");
while (1) delay(500); // stop
}
Serial.println("mDNS started. ");
}
void loop() {}
After uploading the sketch, you can test whether the custom hostname is configured correctly by pinging it from the console on your computer.
Simply open the console and enter the following command:
ping esp32.local
You should then see an output similar to this one:

Testing with a Simple Web Server
To make your ESP32 accessible in a browser, you can add a simple web server for testing. Here’s how you can extend the previous sketch:
#include <WebServer.h>
WebServer server(80); // create web server on port 80
void setup() {
// ... the same setup code as before ...
MDNS.addService("http", "tcp", 80);
server.on("/", []() {
server.send(200, "text/plain", "Hello from ESP32!");
});
server.begin();
Serial.println("Web server started!");
}
void loop() {
server.handleClient();
}
After uploading the sketch, open your browser and enter “esp32.local“; you should then see a message from your ESP32.
Learn more about Web servers on the ESP32 here.
Conclusion
Setting up mDNS on your ESP32 makes development and testing much easier. Instead of remembering IP addresses, you can just use a consistent hostname like esp32.
This simple addition is especially useful when working with web servers, APIs, or other communication services in the local network.
Also, check out how to set up a static IP address for your ESP32 so that it doesn’t change all the time.
What do you use mDNS on your ESP32 for? Share your thoughts in the comments below!
Thanks for reading!
Links marked with an asterisk (*) are affiliate links which means we may receive a commission for purchases made through these links at no extra cost to you. Read more on our Affiliate Disclosure Page.