Setting up mDNS on ESP32 for local network device discovery is one of the most practical skills any IoT developer can learn. Instead of hunting down your ESP32’s IP address every time it reconnects to Wi-Fi, mDNS (Multicast DNS) lets you access it using a friendly hostname like esp32sensor.local — from any browser or app on the same network. This tutorial covers everything from the basics of mDNS to a full working example with a web server and OTA updates, written specifically for the Indian maker community.
What is mDNS and How Does It Work?
mDNS stands for Multicast DNS, defined in RFC 6762. It is a zero-configuration networking protocol that resolves hostnames to IP addresses within a local network without the need for a central DNS server. When your ESP32 announces itself as mydevice.local, it broadcasts this name on the local network using multicast UDP packets on port 5353. Any other device on the same network — your phone, laptop, or Raspberry Pi — can then resolve mydevice.local to the ESP32’s IP address automatically.
mDNS is part of the broader Zeroconf ecosystem that also includes:
- DNS-SD (DNS Service Discovery): Advertises services (HTTP, SSH, MQTT) alongside hostnames
- Link-Local Addressing: Assigns IPs in the 169.254.x.x range when no DHCP server is present
- Bonjour: Apple’s implementation of mDNS + DNS-SD (pre-installed on macOS)
- Avahi: The Linux equivalent of Bonjour
On Windows 10 and later, mDNS is supported natively. On Android, you may need the Network Discovery feature or a third-party app like Service Browser. This is an important consideration for Indian developers building consumer IoT products targeted at Android users.
Hardware Setup for This Tutorial
You need minimal hardware for this tutorial since mDNS is a software feature:
- Any ESP32 development board
- Micro USB or USB-C cable for programming
- 2.4 GHz Wi-Fi router
- Optionally: a DHT11/DHT20 sensor to serve live data from the web server
Ai Thinker ESP32-C3-01M Wi-Fi + BLE Module
A compact ESP32-C3 module with Wi-Fi and Bluetooth — ideal for mDNS-based local discovery projects where small form factor matters.
Basic mDNS Setup on ESP32 with Arduino
The ESP32 Arduino core includes the ESPmDNS library out of the box — no additional installation is needed. Here is the minimal code to get your ESP32 discoverable on the local network:
#include <WiFi.h>
#include <ESPmDNS.h>
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
const char* hostname = "esp32sensor"; // Access at esp32sensor.local
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("nConnected! IP: " + WiFi.localIP().toString());
if (!MDNS.begin(hostname)) {
Serial.println("Error starting mDNS");
return;
}
Serial.println("mDNS started: http://" + String(hostname) + ".local");
}
void loop() {}
After uploading and rebooting, open a browser on any device on the same Wi-Fi network and type http://esp32sensor.local. You will reach the ESP32 without knowing its IP address. This works on macOS, iOS, Windows 10+, and most Linux distributions immediately.
Important Notes:
- The hostname must be unique on the network. If two ESP32s share the same hostname, mDNS resolution becomes unreliable.
- mDNS only works on the local network (LAN). It does not work over the internet or across different subnets.
- If your router has multicast isolation or AP isolation enabled (common on Indian ISP routers), mDNS packets will be blocked. Disable these settings if mDNS discovery fails.
Combining mDNS with ESP32 Web Server
The real power of mDNS comes when you combine it with an ESP32 web server. Users can open http://esp32sensor.local in their browser and see a live dashboard of sensor readings — no app installation required.
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WebServer.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
WebServer server(80);
void handleRoot() {
float t = dht.readTemperature();
float h = dht.readHumidity();
String html = "<h1>ESP32 Sensor</h1>";
html += "<p>Temperature: " + String(t) + " °C</p>";
html += "<p>Humidity: " + String(h) + "%</p>";
server.send(200, "text/html", html);
}
void setup() {
dht.begin();
WiFi.begin("SSID", "PASSWORD");
while (WiFi.status() != WL_CONNECTED) delay(500);
MDNS.begin("esp32sensor");
MDNS.addService("http", "tcp", 80); // Advertise HTTP service
server.on("/", handleRoot);
server.on("/data", []() {
String json = "{"temp":" + String(dht.readTemperature()) +
","hum":" + String(dht.readHumidity()) + "}";
server.send(200, "application/json", json);
});
server.begin();
}
void loop() { server.handleClient(); }
The MDNS.addService("http", "tcp", 80) call is crucial — it advertises the HTTP service via DNS-SD, allowing apps like Bonjour Browser to discover the web interface automatically without knowing the hostname in advance.
DHT11 Temperature And Humidity Sensor Module with LED
This DHT11 module comes with a built-in LED indicator, making it easy to see when data is being read — great for the mDNS web server project.
Using mDNS for OTA (Over-the-Air) Updates
One of the best uses of mDNS with ESP32 is enabling OTA firmware updates. The ArduinoOTA library uses mDNS internally to advertise the device to the Arduino IDE. Once configured, you can upload new firmware wirelessly — no USB cable needed.
#include <ArduinoOTA.h>
// (WiFi.begin and MDNS.begin as before)
void setupOTA() {
ArduinoOTA.setHostname("esp32sensor"); // Same as mDNS hostname
ArduinoOTA.setPassword("admin123"); // Protect OTA with a password
ArduinoOTA.onStart([]() { Serial.println("OTA Start"); });
ArduinoOTA.onEnd([]() { Serial.println("OTA End"); });
ArduinoOTA.begin();
}
void loop() {
ArduinoOTA.handle(); // Must be called in loop
server.handleClient();
}
In the Arduino IDE, go to Tools → Port and you will see esp32sensor.local listed as a network port once the ESP32 is running. Select it and upload as normal. This is extremely useful in India where physical access to deployed devices (e.g., ceiling-mounted sensors in a factory) is difficult.
Advanced: mDNS Service Discovery for Multiple Devices
When you have multiple ESP32 devices on the same network, mDNS service discovery lets you find all of them programmatically. This is the foundation of self-configuring IoT networks used in smart home installations.
// On a central ESP32 or Raspberry Pi — discover all HTTP services
int n = MDNS.queryService("http", "tcp");
for (int i = 0; i < n; i++) {
Serial.printf("Device %d: %s at %s:%dn",
i, MDNS.hostname(i).c_str(),
MDNS.IP(i).toString().c_str(),
MDNS.port(i));
}
This approach is used in home automation systems where a central coordinator (like a Raspberry Pi running Home Assistant) automatically discovers and registers new ESP32 sensor nodes without manual IP configuration — a huge advantage over static IP setups common in older IoT designs.
DHT20 SIP Packaged Temperature and Humidity Sensor
The DHT20 uses I2C instead of single-wire, making it easier to add multiple sensors to a single ESP32 node in a multi-device mDNS network.
Frequently Asked Questions
Does mDNS work on Android smartphones?
Android supports mDNS for some system services, but browser-based .local hostname resolution was added in Android 12+. For older Android devices, use the Network Scanner app or build a Bluetooth/BLE discovery mechanism as an alternative for consumer-facing products.
Why does esp32sensor.local not resolve on my Windows PC?
Windows requires the Bonjour service (installed with iTunes or Apple devices) or the built-in mDNS responder introduced in Windows 10 version 1803. Make sure your Windows is updated. Also confirm that your firewall is not blocking UDP port 5353.
Can I use mDNS on ESP32 without Wi-Fi (e.g., Ethernet)?
Yes. mDNS works on any IP-based network interface. If you add a W5500 Ethernet module to your ESP32, you can run mDNS over Ethernet using the same ESPmDNS library with the appropriate network interface initialisation.
How many mDNS services can one ESP32 advertise?
There is no hard limit in the ESPmDNS library. In practice, advertising 3–5 services (HTTP, MQTT, OTA, custom) on a single ESP32 works reliably. Beyond that, you may see increased multicast traffic impacting network performance on congested Wi-Fi networks.
Build Your Local IoT Network Today
Find the ESP32 boards, sensors, and expansion modules you need at Zbotic.in — shipped fast across all major Indian cities.
Add comment