In this post, we will see how to use multicast DNS (mDNS) to access an ESP8266 or ESP32 on our local network by its name without having to know its IP address.
We will refer to the ESP8266, but the same code is compatible with the ESP32, adjusting the library names. At the end, you have the code for both ESP8266 and ESP32.
We have been covering how to connect the ESP8266 in STA mode and AP mode. Once connected, the first need is usually to find our device on the local network.
In the previous post, we saw how to solve this by setting 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.
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://esp8266.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 ESP8266
ESP8266 supports mDNS through the ‘ESP8266mDNS.h’ library. Its use is very simple; we can configure it with the MDNS.begin(...) instruction. Here is an example.
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.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 esp8266.local address
if (!MDNS.begin("esp8266"))
{
Serial.println("Error starting mDNS");
}
Serial.println("mDNS started");
}
void loop()
{
}
In the case of the ESP32, the library is simply called ‘mDNS’ instead of ‘ESP8266mDNS’.
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 <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include "config.h" // Replace with your network data
#include "ESP8266_Utils.hpp"
#include "ESP8266_Utils_mDNS.hpp"
void setup()
{
Serial.begin(115200);
ConnectWiFi_STA();
InitMDNS();
}
void loop()
{
}
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");
}
That easy, we can access our ESP8266 via the URL ‘http://esp8266.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’s all for now regarding the posts dedicated to configuring the ESP8266 connection. In the next post, we will see how to configure a client, and in the following ones, how to make the ESP8266 act as a server. See you soon!
Download the Code
All the code from this post is available for download on Github.
Version for ESP8266: https://github.com/luisllamasbinaburo/ESP8266-Examples
Version for ESP32: https://github.com/luisllamasbinaburo/ESP32-Examples

