In this post we are going to 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 for the ESP32, adjusting the name of the libraries. At the end you have the code for both the ESP8266 and the ESP32.
We have been looking at how to connect the ESP8266 in STA mode and AP mode in some posts. Once connected, the first need is usually to find our device on the local network.
In the previous post we saw how to solve it by configuring 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.
Domain Name System (DNS) servers contain tables that convert names and domains to an IP address. We are used to using them 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 them by their 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 operates similarly to DNS. This protocol was originally published as RFC 6762, and uses UDP packet multicast.
Zeroconf services (zero-configuration) are a set of technologies that allow you to create a network automatically over the TCP/IP protocol without configuration or special servers.
When an mDNS client needs to resolve a name it sends a multicast message asking the teams on the network to identify themselves. Devices that support mDNS respond with a message that includes their IP address.
However, not everything is as nice 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 the Apple 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 instruction MDNS.begin(...)
. 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 called ‘mDNS’ instead of ‘ESP8266mDNS’.
The implementation of the library is very interesting. Take a look if you want to learn more about the mDNS protocol.
Brief example
Following the philosophy we are having in these tutorials, we are going to 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()
{
}
We add a new file called ‘ESP_Utils_mDNS.hpp’ that contains
void InitMDNS()
{
if (!MDNS.begin(hostname))
{
Serial.println("Error starting mDNS");
}
Serial.println("mDNS started");
}
This easy, we can access our ESP8266 through 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 throw cold water on it, mDNS resolution is not entirely reliable. Almost certainly at some point it will give you difficulties.
For a truly reliable solution, it is better to set up a static IP and, optionally, set up a DNS in our local network. However, mDNS is a convenient solution, and an alternative to consider.
So far, for now, the entries aimed at configuring the connection of the ESP8266. In the next post we will see how to set up 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 in this post is available for download on Github.
Version for the ESP8266: https://github.com/luisllamasbinaburo/ESP8266-Examples
Version for the ESP32: https://github.com/luisllamasbinaburo/ESP32-Examples