-1

My web server running on ESP8266, stop working after while. I cannot connect to it - browser shows connection time out error. No error, no crash... nothing.

Have you any idea how to fix it?

My code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ESP8266mDNS.h>
#include <FS.h>

#include "console.h"
#include "tictactoe.h"
#include "main.h"

#define L_GREEN 2
#define L_YELLOW 0
#define L_ORANGE 4
#define L_RED 5

const char* ssid = "***";
const char* password = "***";

int requests = 0;
String IPs = "[";

IPAddress staticIP86_10(192,168,0,115);
IPAddress gateway86_10(192,168,0,1);
IPAddress subnet86_10(255,255,255,0);

ESP8266WebServer server(80); //Server on port 80

String getContentType(String filename);
bool handleFileRead(String path);

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "1.sk.pool.ntp.org", 60*60, 60*1000);

//===============================================================
// Game Data
//===============================================================

/*String ticPlayer1 = "";
String ticPlayer2 = "";
String ticClick = "-1";
int ticReset = 0;*/

//===============================================================
// LEDs
//===============================================================
void LEDblink(int pin) {
  requests++;
  digitalWrite(pin, HIGH);
  delay(10);
  digitalWrite(pin, LOW);
}
//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
void handleNotFound(){
  if(server.uri().indexOf(".php")>-1) {
    Serial.print("404 "+server.uri()+": "+server.client().remoteIP().toString()+" (Blocked)\n");
    return;
  }
  Serial.print("404 "+server.uri()+": "+server.client().remoteIP().toString()+"\n");
  String s = HTML_Pro_notFound;
  server.send(404, "text/html", s);

  LEDblink(L_YELLOW);
}
void sendFile(String path, String type) {
  Serial.print(server.client().remoteIP().toString()+": "+server.uri()+"\n");
  if (SPIFFS.exists(path)) {                            // If the file exists
    File file = SPIFFS.open(path, "r");                 // Open it
    size_t sent = server.streamFile(file, type);        // And send it to the client
    file.close();                                       // Then close the file again
  }
}
//===============================================================
void reboot() {
 server.send(200, "text/plain", "true");
 LEDblink(L_RED);
 digitalWrite(15, LOW);
 digitalWrite(0, HIGH);
 digitalWrite(2, HIGH);
 ESP.restart();
}
void admin() {
 if(!server.hasArg("user") || !server.hasArg("pass") || server.arg("user") == NULL || server.arg("pass") == NULL || server.arg("request")=="login") {
    String expire = "";
    if(server.arg("remember")=="on"){expire="30";}else{expire="0";}
    server.send(200, "text/html", "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>ProTools.tk | Please Wait</title><link rel='stylesheet' type='text/css' href='/main.css'><script src='/cookies.js'></script></head><body style='background:#2d2d2d'><form class='center' action='/admin' method='post' style='display:none;'><input class='a' type='text' name='user'><input class='a' type='password' name='pass'><input class='a' type='text' name='request' value='panel'><button class='a' type='submit'></button></form><div class='loading center' style='margin-top:150px'></div><script type='text/javascript'>var u = '"+server.arg("user")+"';var p = '"+server.arg("pass")+"';if(u.length > 7 && p.length > 7) {delCookie('user');delCookie('pass');setCookie('user', u, "+expire+");setCookie('pass', p, "+expire+");}if(getCookie('user') && getCookie('user')) {var a=document.getElementsByClassName('a');a[0].value=getCookie('user');a[1].value=getCookie('pass');a[3].click();}else{location.replace('/login');}</script></body></html>");
    //server.sendHeader("Location","/login");
    //server.send(303);
 }
 if((server.arg("user")=="***")&&(server.arg("pass")=="***")) {
   if(server.arg("request")=="data") {
     String c = "{\"uptime\":"+String(millis(),DEC)+",\"req\":"+String(requests)+",\"ips\":"+IPs+"]}";
     server.send(200, "text/json", c);
   }
   else if(server.arg("request")=="panel") {
     server.send(200, "text/plain", "Admin panel here");
   }
   else {
    server.send(400, "text/plain", "400 Bad Request");
   }
 }
 else{
   server.sendHeader("Location","/login");
   server.send(303);
 }
 LEDblink(L_RED);
}
void console() {
 Serial.print("Console :"+server.client().remoteIP().toString()+"\n");
 String s = HTML_console;
 server.send(200, "text/html", s);
 LEDblink(L_ORANGE);
}

//===============================================================
/*void minesPage() {
 String s = HTML_mines;
 server.send(200, "text/html", s);
 LEDblink(L_YELLOW);
}
void minesScript() {
 String s = JS_mines;
 server.send(200, "text/javascript", s);
 LEDblink(L_YELLOW);
}
void minesStyle() {
 String s = CSS_mines;
 server.send(200, "text/css", s);
 LEDblink(L_YELLOW);
}*/

//===============================================================

/*void ticPage() {
 String s = HTML_tictactoe;
 server.send(200, "text/html", s);
 LEDblink(L_YELLOW);
}
void ticScript() {
 String s = JS_tictactoe;
 server.send(200, "text/javascript", s);
 LEDblink(L_YELLOW);
}
void ticStyle() {
 String s = CSS_tictactoe;
 server.send(200, "text/css", s);
 LEDblink(L_YELLOW);
}

void ticData() {
 Serial.print("TicTacToe: "+server.client().remoteIP().toString()+"\n");
 String user = server.arg("user");
 String clicked = server.arg("click");
 String request = server.arg("request");
 String leave = server.arg("leave");
 if(user != "") {
  if(user == "get") {
   server.send(200, "text/plain", "{\"Player1\":\""+ticPlayer1+"\", \"Player2\":\""+ticPlayer2+"\"}");
  }
  else if(ticPlayer1 == "") {
   ticPlayer1 = user;
   server.send(200, "text/plain", "player1");
  }
  else if(ticPlayer2 == "") {
   ticPlayer2 = user;
   server.send(200, "text/plain", "player2");
  }
  else {
   server.send(200, "text/plain", "Full");
  }
  if(ticPlayer1 != "" && ticPlayer2 != "") {
   ticClick = "0";
  }
  digitalWrite(L_ORANGE,HIGH);
  delay(500);
  digitalWrite(L_ORANGE,LOW);
 }
 else if(leave != "") {
  if(leave == ticPlayer1) {
   ticPlayer1 = "";
  }
  else if(leave == ticPlayer2) {
   ticPlayer2 = "";
  }
  ticClick = "-1";
  server.send(200, "text/plain", leave+" left");
 }
 else if(clicked != "") {
  ticClick = clicked;
  server.send(200, "text/plain", "OK: "+ticClick);
 }
 else if(request != "") {
  if(request == "checkClicked") {
   server.send(200, "text/plain", ticClick);
  }
  if(request == "reset") {
   ticReset++;
   if(ticReset == 2) {
    ticPlayer1 = "";
    ticPlayer2 = "";
    ticClick = "-1";
    ticReset = 0;
    server.send(200, "text/plain", "Reseted");
   }
   else {
    server.send(200, "text/plain", "Verification required");
   }
  }
 }
 LEDblink(L_YELLOW);
}*/
//==============================================================
//                  SETUP
//==============================================================
void setup(void){
  Serial.begin(9600);

  //Onboard LED port Direction output
  pinMode(L_GREEN,OUTPUT);
  pinMode(L_YELLOW,OUTPUT);
  pinMode(L_ORANGE,OUTPUT);
  pinMode(L_RED,OUTPUT);

  digitalWrite(L_RED, HIGH);

  WiFi.hostname("ESP8266");

  WiFi.begin(ssid, password);     //Connect to your WiFi router

  MDNS.begin("ESP8266");
  MDNS.addService("http", "tcp", 80);

  digitalWrite(L_RED, LOW);
  // Wait for connection
  digitalWrite(L_ORANGE, HIGH);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  digitalWrite(L_ORANGE, LOW);
  digitalWrite(L_YELLOW, HIGH);

  WiFi.config(staticIP86_10, gateway86_10, subnet86_10);

  //If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP

  SPIFFS.begin();

  server.on("/", []() {                                          // root
    sendFile("/page.html", "text/html");
    LEDblink(L_YELLOW);
  });
  server.on("/main.css", []() {                                  // main.css
    sendFile("/main.css", "text/css");
    LEDblink(L_ORANGE);
  });
  server.on("/page.css", []() {                                  // page.css
    sendFile("/page.css", "text/css");
    LEDblink(L_ORANGE);
  });
  server.on("/page.js", []() {                                   // page.js
    sendFile("/page.js", "text/javascript");
    String ip = server.client().remoteIP().toString();
    if(IPs=="["){
      IPs+="{\"time\":"+String(timeClient.getEpochTime(),DEC)+",\"ip\":\""+ip+"\"}";
    }else{
      IPs+=",{\"time\":"+String(timeClient.getEpochTime(),DEC)+",\"ip\":\""+ip+"\"}";
    }
    LEDblink(L_YELLOW);
  });
  server.on("/login/style.css", []() {                           // login/style.css
    sendFile("/login/style.css", "text/css");
    LEDblink(L_ORANGE);
  });
  server.on("/cookies.js", []() {                                // cookies.js
    sendFile("/cookies.js", "text/javascript");
    LEDblink(L_ORANGE);
  });

  server.on("/translator", []() {                                // translator
    sendFile("/translator/page.html", "text/html");
    LEDblink(L_ORANGE);
  });
  server.on("/translator/style.css", []() {                      // translator/style.css
    sendFile("/translator/style.css", "text/css");
    LEDblink(L_ORANGE);
  });
  server.on("/translator/script.js", []() {                      // translator/script.js
    sendFile("/translator/script.js", "text/javascript");
    LEDblink(L_ORANGE);
  });

  server.on("/login", []() {                                     // login
    sendFile("/login/page.html", "text/html");
    LEDblink(L_ORANGE);
  });
  server.on("/admin", admin);
  server.on("/reboot", reboot);
  server.on("/console", console);

  /*server.on("/minesweeper", minesPage);
  server.on("/minesweeper/script.js", minesScript);
  server.on("/minesweeper/style.css", minesStyle);

  server.on("/tictactoe", ticPage);
  server.on("/tictactoe/script.js", ticScript);
  server.on("/tictactoe/style.css", ticStyle);
  server.on("/tictactoe/data", ticData);*/

  server.onNotFound(handleNotFound);

  server.begin();                  //Start server
  timeClient.begin();
  Serial.println("HTTP server started");
  digitalWrite(L_YELLOW, LOW);
  digitalWrite(L_GREEN, HIGH);
}
//==============================================================
//                     LOOP
//==============================================================
void loop(void){
  timeClient.update();
  server.handleClient();          //Handle client requests
}
  • what version of the esp8266 arduino package you use? – Juraj Nov 10 '18 at 17:41
  • hmm... I am not sure what version do you mean but in Arduino Board Manager I have installed ESP8266 of version 2.0.0 Please specify more where I can find that version if this is not what are you want. – MataxePlay Nov 10 '18 at 18:02
  • What mime type is text/plane? Are you trying to transfer an aircraft? – Majenko Nov 10 '18 at 18:06
  • use the latest version 2.4.2 – Juraj Nov 10 '18 at 18:12
  • @Majenko oops :D thanks for warning! I wrote it wrong and copy it to whole code, but its works good xD – MataxePlay Nov 10 '18 at 18:14
1

Use the latest version of Arduino esp8266 core package (2.4.2 or higher).

  • @MataxePlay, you could accept the answer to mark the question solved – Juraj Nov 30 '18 at 20:52

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.