mDNS for the ESP32 using the Arduino IDE
iabarry on forums at rntlab.com
August 2023
This is a summary of my journey to get multicast Domain Name Service (mDNS) working well on an ESP32. This won’t be quite as pretty as the articles that Rui and Sara post, but I hope you will get the important parts. There will be a short bibliography at the end.
The mDNS feature allows users to easily browse to an IoT device via WiFi or Ethernet. The IoT_name is used rather than local IP address. In deployed systems, the IP address may not be known or easily determined by non-technical users[1]. This results in users being unhappy with the performance of the device. mDNS operates by the browser asking the PC ethernet to find the IoT. The PC broadcasts a query “is anyone on the LAN named “IoT_name”. The IoT sees this query and responds, which provides the PC with the LAN IP (usually something like 192.168.1.123”. In the ESP program, you may have set the static IoT IP[4] or have used DHCP.
There are several important bits to getting mDNS working correctly and I fell in a few holes on the journey. Hopefully this tutorial will help you avoid the holes in the road. Please be aware that you can get mDNS partially working. By that I mean that after a successful dataload, mDNS will work but only for a few minutes. A key to success is using the proper library!
It is assumed you are using IDE 1.8.19 as the Data Upload feature for ESP32 is not compatible with IDE 2.x.
mDNS Library
If you look around the web, there are multiple mDNS libraries and they are not all created equal. I’ve tried several before I came to this method. The single preferred mDNS feature was introduced to the IDE standard libraries in V2.x[2]. You will not have the correct lib if you are still using the libs provided with IDE 1.8.19. Note that there is IDE 2.x and there are libraries 2.x. You do NOT need to use IDE 2.x, but you DO need to use libs 2.x
To begin, see if you have this file, located in this specific directory.
C:\Users\ ***your name *** \AppData\Local\Arduino15\packages\esp32
\hardware\esp32\2.0.11\libraries\ESPmDNS\src\ESPmDNS.h
If you do NOT have this file, close and restart the IDE and accept the BOARD updates[3]. Then check again for the file. Although you may be able to acquire and add individual files manually, I do not recommend it as there may be links to other files (dependencies) not present on your PC. Refer to Note 3 below.
In your program, adding mDNS is rather straightforward. Add the #include and few lines of code.
Code
#include <ESPmDNS.h>
…within SETUP
…
//argument is the IoT device name
if (!MDNS.begin(“*** your desired device name ***”)) {
Serial.println(“Error setting up MDNS responder!”);
while(1) {
delay(1000);
}
}
// Add service to MDNS-SD
MDNS.addService(“_http”, “_tcp”, 80); // <– the UNDERSCORE is very important.
Serial.println(“mDNS responder started”);
} // end setup
The addService call ensures that the mDNS will run after initialization. Without this call, the mDNS will be active for only a few minutes.
Testing
After compiling and reloading the ESP32, you can use your PC browser and enter the “http://IoT_name”. You should be directed to the IoT.
Close the browser and wait 10 minutes.
Open the browser and enter the URL again. It should direct you to the IoT. If your IoT is running 24×7, you need to test mDNS more than 10 minutes after the device is reset. Test it again after 24 hours of operation.
Browser
When a user wishes to access the IoT, in the browser URL window enter: “http://IoT_name” and the browser should be directed to the server on the device. If you just enter “” the browser will try to google the name which leads to who know what???
You can also enter “http://IoT_name.local” but the addition of “.local” .doesn’t seem to matter.
Ping
On Win 10, you can test nDMS by opening a terminal (or cmd) window. Enter “ping IoT_name” and you should get a response. Interestingly, Ping doesn’t work if you enter “ping http://IoT_name”
References
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/mdns.html
Article on using mDNS. A very good starting place
https://github.com/espressif/esp-protocols/tree/master/components/mdns
Lib on github. You shouldn’t need this if you have updated BOARDS and have the 2.0.11 subdir.
NOTES:
- You can find known active IP addresses using the Win command “arp -a”, but this will not show you the device name. You need to access your router to see the map of devices by name and their respective IP address.
- There can be some confusion over the number ‘2’. The current IDE is V2.1.1. However there are a set of ESP libs and tools, which are currently at version 2.0.11 (as of this writing in Aug 2023). Reverences in this document are to the LIBS V2.0.11 only.
- You can manually update the BOARDS in IDE 1.8.19 by going to the top menu. Select:
- Tools
- Boards
- Boards Manager
- In the Type drop down, select ESP32
This will display the version options. Select 2.0.11 (or later) and Install
- When setting a static IP for the IoT, you should identify an unused IP on your LAN. In Win, open a terminal or cmd window and enter: “arp -a”. This will display a list of local IP’s known to your PC. Select a static IP which is NOT currently being occupied.
I hope this brief tutorial is of value.
iabarry
Rui & Sara
Please accept this submission with no strings attached.I wanted to give you the opportunity to review this prior to (hopefully) publishing it.
I couldn’t format the code very well – sorry.
Barry
Hi.
Thank you so much for sharing this quick tutorial.
It surely will be useful for our readers.
Regards,
Sara
Is my post now public? I would like to replace it (publish) with a slightly different version.