ESP32: use mDNS so the chip responds to a friendly name
Replace IP addresses with friendly names like 'kitchen-sensor.local'. mDNS is built into the ESP32 and most operating systems.
mDNS (multicast DNS) is the protocol that lets you reach a device on
your local network by name instead of IP address. With mDNS, you
type kitchen-sensor.local instead of 192.168.1.42. No DNS server,
no configuration, no router setup.
It works on the ESP32 (with Wi-Fi) and on the Raspberry Pi (with
Avahi). Most modern operating systems (Windows 10+, macOS, iOS,
Android) automatically resolve .local names.
What you need
- ESP32 dev board with Wi-Fi
- (For Pi Python section) Raspberry Pi with network
The code: ESP32
#include <WiFi.h>
#include <ESPmDNS.h>
const char* ssid = "your-wifi";
const char* password = "your-password";
const char* hostname = "kitchen-sensor";
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
// Set hostname
WiFi.setHostname(hostname);
// Initialize mDNS
if (!MDNS.begin(hostname)) {
Serial.println("mDNS init failed");
return;
}
Serial.print("mDNS responder started: ");
Serial.print(hostname);
Serial.println(".local");
// Add a service (HTTP server on port 80)
MDNS.addService("http", "tcp", 80);
}
void loop() {
MDNS.update();
}
Upload. Open Serial Monitor. After Wi-Fi connects, you should see:
mDNS responder started: kitchen-sensor.local
Now from any computer on the same network, you can open
http://kitchen-sensor.local/ in a browser and reach the ESP32’s web
server.
The code: Raspberry Pi
mDNS on Linux is provided by Avahi. Most Pi OS images have it preinstalled.
sudo apt install avahi-daemon
sudo systemctl enable avahi-daemon
The Pi responds to <hostname>.local automatically once Avahi is
running. Set the hostname:
sudo hostnamectl set-hostname kitchen-sensor
sudo reboot
After reboot, kitchen-sensor.local resolves to the Pi’s IP.
Why mDNS is useful
Three scenarios where mDNS shines:
- DHCP reassigns the IP. Your router can change the ESP32’s IP at any time. With mDNS, the name stays the same.
- Multiple devices on the same network. IP addresses are hard to remember. Names are not.
- Zero configuration. No DNS server, no router setup, no static IP assignment.
The downside: mDNS only works on the local network. It does not resolve over the internet. For that, you need a real DNS service.
Custom service names
Beyond HTTP, mDNS can advertise any service:
// Add a custom service
MDNS.addService("_ctrlaltbrian", "_tcp", 8080);
This advertises a _ctrlaltbrian._tcp service on port 8080. Other
devices on the network can browse for this service and find your ESP32.
Useful for service discovery in IoT networks:
// On the broker/broadcaster
MDNS.addService("_mqtt", "_tcp", 1883);
MDNS.addService("_ctrlaltbrian-sensor", "_tcp", 8080);
A client looking for sensors can scan the network for the service name:
import zeroconf
zc = zeroconf.Zeroconf()
browser = zeroconf.ServiceBrowser(zc, "_ctrlaltbrian-sensor._tcp.local.", ...)
Browser compatibility
| Platform | mDNS support |
|---|---|
| Windows 10+ | Built in. Works automatically. |
| Windows 7/8 | Need Bonjour installed (it comes with iTunes). |
| macOS | Built in. Works automatically. |
| iOS | Built in. Works automatically. |
| Android | Built in since Android 8.0. |
| Linux | Avahi (install if not present). |
| Chromebook | Built in. |
For older Windows, install Apple’s Bonjour Print Services (free). It ships with iTunes and Adobe software, so most users already have it.
The 250ms timeout
mDNS responses are sent over multicast UDP. Networks with many devices or poor Wi-Fi can drop these packets. If a name does not resolve, try again or check the network.
Tools for testing:
dns-sd -B _http._tcp local.(macOS): list HTTP servicesavahi-browse -at(Linux): list all servicesnslookup kitchen-sensor.local(Windows 10+): query a specific name
Security
mDNS has no authentication. Any device on the local network can
advertise any name. A malicious device could pretend to be
kitchen-sensor.local and MITM your connections. For trusted local
networks this is fine; for untrusted networks, use HTTPS with proper
certificate validation.
What you learned
- mDNS lets you reach devices by name (kitchen-sensor.local).
- It works on the ESP32 (with Wi-Fi) and the Raspberry Pi (with Avahi).
- It is built into most modern operating systems.
- No DNS server or configuration required.
When something breaks
- Name does not resolve. Wi-Fi not connected, mDNS not initialized,
or the OS does not support
.localresolution. - Resolves but wrong device. Another device on the network is using the same name. Change the hostname.
- Slow to resolve. Multicast packets dropped. Try again or use a direct IP.
What to build next
- The ESP32 web server tutorial combines with this for named web servers.
- The ESP-NOW tutorial uses direct MAC addressing, which complements mDNS.
- The book ESP32 Smart Home covers service discovery across multiple devices.