• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

mDNS for ESP32

Q&A Forum › mDNS for ESP32
0 Vote Up Vote Down
Barry asked 2 years ago
5 Answers
0 Vote Up Vote Down
Barry answered 2 years ago

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:

  1. 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.
  2. 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.
  3. You can manually update the BOARDS in IDE 1.8.19 by going to the top menu. Select:
    1. Tools
    2. Boards
    3. Boards Manager
    4. In the Type drop down, select ESP32
      This will display the version options. Select 2.0.11 (or later) and Install
  4. 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

0 Vote Up Vote Down
Barry answered 2 years ago

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

0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Hi.
Thank you so much for sharing this quick tutorial.
It surely will be useful for our readers.
 
Regards,
Sara

0 Vote Up Vote Down
Barry answered 2 years ago

Is my post now public? I would like to replace it (publish) with a slightly different version.

0 Vote Up Vote Down
LoetLuemmel answered 2 years ago

Hi Barry,
Yes, your post is public, because I am an ordinary user here.
You did a great tutorial and I would have created my own DNS for that purpose. – Cool! 😎 
I will try your approach soon myself.
Happy coding!

LoetLuemmel

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.