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...
#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