Learn/mdnsesp-idfnetworking

ESP32 mDNS: Find Your Board by Name on the Home Network

Stop hunting for an ESP32's IP address in the router page. Give each board a .local name with ESP32 mDNS, advertise its service, and reserve its address.

Signed by M·
Brick street in daylight where one house holds a bright orb above a small green board, and thin golden threads stay on that street
How do I find an ESP32 on my home network without knowing its IP address?

Run Multicast DNS on the board. Add Espressif's mdns component, call mdns_init and mdns_hostname_set with a unique name such as porch-cam, and the board answers to porch-cam.local on the same network. Add a DHCP reservation in the router if a tool needs an address that never changes.

Every guide for a home ESP32 eventually says "open the board in your browser" or "point the update tool at the device." That assumes you know where the board is. Most home routers hand out addresses from a pool, so the number you wrote down last week may now belong to a television. ESP32 mDNS fixes the everyday half of that problem: the board answers to a name such as porch-cam.local, and your laptop finds it without a lookup table.

This is a how-to built from Espressif's documentation and the standards it implements. Nothing here was re-run on a board for this article, so treat the code as the documented starting point and test it on yours.

Why a board's address keeps moving, and what a name fixes

A normal DNS lookup asks a server. RFC 6762, the Multicast DNS standard, removes the server. Any query for a name ending in .local is multicast to 224.0.0.251, or FF02::FB for IPv6, on UDP port 5353, and the device that owns the name replies. Espressif's documentation notes that macOS ships it as Bonjour and that Linux usually provides it through Avahi.

At startup a responder probes for the names it wants before it announces them. If another device already holds the name, the standard expects one side to choose a different one. That is cooperative by design: there is no central authority on a home network to decide who owns porch-cam.

A hostname tells you where a board is. Service discovery tells you what it offers. RFC 6763, DNS-Based Service Discovery, lets a client ask "who here runs _http._tcp?" and get back named instances with a port and optional TXT key and value pairs.

Multicast DNS also has limits. It lives on one link. A board on the main Wi-Fi is invisible to a laptop on a guest network or a separate IoT VLAN unless the router relays mDNS between them. Espressif's documentation also cites Wi-Fi multicast behavior among the reasons for its design choices, so test names on the network the board will actually live on.

It is also not authentication. RFC 6762 says its conflict handling assumes cooperating hosts, and that networks which cannot rule out a hostile device need IPsec or DNSSEC signatures. On a home network, a name is a convenience. Access to the board should still depend on its own credentials.

Set up ESP32 mDNS with Espressif's component

Add the component to an ESP-IDF project

Espressif ships mDNS as a managed component rather than inside the core framework. The component registry page listed 1.15.1 as the latest release when this was written, with one command to add it:

idf.py add-dependency "espressif/mdns^1.15.1"

The library starts its own FreeRTOS task. Espressif's mDNS documentation lists the task priority and stack size as configuration options if you need to tune memory on a small board.

Give each board its own hostname

Start the responder after the network stack is initialized, then set the name:

#include "mdns.h"

void start_mdns(void)
{
    ESP_ERROR_CHECK(mdns_init());
    ESP_ERROR_CHECK(mdns_hostname_set("porch-cam"));
    mdns_instance_name_set("Porch camera");
}

The hostname is what answers as porch-cam.local. The instance name is the friendly label a browsing app shows. Espressif's documentation is explicit that setting your own hostname is mandatory before you advertise services.

Pick names the way you would label a fuse box: by job and room, lowercase, with hyphens. Do not flash one fixed hostname into every board from the same firmware. Two boards called esp32 will collide, and one of them will end up answering to a name you did not choose. Building the name from a setting, or adding the last bytes of the MAC address, keeps each one unique.

Advertise a service and check it from a laptop

If the board serves a status page on port 80, say so:

mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0);

TXT records can carry small facts such as a firmware version. Espressif's own example puts a user name and password in TXT fields to show the syntax; do not copy that part. Anything in a TXT record can be read by every device on the network.

Espressif notes that its library does not resolve the board's own name, so test from another machine. On a laptop on the same network, ping porch-cam.local should answer. On macOS, dns-sd -B _http._tcp browses for the service; on Linux, avahi-browse -rt _http._tcp does the same. The board itself can look up other devices with mdns_query_a and browse with mdns_query_ptr.

When a name is not enough: reserve the address

A DHCP reservation in the router

Some tools want a number, not a name, and some networks block multicast. The fix is to keep the address stable at the source. RFC 2131 calls it manual allocation: the administrator assigns an address to a client, and DHCP simply delivers it. Home routers usually label it a DHCP reservation or static lease, keyed to the board's MAC address.

A reservation keeps the decision in one place. The board still asks for an address the normal way, and if you replace the router you move one list instead of reflashing devices.

A static address on the board

The other route is to stop the DHCP client and set the address in firmware. ESP-IDF's esp_netif API provides esp_netif_dhcpc_stop and esp_netif_set_ip_info for that, and esp_netif_set_hostname for the name the board offers to the DHCP server. The risk is that a hard-coded address can collide with one the router hands out later. If you go this way, pick an address outside the router's pool.

How names fit the rest of a dldeploy home

The other guides on this site assume you can reach the board. ESP32 OTA updates need a device to update, SNTP time sync needs a board you can check, and a power-cycle test is easier when the board comes back under the same name. If you are starting from scratch, deploy an ESP32 at home in an afternoon covers flashing and Wi-Fi first.

ESP32 mDNS is a LAN convenience; it does not identify or authenticate a device. The Connect cluster covers how devices reach each other, and the MATA Home integration explains how a local board will join the home computer. On mata.network, the guide to ESP32 devices on MATA Home describes that home computer from the household's side.

FAQ

Quick answers for builders evaluating this technology.

What is a .local address?

Names ending in .local are resolved by Multicast DNS, defined in RFC 6762. Instead of asking a DNS server, the computer multicasts the question to 224.0.0.251 (or FF02::FB for IPv6) on UDP port 5353, and the device that owns the name answers. It works only on the local link.

Why can my phone not reach board.local from the guest network?

Multicast DNS questions stay on one link, so a guest network, a separate VLAN or an isolated IoT SSID will not see the board's answer unless the router runs an mDNS proxy or reflector. Either put the client on the same network or connect by a reserved IP address.

Can an ESP32 resolve its own .local name?

Not with Espressif's mDNS library. Its documentation says self-querying names is not supported, by design. The board already knows its own address, so test the name from a laptop or phone on the same network instead, for example with ping porch-cam.local.

What happens if two boards use the same mDNS hostname?

RFC 6762 has every responder probe for its names at startup and resolve conflicts cooperatively, so one of them has to pick another name. You avoid the confusion entirely by giving each board a distinct hostname, for example from its job and room or a suffix from its MAC address.

Is mDNS secure enough to trust a name on my network?

Only as far as you trust every device on that network. RFC 6762 says conflict resolution assumes cooperating participants, and that hosts which cannot rule out hostile devices on the link need IPsec or DNSSEC signatures. Treat a .local answer as a convenience, and keep device access authenticated.