como-configurar-un-esp8266-para-acceder-por-mdns

How to Set Up ESP32 for mDNS Access

  • 4 min

In this post, we will see how to use multicast DNS (mDNS) to access an ESP32 on our local network by its name without having to know its IP address.

The example is oriented to ESP32. In many cases it can also be adapted to ESP8266 by changing libraries and a few pin details.

Once connected to the network, the first need is usually to find our device on the local network.

One way to solve this is to set a static IP. Another alternative is to use a network scanner. However, it is also possible to use mDNS to locate the device by its name, which is usually much more convenient.

Unicast Domain Name System (DNS) servers contain tables that convert names and domains to an IP address. We are used to their use on the Internet. Although it is possible to configure a local DNS server, many small local networks do not have a DNS server, so we can only access by IP address.

Fortunately, there is another way: the multicast DNS protocol, which resolves domain names that use ‘.local’ as a suffix. For example, http://esp32.local

mDNS is a Zeroconf service that essentially works similarly to DNS. This protocol was originally published as RFC 6762 and uses UDP packet multicast.

Zeroconf (zero-configuration) services are a set of technologies that allow creating a network automatically over the TCP/IP protocol without special configuration or servers.

When an mDNS client needs to resolve a name, it sends a multicast message asking the devices on the network to identify themselves. Devices that support mDNS respond with a message that includes their IP address.

However, not everything is as rosy as it seems. mDNS does not work natively on all operating systems.

  • Mac OSX supports mDNS through its Bonjour service.
  • Linux also supports it natively in many distributions.
  • Windows partially supports mDNS, but only for printers. It can be made to work by installing Apple’s Bonjour service.
  • Android implements mDNS internally but does not resolve mDNS names natively.

mDNS on the ESP32

ESP32 supports mDNS through the ESPmDNS.h library. Its use is very simple; we can configure it with the MDNS.begin(...) instruction. Here is an example.

#include <WiFi.h>
#include <ESPmDNS.h>
 
const char* ssid = "ssid";
const char* password = "password";
 
void setup()
{
   Serial.begin(115200);
   delay(10);
 
   WiFi.mode(WIFI_STA);
   WiFi.begin(ssid, password);
   while (WiFi.status() != WL_CONNECTED) { delay(200); }
   
   // Start mDNS at esp32.local address
   if (!MDNS.begin("esp32"))
   {             
     Serial.println("Error starting mDNS");
   }
   Serial.println("mDNS started");
}
 
void loop()
{
}
Copied!

In ESP8266 the equivalent library is called ESP8266mDNS. Its basic use is very similar.

The library implementation is very interesting. Take a look if you want to learn more about the mDNS protocol.

Summary Example

Following the philosophy we have been using in these tutorials, we will simplify the code by dividing it. To the files we already had, ‘config.h’ and ‘ESP_Utils.hpp’, we add a file called ‘ESP_Utils_mDNS.hpp’.

The code is reduced to:

#include <WiFi.h>
#include <ESPmDNS.h>
 
#include "config.h"  // Replace with your network data
#include "ESP32_Utils.hpp"
#include "ESP32_Utils_mDNS.hpp"
 
void setup()
{
   Serial.begin(115200);

   ConnectWiFi_STA();
   
   InitMDNS();
}
 
void loop()
{
}
Copied!

Add a new file called ‘ESP_Utils_mDNS.hpp’ containing:

void InitMDNS()
{
   if (!MDNS.begin(hostname)) 
   {             
     Serial.println("Error starting mDNS");
   }
   Serial.println("mDNS started");
}
Copied!

That easy, we can access our ESP32 via the URL ‘http://esp32.local’. Of course, we can change the name and address to whatever we want by changing it in the ‘MDNS.begin(…)’ function.

However, and not to be a killjoy, mDNS resolution is not completely reliable. It will almost certainly give you trouble at some point.

For a truly reliable solution, it is better to set a static IP and, optionally, configure a DNS on our local network. However, mDNS is a convenient solution and an alternative to consider.

That is it for the basic ESP8266 network configuration. With STA, AP, static IP, and mDNS we already have a solid foundation to build fairly capable clients, servers, and connected devices.

Download the Code

All the code from this post is available for download on Github.

github-full

Version for ESP8266: https://github.com/luisllamasbinaburo/ESP8266-Examples

Version for ESP32: https://github.com/luisllamasbinaburo/ESP32-Examples