How to Assign a Web Address to your ESP8266 using Multicast DNS

Typically we use the ESP8266’s IP address to access the server, however, it’s easier to remember a name than a set of numbers. When we connect our Esp8266 to our local Wi-Fi network, it gets an IP address assigned by our wireless router. Then when we set up a web server running on the Esp8266, we use that IP address to access different routes that call different functions that are part of our firmware. The problem with that IP address is that it takes an extra step to find out what it is. It can change over time and it can be difficult to remember if you’re working with different devices.

So let’s see how to assign an easy-to-remember web address to your Esp8266 using multicast DNS or mDNS. Let’s start with a simple web server code used in the previous article showing how to set up a local ESP8266 server

Here in the code we will include an additional library
# include <ESP8266mDNS.h>
Inside that library, we now have access to a class called MDNS responder that we will use to declare an object.
MDNSResponder mdns;
With this object, we can use the begin method in order to create the web address that we will be using and name it “esp-01”. The second parameter that this method needs is the IP address of the Esp8266 that we can use using the local IP method of the Wi-Fi object.
mdns.begin("esp-01", Wifi.localIP());
Add a conditional statement that when it evaluates to true it’ll print out a message to the user saying something like “MDNS responder has started!”.
Lastly, use the MDNS built-in object to call the method Add service
MDNS.addService("http","tcp","80");
This requires three parameters. These parameters describe the type of service that we want to broadcast over the network. In this case, we will use HTTP over TCP on 80. 

Code

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

ESP8266WebServer server;

char* ssid = "YOUR_SSID";
char* password = "YOUR_PASSWORD";

MDNSResponder mdns;


void setup()
{
 WiFi.begin(ssid,password);
 Serial.begin(115200);
 while(WiFi.status()!=WL_CONNECTED)
 {
 Serial.print(".");
 delay(500);
 }
 Serial.println("");
 Serial.print("IP Address: ");
 Serial.println(WiFi.localIP());

if (mdns.begin("esp8266-01", WiFi.localIP()))
Serial.println("MDNS responder started");

server.on("/",[](){server.send(200,"text/plain","Hello World!");});
server.begin();

MDNS.addService("http", "tcp", 80);}


void loop()
{
server.handleClient();
}

Connect your board and test the code by opening the serial monitor and see first the IP address and the little print out message that we gave us the “MDNS Responders started!” 

1

If we  go to our browser we can type in the address we chose which is http:// esp-01.local

2

So that’s it! You have a web address for your ESP8266 server!

Christine

Christine

Author at onlineshouter
Christine writes for people who seek for knowledge about SEO, blogging, online marketing, gadgets and web apps.
Christine

Leave a Reply

Your email address will not be published.