How to IP address resolution by mDNS.
Since ESP-IDF Ver5, mDNS has been moved from built-in library to this IDF component registry.
Accordingly, the mDNS official example has been removed from the ESP-IDF repository.
The official repository for mDNS is here.
The official repository comes with example code, but it's a bit confusing.
There are two methods for IP address resolution using mDNS.
- IP address resolution by host name
- IP address resolution by service name
This is a quick example of each.
ESP-IDF V4.4/V5.x.
ESP-IDF V5.0 is required when using ESP32-C2.
ESP-IDF V5.1 is required when using ESP32-C6.
Requires two ESP32s.
To find the IP address, you need to know the mDNS hostname.
mDNS hostnames must be unique within the network.
Write query-host1 to ESP32#1 and query-host2 to ESP32#2.
The mDNS name for this project is esp32-mdns1.local
.
Then look for the the mDNS name of esp32-mdns2.local
.
git clone https://github.com/nopnop2002/esp-idf-mdns
cd esp-idf-mdns/query-host1
idf.py menuconfig
idf.py flash
The mDNS name for this project is esp32-mdns2.local
.
Then look for the mDNS name of esp32-mdns1.local
.
cd esp-idf-mdns/query-host2
idf.py menuconfig
idf.py flash
To find the IP address, you need to know the service name.
Multiple hosts with the same service name are allowed within the network.
If you give two nodes the same service name, they can find each other.
This is useful when doing P2P communication with UDP.
Write query-service to ESP32#1 and ESP32#2.
Each mDNS hostname is generated from MAC address.
Therefore, the other party's mDNS host name is completely unknown.
This project looks for the service name of _service_49876._udp
.
git clone https://github.com/nopnop2002/esp-idf-mdns
cd esp-idf-mdns/query-service
idf.py menuconfig
idf.py flash
This project looks for the service name of _service_49876._udp
.
cd esp-idf-mdns/query-service
idf.py menuconfig
idf.py flash
$ sudo apt install avahi-utils
- Translate one or more fully qualified host names into addresses.
$ avahi-resolve -n esp32-mdns1.local
esp32-mdns1.local 192.168.10.115
- Use Avahi daemon to browse all mDNS network services.
$ avahi-browse -art
+ enp2s0 IPv4 ESP32 with mDNS _service_49876._udp local
= enp2s0 IPv4 ESP32 with mDNS _service_49876._udp local
hostname = [esp32-mdns-05C634.local]
address = [192.168.10.115]
port = [49876]
txt = []
- Use the Avahi daemon to browse specific mDNS network services.
$ avahi-browse -rt _service_49876._udp
+ enp2s0 IPv4 ESP32 with mDNS _service_49876._udp local
= enp2s0 IPv4 ESP32 with mDNS _service_49876._udp local
hostname = [esp32-mdns-05C634.local]
address = [192.168.10.115]
port = [49876]
txt = []
For more information is here.