How to discover ESP32 service over mDNS


Say your ESP32 is connected to a WiFi network, and you wish other devices on the same network to be able to discover your ESP32 and interact with it (say through TCP or UDP). How do you achieve that? The answer is multicast DNS (mDNS) with DNS-Service Discovery (DNS-SD). While going into the details of mDNS and DNS-SD is beyond the scope of this article, you can see this article for getting an overview. Very briefly, while DNS facilitates a phone book of the internet (maps hostnames with IP addresses), mDNS facilitates the phonebook of the local network.

Think of this tutorial as an implementation LinkedIn for connected devices. We will essentially make an ESP32 tell the other devices on the network: “Hey, I’m <name>. My IP address is <ip>. I provide the following services: <service_name> + <protocol (tcp/udp)>. Let’s connect”.

Example Code

The example below will make this clear. The code for the same is quite straightforward, as you can see below:

#include "ESPmDNS.h"
#include <WiFi.h>
const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPassword";

#define MDNS_DEVICE_NAME "my-esp32"
#define SERVICE_NAME "my-service"
#define SERVICE_PROTOCOL "udp"
#define SERVICE_PORT 5600
void setup(){
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  if(!MDNS.begin(MDNS_DEVICE_NAME)) {
     Serial.println("Error encountered while starting mDNS");
void loop(){}

Replace the SSID and password with the correct values. You can also configure the service constants. As you can see, we begin the mDNS service using our device name (equivalent to a hostname), and we broadcast a service offering to anyone who discovers this device.

Testing it out

Now let’s see how this appears to the other devices in the same network. On your mobile phone, download an mDNS Discovery app. For example, this can be a good app for Android. Make sure that your phone is connected to the same network as your ESP32.

Open the app, enter the service name in the search box, and the protocol as UDP. You should be able to see that the ESP32 is visible to your phone (its IP address and port are also displayed).

Discovering ESP32

Thus, your mobile phone now knows that on your local network, it can communicate with the ESP32 at the specified IP address, over UDP on the specified port. Setting up UDP connection and handling UDP messages on ESP32 is a topic for a separate article.

For more tutorials on ESP32, check out Also, you may find this course on ESP32 on Udemy to be quite helpful. Do check it out

1 comment

Leave a comment

Your email address will not be published. Required fields are marked *