D1 mini local server

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("");
}

Sounds like the Wemos had lost the wi-fi connection. Check for that at the beginning of loop() and if it has been lost, reset.

No, it is not internet connection issue, it pings fine. How come I could not access webserver after a few minutes? If I push the reset button, it works again, but after a few minutes, it will stop

Does anyone know please?

I've been running your sketch on my Wemos D1 Mini R2 for over an hour without issue.

My first impression is another device has the same IP address, and I suspect that is the case. You could easily test that by pinging the IP and turning off your D1. Otherwise, there is something happening in your WiFi network, or on your particular D1. Either way, your sketch seems to work so it's unlikely to be a programming issue.

@caineroad
I don't know the answer to your question but maybe putting your WiFi credentials in a public forum isn't a sensible thing to do. I suggest you edit them out.

I didn’t put my real credentials, plus even if you know it you still won’t be able to access to it

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.