esp8266 restarting

John09
Posts: 4
Joined: Mon Jan 15, 2024 9:17 pm

esp8266 restarting

Postby John09 » 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);

}

bobtidey
Posts: 43
Joined: Mon Jun 18, 2018 2:24 pm

Re: esp8266 restarting

Postby bobtidey » Mon Jan 22, 2024 5:49 pm

There is nothing in that code that would cause restarts. If code is going to cause a restart then it is normally down to long delays in the loop() which then causes the watchdog to trigger. If you monitor the serial port then you would get the restart code which will say why a restart occurred.

A more likely cause would be a hardware issue. A power supply that can't deliver the current spikes that an esp8266 draws will cause restarts. These can be up to 500mA so a supply that can handle these must be used. The leads from the power supply to the esp8266 should be low resistance and a local decoupling capacitor near the board will help avoid power issues. Glitches into the reset or EN lines will also trigger restarts so these should have decent pull ups if not already present on the module.

Who is online

Users browsing this forum: No registered users and 2 guests