esp8266 restarting
Posted: Mon Jan 15, 2024 9:24 pm
I am running a couple of systems running esp8266. They all have the same backbone code as far as connecting to wifi, running server and otp. The problem i am having is that those systems all seem to restart occasionaly. I am trying to figure out why they restart. For instance, sometimes they will run only about 8 hours and restart, then another time they will run for days. Can you spot anything in my code that would make them crash or restart. I removed everything apart from basics from the code. What max uptime should i reach with this code and what could interrupt this and crash/restart it ?
Code: Select all
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "ESP8266WebServer.h"
#include <math.h>
String wifiStatus = "";
ESP8266WebServer server(80);
// time related
int time_now = 10000;
int period = 0;
int periodOTA = 0;
int programLogicExecuted = 0;
String currentTime = "";
void setup() {
connectToWifi();
delay(5000);
startServer();
setupOTA();
}
void loop() {
int days = millis()/1000/60/60/24;
int hours = (millis()/1000/60/60) - (days*24);
int minutes = (millis()/1000/60) - (days*24*60) - (hours*60);
int seconds = (millis()/1000) - (days*24*60*60) - (hours*60*60) - (minutes*60);
currentTime = String(days) + "d " + String(hours) + "h " + String(minutes) + "m " + String(seconds) + "s";
if (millis() > time_now + period) {
time_now = millis();
period = 10000;
checkWifiStatus();
programLogicExecuted++;
}
ArduinoOTA.handle();
server.handleClient();
}
void programLogic() {
}
void checkWifiStatus() {
if (WiFi.status() == WL_CONNECTED) return;
else {
connectToWifi();
setupOTA();
startServer();
}
}
int connectToWifi() {
WiFi.mode(WIFI_STA);
WiFi.begin("MYWIFISSD", "MYWIFIPASSWORD");
IPAddress ip(192, 168, 64, 52);
IPAddress gateway(192, 168, 64, 1);
IPAddress subnet(255, 255, 255, 0);
WiFi.config(ip, gateway, subnet);
}
void startServer() {
server.on("/", handleRootPath);
server.on("/data.html", handleRootPath);
server.begin();
}
void setupOTA() {
ArduinoOTA.begin();
}
void handleRootPath() {
String itext =
"Running: " + String(currentTime) + "\n" +
"Program_Logic_Executed: " + String(programLogicExecuted) + "\n\n";
server.send(200, "text/plain", itext);
}