Hi all,
I made the following codes for the D1 Mini, so I could go within my local network to control the device built-in LED. It worked perfectly for first few minutes. But after so it will not load the webserver, on laptop or phone. But it replies when I send the ping, so internet connection is established. Why does webserver not respond ?
Below is the local network during successful attempt
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
const char* ssid = "Saif"; //!!!!!!!!!!!!!!!!!!!!! name of your router
const char* password = "glockglock2777"; //!!!!!!!!!!!!!!!!!!!!!your wifi password.
WiFiServer server(80);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
// ArduinoOTA.setHostname("myesp8266");
// No authentication by default
// ArduinoOTA.setPassword((const char *)"123");
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
ArduinoOTA.handle();
delay(10); // delay to gife the arduino time to move to the next operation.
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
WiFi.reconnect();
}
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value = LOW;
if (request.indexOf("/LED=ON") != -1) {
digitalWrite(LED_BUILTIN, LOW);
value = LOW;
}
if (request.indexOf("/LED=OFF") != -1){
digitalWrite(LED_BUILTIN, HIGH);
value = HIGH;
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("LED is now: ");
if(value == HIGH) {
client.print("OFF");
} else {
client.print("ON");
}
client.println("<br><br>");
client.println("Click <a href=\"/LED=ON\">here</a> turn the LED ON<br>");
client.println("Click <a href=\"/LED=OFF\">here</a> turn the LED OFF<br>");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
}