Esp8266 / Sonoff freeze after some hour or some days, randomly

Hi to all!

I have a big problem both with two esp8266 nodemcu 12E, and with a sonoff basic R2 too (chip identified by esptool.py in esp8285). The problem is that in a random way the boards are blocked, no longer communicating with the blynk server, and not even physically reacting to the various hardware input on gpio (button), so it seems that the whole system crashes. This happens after an hour, like after two days. Everything works perfectly when it goes. This is my code, quite simple (maybe not edited perfectly).

Blynk version 0.54
esp8266 version board: 2.4.2

Initially I was thinking of a problem of powering the nodemcu (with a micro usb power supply of a mobile phone), so I took a sonoff basic because it was powered directly at 220V, but unsuccesfully!

Has it happened to others that esp8266 get stuck?
I don’t know where to hit my head anymore :frowning_face:
Thanks!


#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN2 3
#define DHTTYPE DHT11
DHT dht2(DHTPIN2, DHTTYPE);

char auth[] = "xxxx";
char ssid[] = "xxxx";
char pass[] = "xxxx";

int relay1 = 12;
int ledboard = 13;
int pulsante = 0;
int switch1 = 1;
int relayState = HIGH;
int inMovimento = LOW;
int ledStop = LOW;
int cancelloON = LOW;
int ledState = LOW;

unsigned long previousMillis = 0;
volatile bool pinChanged = false;
volatile int  pinValue   = 0;

SimpleTimer timer;
SimpleTimer tripWire;

WidgetLED led1(V80); 
WidgetLED led2(V81); 
WidgetLED led3(V82); 
WidgetLED ledb(V83); 


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  dht2.begin();
   
  while (Blynk.connect() == false) {
    // Wait until connected
  }

  timer.setInterval(30000L, sendDHT);         // Temperature sensor DHT "polling" interval.
  timer.setInterval(864000000L, ressystem);   //auto reset 
  timer.setInterval(1005L, blinkLedB);   //blink led widget
      
  pinMode(relay1, OUTPUT);
  pinMode(ledboard, OUTPUT);
  pinMode(pulsante, INPUT_PULLUP);
  pinMode(switch1, INPUT_PULLUP);

  inMovimento = LOW; 
  ledStop = LOW;
  led1.off();
  led2.off();
  led3.off();
  digitalWrite(relay1, LOW);

  attachInterrupt(digitalPinToInterrupt(pulsante), checkPin, CHANGE);
    
  Blynk.setProperty(V85, "onLabel", "APRI/CHIUDI");
  Blynk.setProperty(V85, "offLabel", "APRI/CHIUDI");
  
  
}


void loop()
{
  Blynk.run();
  timer.run();
  tripWire.run();

if (inMovimento == HIGH) { //faster blinking of led on board when open the door
  const long interval = 200;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
  previousMillis = currentMillis;
  if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledboard, ledState);
    }  
    }

if (inMovimento == LOW) { //slow blinking of led on board when the system is waiting a command
  const long interval = 600;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
  previousMillis = currentMillis;
  if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledboard, ledState);
    }  
    }

if (pinChanged) { //read the switch on board at gpio0 for run the code for open or close the door
if (pinValue) {
    digitalWrite(relay1, HIGH);
    led1.on();
    inMovimento = HIGH;
    Blynk.setProperty(V85, "onLabel", "IN MOVIM");
    Blynk.setProperty(V85, "offLabel", "IN MOVIM");
    tripWire.setTimeout(700L, tripDeactivateOPEN);
    tripWire.setTimeout(15000L, tripnonmovimento);
    } 
    pinChanged = false;
    }
    
if (digitalRead(switch1) == LOW) { //read a magnetic contact for tell me in the widget app if the door is opened or closed
        led3.off();
        } else {
        led3.on();
        }     
}

BLYNK_WRITE(V85) { //switch widget for open/close door
  int read1 = param.asInt();
    if (read1 == HIGH && inMovimento == LOW) {
        Serial.println(switch1);
        digitalWrite(relay1, HIGH);
        led1.on();
        inMovimento = HIGH;
        Blynk.setProperty(V85, "onLabel", "IN MOVIM");
        Blynk.setProperty(V85, "offLabel", "IN MOVIM");
        tripWire.setTimeout(700L, tripDeactivateOPEN);
        tripWire.setTimeout(15000L, tripnonmovimento);
        }
    }

void tripDeactivateOPEN() {    
       digitalWrite(relay1, LOW);
       }
    
void tripnonmovimento() {
  led1.off();
  inMovimento = LOW;
  Blynk.setProperty(V85, "onLabel", "APRI/CHIUDI");
  Blynk.setProperty(V85, "offLabel", "APRI/CHIUDI");
  }
  
void checkPin() { //read the switch on board
  pinValue = !digitalRead(pulsante);
  pinChanged = true;
}
  
void sendDHT(){ //read the sensor DHT11 and write in app widget
  
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();
  if (isnan(h2) || isnan(t2)) { 
  return; 
  }
  Blynk.virtualWrite(V87, h2);
  Blynk.virtualWrite(V86, t2); 
 }

void blinkLedB() //blink of a widget led on app
{
  if (ledb.getValue()) {
    ledb.off();
    } else {
    ledb.on();
    }
}

void ressystem(){ //programmed restart with timer
 ESP.restart(); 
}

You should read this…

Pete.

I need to check a pin in real time… Can you help me to put out of loop? Ty

Anyway, it is not only a loop problem, because it freeze with this code too:


#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>
#include <DHT_U.h>

#define ONE_WIRE_BUS 2          
#define DHTPIN2 5
#define DHTTYPE DHT11
#define COLOR_fredda "#66CCFF" // Dark Blue
#define COLOR_tiepida "#FFD800" // Arancio chiaro
#define COLOR_calda "#FFA500" // Arancio scuro
#define COLOR_bollente "#FF0000" // Red
#define COLOR_grigio "#202020" // grigio scuro

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);


DeviceAddress sensor1 = { 0x28, 0x80, 0x80, 0x45, 0x92, 0x0F, 0x02, 0x9D };
DeviceAddress sensor2 = { 0x28, 0xB0, 0x93, 0x45, 0x92, 0x03, 0x02, 0x0F };
DeviceAddress sensor3 = { 0x28, 0x41, 0x37, 0x45, 0x92, 0x17, 0x02, 0xB1 };
DeviceAddress sensor4 = { 0x28, 0xE5, 0x22, 0x45, 0x92, 0x12, 0x02, 0x60 };
DeviceAddress sensor5 = { 0x28, 0xEE, 0x5C, 0x45, 0x92, 0x02, 0x02, 0xBB };


DHT dht2(DHTPIN2, DHTTYPE);

char auth[] = "xx";
char ssid[] = "xx";
char pass[] = "xx";

SimpleTimer timer;
SimpleTimer tripWire;

float floatTemp1 = 0;
float floatTemp2 = 0;
float floatTemp3 = 0;
float floatTemp4 = 0;
float floatTemp5 = 0;
int led = LOW;

WidgetLED ledb(V51); //online caldaia

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  
  while (Blynk.connect() == false) {
    // Wait until connected
  }

  timer.setInterval(2005L, sendTemps);       // Temperature sensor "polling" interval.
  timer.setInterval(10000L, sendDHT);         // Temperature sensor DHT "polling" interval.
  timer.setInterval(18000000L, ressystem);   //reset ogni 5 ore 
  timer.setInterval(1000L, blinkLedB);   
    
  sensors.begin();
  sensors.setResolution(12);                // More on resolution: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/
  dht2.begin();
  
  led = LOW; 
}


void blinkLedB()
{
  if (ledb.getValue()) {
    ledb.off();
    } else {
    ledb.on();
    }
}


void sendTemps()
{
  sensors.requestTemperatures();                  // Polls the sensors.

  float floatTemp1 = sensors.getTempC(sensor1);
  char t_buffer1[15];
  dtostrf(floatTemp1, 3, 1, t_buffer1);
  Blynk.virtualWrite(1, t_buffer1);

  float floatTemp2 = sensors.getTempC(sensor2);
  char t_buffer2[15];
  dtostrf(floatTemp2, 3, 1, t_buffer2);
  Blynk.virtualWrite(2, t_buffer2);

  float floatTemp3 = sensors.getTempC(sensor3);
  char t_buffer3[15];
  dtostrf(floatTemp3, 3, 1, t_buffer3);
  Blynk.virtualWrite(3, t_buffer3);

  float floatTemp4 = sensors.getTempC(sensor4);
  char t_buffer4[15];
  dtostrf(floatTemp4, 3, 1, t_buffer4);
  Blynk.virtualWrite(4, t_buffer4);

  float floatTemp5 = sensors.getTempC(sensor5);
  char t_buffer5[15];
  dtostrf(floatTemp5, 3, 1, t_buffer5);
  Blynk.virtualWrite(5, t_buffer5);
  
 if (floatTemp1 <= -15) {
      Blynk.setProperty(V1, "color", COLOR_grigio);
      }
    else if (floatTemp1 >= -14 && (floatTemp1 <= 24)) {
      Blynk.setProperty(V1, "color", COLOR_fredda);
      }
    else if (floatTemp1 >= 25 && (floatTemp1 <= 34)) {
      Blynk.setProperty(V1, "color", COLOR_tiepida);
      }
    else if (floatTemp1 >= 35 && (floatTemp1 <= 44)) {
      Blynk.setProperty(V1, "color", COLOR_calda);
      }
    else if (floatTemp1 >= 45) {
      Blynk.setProperty(V1, "color", COLOR_bollente);
      }
    

 if (floatTemp2 <= -15) {
      Blynk.setProperty(V2, "color", COLOR_grigio);
      }
    else if (floatTemp2 >= -14 && (floatTemp2 <= 24)) {
      Blynk.setProperty(V2, "color", COLOR_fredda);
      }
    else if (floatTemp2 >= 25 && (floatTemp2 <= 34)) {
      Blynk.setProperty(V2, "color", COLOR_tiepida);
      }
    else if (floatTemp2 >= 35 && (floatTemp2 <= 44)) {
      Blynk.setProperty(V2, "color", COLOR_calda);
      }
    else if (floatTemp2 >= 45) {
      Blynk.setProperty(V2, "color", COLOR_bollente);
      }

 if (floatTemp3 <= -15) {
      Blynk.setProperty(V3, "color", COLOR_grigio);
      }
    else if (floatTemp3 >= -14 && (floatTemp3 <= 24)) {
      Blynk.setProperty(V3, "color", COLOR_fredda);
      }
    else if (floatTemp3 >= 25 && (floatTemp3 <= 34)) {
      Blynk.setProperty(V3, "color", COLOR_tiepida);
      }
    else if (floatTemp3 >= 35 && (floatTemp3 <= 44)) {
      Blynk.setProperty(V3, "color", COLOR_calda);
      }
    else if (floatTemp3 >= 45) {
      Blynk.setProperty(V3, "color", COLOR_bollente);
      }
  
 if (floatTemp4 <= -15) {
      Blynk.setProperty(V4, "color", COLOR_grigio);
      }
    else if (floatTemp4 >= -14 && (floatTemp4 <= 24)) {
      Blynk.setProperty(V4, "color", COLOR_fredda);
      }
    else if (floatTemp4 >= 25 && (floatTemp4 <= 34)) {
      Blynk.setProperty(V4, "color", COLOR_tiepida);
      }
    else if (floatTemp4 >= 35 && (floatTemp4 <= 44)) {
      Blynk.setProperty(V4, "color", COLOR_calda);
      }
    else if (floatTemp4 >= 45) {
      Blynk.setProperty(V4, "color", COLOR_bollente);
      }

 if (floatTemp5 <= -15) {
      Blynk.setProperty(V5, "color", COLOR_grigio);
      }
    else if (floatTemp5 >= -14 && (floatTemp5 <= 24)) {
      Blynk.setProperty(V5, "color", COLOR_fredda);
      }
    else if (floatTemp5 >= 25 && (floatTemp5 <= 34)) {
      Blynk.setProperty(V5, "color", COLOR_tiepida);
      }
    else if (floatTemp5 >= 35 && (floatTemp5 <= 44)) {
      Blynk.setProperty(V5, "color", COLOR_calda);
      }
    else if (floatTemp5 >= 45) {
      Blynk.setProperty(V5, "color", COLOR_bollente);
      }   
          
}

  
void sendDHT(){ 
  
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();
  if (isnan(h2) || isnan(t2)) { 
  return; 
  }
  Blynk.virtualWrite(V22, h2);
  Blynk.virtualWrite(V23, t2); 
  }

void ressystem(){
 ESP.restart(); 
 }

void loop()
{
  Blynk.run();
  timer.run();
}

Okay, things I would try…

Make sure that you’re using the libraries and the latest Arduino core.

Check your physical connections and maybe try a different power supply.

Upload the simple Blynk blink sketch. If this is okay then it’s not a hardware, power supply or networking issue

Comment-out the DHT libraries and all the code that calls them, and the timer that calls the routine. Hard-code some dummy temperature readings, so you have something to send to Blynk, then try again. If that is okay then try a different DHT library.

Pete.

1 Like

Ok, i will try these step, thanks

You might consider adding a Serial.print to the beginning and end of each timer routine. This will tell you how much time you’re spending in each routine (in milliseconds). And if the Sonoff always freezes in the same routine, it may help you to isolate the problem. Just a suggestion. Good luck.

void sendDHT(void) { 
   unsigned long time = millis();
   Serial.print("sendDHT: ");
   // …
   Serial.println(millis() - time);
}

void ressystem(void) {
   unsigned long time = millis();
   Serial.print("ressystem: ");
   // …
   Serial.println(millis() - time);
}

void blinkLedB(void) {
   unsigned long time = millis();
   Serial.print("blinkLedB: ");
   // …
   Serial.println(millis() - time);
}
2 Likes

I have some updates. I am increasingly convinced that the problem of the two esp that are blocked is due to an unstable power supply. My initial test was to use a sonoff that would have eliminated any doubt with its proprietary power supply. But then that also blocked. I immediately discovered that using the gpio1 connected to GND (I use it with a magnetic contact normally closed to GND), restarting the sonoff did not start the sketch (perhaps it entered flash mode or uart). Maybe at the time of the block there was an automatic reset of sonoff or the power went off, I don’t know (I was away when it happened). When removed the contact from GND, it is immediately restarted and now we are 3 consecutive days without a block. While the two esp8266 12e (with the same sketch loaded) are blocked almost daily. Now I have to wait for other days, but if the sonoff remains online for two weeks, it is most likely just a power problem. Thanks to your advice I have so many changes to make to the sketch but unfortunately to do these tests I have to wait days with the device always on, waiting for the block. Meanwhile, thank you for the valuable information.

1 Like

I’m continuing with the newbie-experiments :slight_smile:

As I have already said, at the moment I am using a single token with 3 different devices (2 esp8266 + 1 sonoff basic). And as already written, 1 esp8266 freezes every 3/4 hours and most likely it is due to an unstable power supply. ok!

A strange thing happened this morning: the sonoff basic (and the usual esp8266) on the app were both offline, while the other esp was online (I have a led widget on app that flashes during the normal operation of each individual device)!

After finishing dressing the children, (about half an hour later) I checked again and nothing had changed. I reset the blocked esp8266 as I usually do to restart it, and magically the sonoff is back online (immediately). Was it a coincidence? Is it possible that using a single token on multiple devices can cause problems if only one device is blocked for any reason? Obviously I’m using different VPINs on each device.

Needless to say, the next thing to do is fix the power supply and clean the sketch + loop() (as I did on the sonoff basic thanks to the suggestions I had on this community) of both esp.

But it was interesting to understand if using a token on multiple devices is contraindicated or if it was just a coincidence. :slight_smile:

I don’t think you’ve mentioned the single token before.
It’s not how Blynk is meant to be used, 1 device = 1 auth code (token).

using Blynk correctly would mean that you don’t have to worry about the online LED either, the Blynk app takes care of this for you, showing offline status for each device at the top of the screen.
When using multiple devices, you can mix widgets attached to different devices on the same screen, so there’s no reason not to do it this way.

If you want to exchange data between devices then you’ll need to use Bridge, but I’m not sure that’s a requirement in your case.

Pete.

I thought I said it, but no, you’re right. Sorry! Using the same token seemed the logical thing to me! I had read about bridge and it seemed too complicated (I’m a newbie, you know :slight_smile:) and then I used the same token (also because it seemed to work well). Now I have just created a second and a third token (as your advice) and I saw that it is really simple and more orderly !! As always, thanks a lot! ps: having a single token, even if wrong, can still give problems in practice?

I don’t know if using one auth code on multiple devices can cause a problem.
I’ve used two devices with the same auth code in the past, before I realised that it wasn’t the best thing to do, but that was with a much older version of the library and cloud server.

See if using one auth code per device solves your problems and report back with your findings.

Pete.

1 Like

Sure, I do some tests and let you know. PS: In five days, in my small way, I discovered so many new things. Thanks again!

1 Like

Other updates. Sorry if I do it long but I wanted to tell you this story that made me crazy.

I begin immediately to say that the main problem with this story is not having a computer at home, so I can’t read the serial. But it’s not a problem since I do everything in the office, I try the system for a few hours and then I take it and try it at home.

The title of this discussion is: “Esp8266 / Sonoff freeze after some hour or some days, randomly”. Obviously they all crashed after a few hours/minutes/days of testing at home (remember this “at home”). Having discovered the OTA update (wonderful!), I placed ESP8266 and the modified SONOFF in the final positions, then in the garage, modifying only the SSID being an access point different from the home one. I left the other ESP under the home SSID because I placed it in the boiler.

Thinking of a power problem and as suggested by someone the two ESPs I fed them with a power supply from a mobile phone instead of a “cheap” power supply. And nothing, after a few minutes or a few hours, the ESP of the boiler stops. But the other two in the garage work!! Strange…

Then I put (for testing) in all three devices a code that counts the minutes and I visualize them on a widget, and I discovered that when in the app the devices go offline (continuosly), the next reconnection the minute counter doesn’t reset but goes on. So I realized it was a wifi connection problem or a problem with cloud servers. Thanks to a code from @Gunner I entered a code found in “# 9 Basic” Keeps running without WiFi or Blynk Server connection, but soon as they are available." This code has also a counter in “seconds” and I left it active. To test these “seconds” I put them on widgets for all three devices, and from this I discovered that while for the two in the garage the seconds are advancing well, normally, that of the boiler counted for 5 seconds, then disconnected, then reconnected (sometimes 10 times in half a minute), then jumped seconds, stopped … etc etc … very very unstable!

So I resurrected an old computer and installed the arduino IDE to read the Serial. I realized that there were many problems with wifi, with Serial print: “MDNSResponder possible race-condition”, “login timeout”, some connection with “ping 2500ms”, continuous disconnections, and lots of strange messages, so I connected the esp to my cell’s hotspot, and everything perfect. Now I have added a temporary access point at home (waiting for my new fritz!box to arrive) and it has been working regularly since yesterday without disconnection.

The strange thing and that did not make me think of a router/wifi problem is that at home I have about twenty original sonoffs with ewelink, 5 alexa echo, 2 tablets, many phones, 4 wifi cameras, and I’ve never had any problems with these devices. But the Serial does not lie, as soon as I changed access points, everything went smoothly. Maybe Esp wifi is more sensitive? I do not know!

Sorry if I wrote a papyrus, but if the solution to my problem (which has been beating my head for months and months) is a router with a non-stable wifi, maybe it can be useful to someone who has my same symptoms and look for errors in the code, in the connections and in a thousand other things not thinking maybe about the most logical thing, the WiFi connection! :smile:

Ps: the sonoff device that I was testing “unfortunately” I was testing right in the garage in my small laboratory, then connected to the working wifi.

Thanks again to everyone (specially to @PeteKnight and to @wickedbeernut for help me here and to @Gunner for his useful examples page), look forward to the next developments :slight_smile:

3 Likes

This is a great example of how to do logical testing to narrow-down the issue and identify it’s cause.
Many other forum members could benefit from taking this approach when it comes to diagnosing the cause of this type of problem.

Pete.

1 Like

I’m a beginner but I don’t give up easily! :slight_smile: also because I discovered that there is always a reason :smile:

1 Like

Ok, I can say with certainty that I have solved all my problems (found with a series of tests as I wrote above) by “simply” changing the router. I’m too happy. In the meantime I made some interesting changes to my project, Blynk is really a beautiful invention :slight_smile:

Feel free to share the “changes” and maybe some pics under projects made. I love that section, gives new ideas of how to put things together. I wish everyone would post pics, code and diagrams of all their projects…

Hello,You are the best.I have also facing this problem for a long time. I got the solution from your topic.That is I need to change Wi-Fi router. Thanks.

1 Like