-->
Page 1 of 3

Struggling with mDNS not working

PostPosted: Fri Jun 05, 2015 5:32 am
by OliverNZ
Hi all,

Trying to have mDNS working on my ESP-01 and it did work once but then changed code and it never worked again. Code is below. I'm a bit unsure where the mdns.update() belongs too. I'm sure it's trivial but I'm just starting out s need some help. Followed the example code but alas...

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <OneWire.h>

const char* ssid         = "*******";
const char* password     = "*******";
const char* mDNSname     = "iot01";
const int   relay        = 2;
const String relayName   = "GPIO" + String(relay);
const String ESPtype     = "ESP-01";

MDNSResponder mdns;
ESP8266WebServer server(80);

void setup(void)
{
  Serial.begin(115200);
  delay(10);
 
  Serial.println("");
  Serial.println("Hello....Booting");
  pinMode(relay, OUTPUT);
  digitalWrite(relay, 0);

  // Connect to WiFi network
  WiFi.mode(WIFI_STA);  // Just client mode! Comment this line for Client & AP!
  WiFi.begin(ssid, password);
  Serial.println("");
 
  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());
  delay(250);

  // Set up mDNS responder:
  // - first argument is the domain name, in this example
  //   the fully-qualified domain name is "esp8266.local"
  // - second argument is the IP address to advertise
  //   we send our IP address on the WiFi network
  //   Note: for AP mode we would use WiFi.softAPIP()!
  if (!mdns.begin(mDNSname, WiFi.localIP())) {
    Serial.println("Error setting up MDNS responder!");
    while(1) {
      delay(1000);
    }
  }
  Serial.println("mDNS responder started");
  Serial.print(String(mDNSname));
  Serial.println(".local");

  mdns.update();
 
  server.on("/GPIO2", []() {
   
    String state=server.arg("state");
   
    int st = state.length() - 1;
   
    switch (st) {
     case 2:   digitalWrite(relay,1); break;
     case 3:  digitalWrite(relay,0); break;
     case 4:
         if ( String(digitalRead(relay)) == "0" ) digitalWrite(relay,1);
         else digitalWrite(relay,0);
         break;
     default: break;
     }
     server.send(200, "text/plain", String(digitalRead(relay)));
     Serial.println(relayName + " is : " + state);
  });
 
  server.on("/", []() {
    server.send(200, "text/plain", ESPtype + " one Relay on " + relayName + ": " + String(mDNSname) + ".local");
  });

  server.begin();
  Serial.println("HTTP server started");
}
 
void loop(void)
{
  // mdns.update();
  server.handleClient();
}


Cheers
Oliver

Re: Struggling with mDNS not working

PostPosted: Sun Jun 07, 2015 1:20 pm
by remcohn
Ive been trying to get both mDNS and UDP to work, and got neither to work, so might be related.

Re: Struggling with mDNS not working

PostPosted: Sun Jun 07, 2015 4:51 pm
by OliverNZ
The odd thing is, that it was working. In retrospect I can't remember though if that was 1.6.1 and it stopped working in 1.6.4.

Re: Struggling with mDNS not working

PostPosted: Thu Jun 25, 2015 6:09 pm
by watz
Same problem here with 1.6.5 and esp8266-1.6.4-673-g8cd3697.zip. Apperently the code got a huge update mid may, The update() call has been removed from the examples with this update. The docs on github don't reflect that.

Anyway, the bundled mDNS example does not work for me.