ESP1 failed to resolve IP of ESP2 when ESP1 also runs MDNS.begin()
Hi,
I have two ESP8266. ESP1 resolve the IP of the server on ESP2 by its name "esp8266.local" and it works just fine. But when I started running MDNS on the ESP1, it failed to resolve the IP. When I commented out this line if (MDNS.begin(HOST_NAME)) { Serial.println("MDNS responder started"); }
it started to resolve the IP normally.
Any help on how to get around this?
What is the hostname on ESP1?
If you can post me some sample code, too.
ESP1 code is:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
#include <mDNSResolver.h> //https://github.com/madpilot/mDNSResolver
#include <EEPROM.h>
#include <ESP8266httpUpdate.h>
#define HOST_NAME "esp8266"
const char* ssid = "myWiFi";
const char* password = "myPassword";
String hostName;
//#define NAME_TO_RESOLVE "master-default.local"
IPAddress masterHostIP (0, 0, 0, 0);
int count = 0;
using namespace mDNSResolver;
WiFiUDP udp;
Resolver resolver(udp);
ESP8266WebServer server(80);
void setup () {
Serial.begin(115200);
delay(100);
Serial.println("\r\nsetup()");
pinMode(2, OUTPUT);
Serial.println("");
Serial.println("client");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); //setting up Station
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.print(WiFi.localIP());
delay(1000);
Serial.println("Connect");
Serial.print("Resolving ");
//Serial.println(NAME_TO_RESOLVE);
if (MDNS.begin(HOST_NAME)) {
Serial.println("MDNS responder started");
}
resolveIP();
}
//===========================================================
void loop() {
server.handleClient();
resolver.loop();
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
resolveIP();
count++;
Serial.println(count);
String Uri = "/toggle";
String ipStr = String(masterHostIP[0]) + '.' + String(masterHostIP[1]) + '.' + String(masterHostIP[2]) + '.' + String(masterHostIP[3]);
hostName += "http://";
hostName += ipStr;
hostName += Uri;
sendRequest();
}
delay(3000); //Send a request every 3 seconds
}
void resolveIP(){
resolver.setLocalIP(WiFi.localIP());
const char* NAME_TO_RESOLVE = "master-default.local";
masterHostIP = resolver.search(NAME_TO_RESOLVE);
if (masterHostIP != INADDR_NONE) {
Serial.print("Resolved: ");
Serial.println(masterHostIP);
} else {
Serial.println("Not resolved");
}
}
void sendRequest(){
HTTPClient http; //Declare an object of class HTTPClient
http.begin(hostName); //
Serial.println(hostName);
int httpCode = http.GET(); //Send the request
if (httpCode > 0) { //Check the returning code
String payload = http.getString(); //Get the request response payload
Serial.println(payload); //Print the response payload
}
http.end(); //Close connection
hostName = "";
}
ESP2 Code is:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <mDNSResolver.h> //https://github.com/madpilot/mDNSResolver
const char* ssid = "myWiFi";
const char* password = "myPassword";
const char* hostName = "master-default";
ESP8266WebServer server(80);
MDNSResponder mdns;
const int led = 16;
void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/plain", "hello from esp8266!");
digitalWrite(led, 0);
}
void turnOff() {
digitalWrite(led, 1);
server.send(200, "text/plain", "LED OFF");
}
void turnOn() {
digitalWrite(led, 0);
server.send(200, "text/plain", "LED ON");
}
void toggle() {
digitalWrite(led, !digitalRead(led));
if (digitalRead(led))
server.send(200, "text/plain", "LED ON");
else
server.send(200, "text/plain", "LED OFF");
}
void handleNotFound() {
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
void setup(void) {
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.hostname(hostName);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (mdns.begin(hostName, WiFi.localIP())) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/turnON", turnOn);
server.on("/turnOFF", turnOff);
server.on("/toggle", toggle);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
MDNS.addService("http", "tcp", 80);
}
void loop(void) {
server.handleClient();
mdns.update();
}
I don't have two ESPs on hand at the moment (so a physical test might have to wait until the weekend) but there are two things you can try:
- Can you resolve master-default.local using your computer?
- Can you run wireshark, or tcpdump and filter any udp traffic on 5353 and see what messages are being generated?
Okay! the answer to your questions:
-
Yes! the master-default.local was resolved successfully using computer and/or iOS browser. Further more, it was also resolved by the ESP1 successfully when I commented out the line
if (MDNS.begin(HOST_NAME)) { Serial.println("MDNS responder started"); }from the ESP1's code, but when activate this line again it became no longer able to resolve the IP of master-default.local. So, I believe the issue is within theMDSN.begin(); -
I will try and feedback to you.
Any Update ?
Unfortunately not. I’ve not been able to find time to look yet.
I had a quick read of the ESP8266 implementation of MDNS - it is a complete implementation, so you don’t need to use my library as you can query MDNS using the built in client.
https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266mDNS/examples/mDNS-SD_Extended/mDNS-SD_Extended.ino
I think this may actually do what you are trying to do?