Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By atifkamal
#80376 Hi, I am dealing with a problem in my project based on wemos d1 mini clone module, issue is web server stops responding randomly and specifically when I try to access http://1.1.1.1:88/update, for the first time page come up with update option but when using http://1.1.1.1/ and after toggling some switches and/or try to access http://1.1.1.1:88/update it hangs (ping from wemos ip is ok), then the only way out is to reset wemos. Experts are requested to please shed some light on issue, thanks in advance.

here is the sketch:

Code: Select all#include <IRrecv.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#include <WiFiManager.h>
#include <FS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
// #include <DNSServer.h>
#include <ESP8266SSDP.h>

uint16_t RECV_PIN = TX; //d10 TX
IRrecv irrecv(RECV_PIN);
decode_results results;

const char* mdnsName = "esp8266b1";
String s = "";
int n, swval[9];
unsigned int data_num = 0;
int LED0 = 0;
int LED1 = D0;
int LED2 = D1;
int LED3 = D2;
int LED4 = D3;
int LED5 = D4;
int LED6 = D5;
int LED7 = D6;
int LED8 = D7;
int errpin = RX; //D9 RX
int wifibtn = D8;


ESP8266WebServer httpServer(88); //Server on port 88
ESP8266HTTPUpdateServer httpUpdater;
MDNSResponder mdns;
WiFiServer server(80);
// DNSServer dnsServer;


void setup() {
 Serial.begin(115200);
  SPIFFS.begin();
  Serial.println("SPIFFS Begin...");
  irrecv.enableIRIn();

//WiFiManager
   
  WiFiManager wifiManager;  //Local intialization.
   // wifiManager.resetSettings(); //reset saved settings
    //set custom ip for portal
    //wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
    //fetches ssid and pass from eeprom and tries to connect
    //if it does not connect it starts an access point with the specified name
    //here  "AutoConnectAP"
    //and goes into a blocking loop awaiting configuration
  wifiManager.autoConnect("AutoConnectAP");
    //wifiManager.autoConnect(); //or use this for auto generated name ESP + ChipID
  Serial.println("Connected...)");  //if you get here you have connected to the WiFi
  Serial.println();
  Serial.print("Connected to AP: ");
  Serial.println(WiFi.SSID()); 
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 

// mDNS
  if (!mdns.begin(mdnsName)) {
    Serial.println("Error setting up MDNS responder!");
    while (1) {
      delay(10);
    }
  } else {
    Serial.print("mDNS responder started: http://");
    Serial.print(mdnsName);
    Serial.println(".local");
    delay(10);
  }
// mDNS End

// ArduinoOTA
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
// ArduinoOTA.setHostname("myesp8266");
// No authentication by default
// ArduinoOTA.setPassword((const char *)"123");
ArduinoOTA.onStart([]() {
 Serial.println("ArduinoOTA Started...");
 });
 ArduinoOTA.onEnd([]() {
 Serial.println("\nArduinoOTA End...");
 });
 ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
 Serial.printf("OTA:Progress: %u%%\r", (progress / (total / 100)));
 });
 ArduinoOTA.onError([](ota_error_t error) {
 Serial.printf("OTA Error[%u]: ", error);
 if (error == OTA_AUTH_ERROR) Serial.println("OTA:Auth Failed");
 else if (error == OTA_BEGIN_ERROR) Serial.println("OTA:Begin Failed");
 else if (error == OTA_CONNECT_ERROR) Serial.println("OTA:Connect Failed");
 else if (error == OTA_RECEIVE_ERROR) Serial.println("OTA:Receive Failed");
 else if (error == OTA_END_ERROR) Serial.println("OTA:End Failed");
 });
 ArduinoOTA.begin();
 Serial.println("OTA:Begin...");
 // ArduinoOTA End

  delay(10);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  pinMode(errpin, OUTPUT);
  pinMode(wifibtn, INPUT);
 
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED4, HIGH);
  digitalWrite(LED5, HIGH);
  digitalWrite(LED6, HIGH);
  digitalWrite(LED7, HIGH);
  digitalWrite(LED8, HIGH);
  digitalWrite(errpin, HIGH);

server.begin();


// httpUpdater
  MDNS.begin(mdnsName);
  httpUpdater.setup(&httpServer);
  httpServer.begin();
  MDNS.addService("http", "tcp", 88);
  Serial.printf("HTTPUpdateServer ready! Open http://%s.local/update in your browser\n", mdnsName);
// httpUpdater




// fileread statements start
  File f = SPIFFS.open("/relaydatafile.txt", "r");
  if (!f) {
    Serial.println("File open failed");
  }
  String s = f.readStringUntil('\n');
  Serial.println();
  Serial.print("Reading from SPIFFS file..: ");
  Serial.println(s);
  while (s.indexOf(",") != -1) {
    swval[data_num] = s.substring(0, s.indexOf(",")).toInt();
    data_num++; // increment our data counter
    s = s.substring(s.indexOf(",") + 1);
//  Serial.println(s);
  }
  swval[data_num] = s.toInt();
  Serial.print("Values filled from EPROM to array: ");
  for (int l = 0; l <= 8; l++) {
    Serial.print(swval[l]);
  }
  Serial.println();
  Serial.println();
 
//setting initial states of LEDs
   int ledarray[] = {LED0, LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8};
   for (int d = 0; d<=8; d++) {
  if (swval[d] == 1) {
    digitalWrite(ledarray[d], LOW);
    } else {
    digitalWrite(ledarray[d], HIGH);
    }
   }
} // void setup


void filewrite() {
  File f = SPIFFS.open("/relaydatafile.txt", "w");
  if (!f) {
    Serial.println("File open failed");
  }
  else {
    char swvalstring[18];
    sprintf(swvalstring, "%u,%u,%u,%u,%u,%u,%u,%u,%u,,", swval[0], swval[1], swval[2], swval[3], swval[4], swval[5], swval[6],  swval[7], swval[8]);
    Serial.println();
    Serial.println("Writing Relay States in EPROM..");
    f.print(swvalstring);
    f.close();
  }
} //void filewrite

void relayswitch(int i, int j) {
  if (swval[j] == 1) {
    digitalWrite(i, HIGH);
    swval[j] = 0;
    Serial.println(i + "Off");
  } else {
    digitalWrite(i, LOW);
    Serial.println(i + "On");
    swval[j] = 1;
    filewrite();
  }
}//void relayswitch

void rewifi() {
  if (WiFi.status() != WL_CONNECTED) {
    digitalWrite(errpin, LOW);
    Serial.println(errpin + "ERR Pin Low");
    delay(500);
    digitalWrite(errpin, HIGH);
    Serial.println(errpin + "ERR Pin High");
    WiFi.disconnect();
    WiFi.mode(WIFI_STA);
    delay(1000);
  }
}//void rewifi

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    digitalWrite(errpin, HIGH);
  } else {
    digitalWrite(errpin, LOW);
  }
  //reconnect wifi on button press
  if (digitalRead(wifibtn) == HIGH) {
    Serial.println(wifibtn + "WiFi Button HIGH");
    ESP.restart();
    return;
  }

  httpServer.handleClient();
 
  ArduinoOTA.handle();

  //ir receiveing code
  if (irrecv.decode(&results)) {
    unsigned int ircode = results.value;

    if (ircode == 16724175)    {
           relayswitch(LED1, 1);
    } else if (ircode == 16718055) {
           relayswitch(LED2, 2);
    } else if (ircode == 16743045) {
           relayswitch(LED3, 3);
    } else if (ircode == 16716015) {
            relayswitch(LED4, 4);
    } else if (ircode == 16726215) {
           relayswitch(LED5, 5);
    } else if (ircode == 16734885) {
           relayswitch(LED6, 6);
    } else if (ircode == 16728765) {
            relayswitch(LED7, 7);
    } else if (ircode == 16730805) {
            relayswitch(LED8, 8);
    } else if (ircode == 16753245) { // power button in ir remote to reconnect wifi
            rewifi();
    }
    irrecv.resume();  // Receive the next value
  }
  delay(100);

  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
 String request = client.readStringUntil('\r');
 client.flush();

  // Set LED according to the request
  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.println("<meta name = \"viewport\" content = \"width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0\">");
  client.println("<title>ESP8266B1</title>");
  client.println("<style>");
  client.println("\"body { background-color: #808080; font-family: Verdana, Geneva, sans-serif; Color: #000000; }\"");
  client.println(".button {background-color: #4CAF50; border: none; color: white; padding: 10px 18px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 8px;}");
  client.println(".buttonon {background-color: white;color: black;border: 2px solid #4CAF50;}");
  client.println(".buttonoff {background-color: white;color: black;border: 2px solid #f44336;}");
  client.println(".circlered {height: 10px; width: 10px; background-color: #f44336; border-radius: 80%;}");
  client.println(".circlegreen {height: 10px; width: 10px; background-color: #4CAF50; border-radius: 80%;}");
  client.println("</style>");
  client.println("</style>");
  client.println("</head>");
  client.println("<body>");
  client.println("<h1>");
  client.println("<font color=\"#808000\">Home | B1 | SwitchBoard-1</font>");
  client.println("</h1>");
  client.println("<P>");
  client.println("Connected to AP: ");
  client.println(WiFi.SSID());   
  client.print(", IP: ");
  client.println(WiFi.localIP());
  client.println("<br><br>");

  // Return the response LED1
  client.print("Switch 1: ");
  if (request.indexOf("/LED1=ON") != -1)  {
    digitalWrite(LED1, LOW);
    swval[1] = 1;
  }
  if (request.indexOf("/LED1=OFF") != -1)  {
    digitalWrite(LED1, HIGH);
    swval[1] = 0;
  }
  if (swval[1] == 1) {
    client.println("<a href=\"/LED1=OFF\"\"><button class=\"button buttonoff\">OFF</button></a><br>");
  }
  if (swval[1] == 0) {
     client.println("<a href=\"/LED1=ON\"\"><button class=\"button buttonon\"> ON </button></a>    ");
  }
  client.println("<P>");



  // Return the response LED2
   client.print("Switch 2: ");
  if (request.indexOf("/LED2=ON") != -1)  {
    digitalWrite(LED2, LOW);
    swval[2] = 1;
  }
  if (request.indexOf("/LED2=OFF") != -1)  {
    digitalWrite(LED2, HIGH);
    swval[2] = 0;
  }
  if (swval[2] == 1) {
    client.println("<a href=\"/LED2=OFF\"\"><button class=\"button buttonoff\">OFF</button></a><br>");
  }
  if (swval[2] == 0) {
   client.println("<a href=\"/LED2=ON\"\"><button class=\"button buttonon\"> ON </button></a>    ");
  }
  client.println("<P>");


  // Return the response LED3
  client.print("Switch 3: ");
  if (request.indexOf("/LED3=ON") != -1)  {
    digitalWrite(LED3, LOW);
    swval[3] = 1;
  }
  if (request.indexOf("/LED3=OFF") != -1)  {
    digitalWrite(LED3, HIGH);
    swval[3] = 0;
  }
  if (swval[3] == 1) {
    client.println("<a href=\"/LED3=OFF\"\"><button class=\"button buttonoff\">OFF</button></a><br>");
  }
  if (swval[3] == 0) {
     client.println("<a href=\"/LED3=ON\"\"><button class=\"button buttonon\"> ON </button></a>    ");
  }
  client.println("<P>");


  // Return the response LED4
  client.print("Switch 4: ");
  // Control LED 4
  if (request.indexOf("/LED4=ON") != -1)  {
    digitalWrite(LED4, LOW);
    swval[4] = 1;
  }
  if (request.indexOf("/LED4=OFF") != -1)  {
    digitalWrite(LED4, HIGH);
    swval[4] = 0;
  }
  if (swval[4] == 1) {
    client.println("<a href=\"/LED4=OFF\"\"><button class=\"button buttonoff\">OFF</button></a><br>");
  }
  if (swval[4] == 0) {
   client.println("<a href=\"/LED4=ON\"\"><button class=\"button buttonon\"> ON </button></a>    ");
  }
  client.println("<P>");


  // Return the response LED5
 client.print("Switch 5: ");
  if (request.indexOf("/LED5=ON") != -1)  {
    digitalWrite(LED5, LOW);
    swval[5] = 1;
  }
  if (request.indexOf("/LED5=OFF") != -1)  {
    digitalWrite(LED5, HIGH);
    swval[5] = 0;
  }
  if (swval[5] == 1) {
    client.println("<a href=\"/LED5=OFF\"\"><button class=\"button buttonoff\">OFF</button></a><br>");
  }
  if (swval[5] == 0) {
     client.println("<a href=\"/LED5=ON\"\"><button class=\"button buttonon\"> ON </button></a>    ");
  }
  client.println("<P>");


  // Return the response LED6
  client.print("Switch 6: ");
  if (request.indexOf("/LED6=ON") != -1)  {
    digitalWrite(LED6, LOW);
    swval[6] = 1;
  }
  if (request.indexOf("/LED6=OFF") != -1)  {
    digitalWrite(LED6, HIGH);
    swval[6] = 0;
  }
  if (swval[6] == 1) {
    client.println("<a href=\"/LED6=OFF\"\"><button class=\"button buttonoff\">OFF</button></a><br>");
  }
  if (swval[6] == 0) {
    client.println("<a href=\"/LED6=ON\"\"><button class=\"button buttonon\"> ON </button></a>    ");
  }
  client.println("<P>");


  // Return the response LED7
  client.print("Switch 7: ");
  if (request.indexOf("/LED7=ON") != -1)  {
    digitalWrite(LED7, LOW);
    swval[7] = 1;
  }
  if (request.indexOf("/LED7=OFF") != -1)  {
    digitalWrite(LED7, HIGH);
    swval[7] = 0;
  }
  if (swval[7] == 1) {
    client.println("<a href=\"/LED7=OFF\"\"><button class=\"button buttonoff\">OFF</button></a><br>");
  }
  if (swval[7] == 0) {
   client.println("<a href=\"/LED7=ON\"\"><button class=\"button buttonon\"> ON </button></a>    ");
  }
  client.println("<P>");


  // Return the response LED8
  client.print("Switch 8: ");
  if (request.indexOf("/LED8=ON") != -1)  {
    digitalWrite(LED8, LOW);
    swval[8] = 1;
  }
  if (request.indexOf("/LED8=OFF") != -1)  {
    digitalWrite(LED8, HIGH);
    swval[8] = 0;
  }
  if (swval[8] == 1) {
    client.println("<a href=\"/LED8=OFF\"\"><button class=\"button buttonoff\">OFF</button></a><br>");
  }
  if (swval[8] == 0) {
    client.println("<a href=\"/LED8=ON\"\"><button class=\"button buttonon\"> ON </button></a>    ");
  }
 
  filewrite();
  client.println("<P>");
  client.println("<P>");
  client.println("<br><br>");
  client.println("Hardware:WeMOS D1 mini");
  client.println("<br>");
  client.print("Firmware update: http://");
  client.print(WiFi.localIP()); 
  client.print(":88/update");
  client.println("<br>");
  client.println("</html>");
  delay(100);
 
}// void loop End