BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

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


#1

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(); 
}

#2

You should read this…

Pete.


#4

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


#5

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();
}


#6

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.


#7

Ok, i will try these step, thanks


#8

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);
}

#9

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.