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