ESP8266 mDNS

This tutorial explains how to Multicast DNS using ESP8266? In networking world it is difficult to remember ip address of each website and computer, to solve this problem Domain Name System (DNS) is used to make human understandable names. In ESP8266 when using ESP as web server, It is difficult to remember ip address of ESP8266 and also it is difficult to identify ip address of ESP in DHCP mode. i.e. WiFi router assigns IP address to ESP8266. Most ESP8266 application doesn’t have display interface and they are not easy to access to know its IP address. To overcome this problem mDNS is used.

What is mDNS?

for more information on mDNS read RFC6762

As networked devices become smaller, more portable, and more ubiquitous, the ability to operate with less configured infrastructure is increasingly important. In particular, the ability to look up DNS resource record data types (including, but not limited to, host names) in the absence of a conventional managed DNS server is useful.

Multicast DNS (mDNS) provides the ability to perform DNS-like operations on the local link in the absence of any conventional Unicast DNS server. In addition, Multicast DNS designates a portion of the DNS namespace to be free for local use, without the need to pay any annual fee, and without the need to set up delegations or otherwise configure a conventional DNS server to answer for those names.

The primary benefits of Multicast DNS names are that

  1. They require little or no administration or configuration to set them up,
  2. They work when no infrastructure is present, and
  3. They work during infrastructure failures.

Using mDNS in Local Network on ESP8266

Program for mDNS using ESP8266

This program creates ESP8266 mDNS with name esp8266 MDNS.begin(“esp8266”)

/*
 * Circuits4you.com
 * mDNS example ESP8266 in Arduino IDE
 * After connecting to WiFi router enter esp8266.local in web browser
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char* wifiName = "circuits4you.com";
const char* wifiPass = "your_password";
  
ESP8266WebServer server(80);

//Handles http request 
void handleRoot() {
  digitalWrite(2, 0);   //Blinks on board led on page request 
  server.send(200, "text/plain", "hello from esp8266!");
  digitalWrite(2, 1);
}


// the setup function runs once when you press reset or power the board
void setup() {
  
  Serial.begin(115200);
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  
  Serial.print("Connecting to ");
  Serial.println(wifiName);

  WiFi.begin(wifiName, wifiPass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());   //You can get IP address assigned to ESP

   if(WiFi.status() == WL_CONNECTED) //If WiFi connected to hot spot then start mDNS
  {
    if (MDNS.begin("esp8266")) {  //Start mDNS with name esp8266
      Serial.println("MDNS started");
    }
  }

  server.on("/", handleRoot);  //Associate handler function to path
    
  server.begin();                           //Start server
  Serial.println("HTTP server started");
}

// the loop function runs over and over again forever
void loop() {
  server.handleClient();
}

After uploading this program open serial monitor to see everything is fine.

Then open web browser and enter esp8266.local in address bar. Make sure your esp and the laptop (NOT ANDROID PHONE)  on which you are opening web browser use same WiFi network.

DNS system have www.xyzabc.com like names. but when you use mDNS that is local DNS system does not have any DNS server. you have to enter domain name after that dot(.) local. ex. esp8266.local

esp8266 mdns
ESP8266 mDNS

Important Note: Android OS does not support mDNS. You have to use IP address in mobile phone. To solve this problem, display IP address in web page. so that you can get ip of ESP8266 on your laptop.

 

Leave a Reply