How to name 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 name resolution using mDNS.
- Name resolution by host name
- Name 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.
Write query-host1 to ESP32#1 and query-host2 to ESP32#2.
mDNS hostname for ESP32#1 is esp32-mdns1.local.
mDNS hostname for ESP32#2 is esp32-mdns2.local.
Look for esp32-mdns2.local
.
git clone https://github.com/nopnop2002/esp-idf-mdns
cd esp-idf-mdns/query-host1
idf.py menuconfig
idf.py flash
Look for 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.
Duplicate service names are allowed.
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.
Look for the service name of _service_49876
.
git clone https://github.com/nopnop2002/esp-idf-mdns
cd esp-idf-mdns/query-service
idf.py menuconfig
idf.py flash
Look for the service name of _service_49876
.
cd esp-idf-mdns/query-service
idf.py menuconfig
idf.py flash