ESP8266WiFi board restart

Hi all,

i’am trying to write my first program for ESP8266 in pairing with this fantastic app Blynk.
I have one problem, when I push the two button, sometimes (very often) the board restart itself, and I don’t know why.

Here is the code:

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "*************************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "**************";
char pass[] = "***************";

#define DHTPIN 2          // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

int Setpoint; 
int Loffset;
int SetLoffset;
int Hoffset; 
int SetHoffset;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
WidgetLCD lcd(V1);
BLYNK_WRITE(V0){   // add a slider to your project on V0 range 0 to 30 (minutes)
  Setpoint = param.asInt();  // set variable as Slider value
}
BLYNK_WRITE(V2){   // add a slider to your project on V0 range 0 to 30 (minutes)
  Loffset = param.asInt();  // set variable as Slider value
}
BLYNK_WRITE(V3){   // add a slider to your project on V0 range 0 to 30 (minutes)
  Hoffset = param.asInt();  // set variable as Slider value
}

void aggdisplay() {
  lcd.clear();
  lcd.print(0,0, "Il setpoint è a");
  lcd.print(0,1, Setpoint);
  delay(1000);
  lcd.clear();
  lcd.print(0,0, "Si accende a:");
  SetLoffset = Setpoint + Loffset;
  lcd.print(0,1, SetLoffset);
  delay(1000);
  lcd.clear();
  lcd.print(0,0, "Si spegne a:");
  SetHoffset = Setpoint + Hoffset;
  lcd.print(0,1, SetHoffset);
  delay(1000);  
}

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  aggdisplay();
}

void setup()
{  
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);  
  digitalWrite(4, HIGH); 
  digitalWrite(5, HIGH); 
  
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();

  Blynk.syncAll();

  // Setup a function to be called every 10 second
  timer.setInterval(10000L, sendSensor);
  
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer3
}

delay(1000);
  lcd.clear();
  lcd.print(0,0, "Si accende a:");
  SetLoffset = Setpoint + Loffset;
  lcd.print(0,1, SetLoffset);
  delay(1000);
  lcd.clear();
  lcd.print(0,0, "Si spegne a:");
  SetHoffset = Setpoint + Hoffset;
  lcd.print(0,1, SetHoffset);
  delay(1000);

Remove all your delays from your code. Delay’s cause heartbeat timeouts, which lead to connection instability.

General rule, don’t use delay unless you are an advanced Blynk user.

Replace with SimpleTimer setTimeout functions with a callback function.

Thank you very much Jamin for your quick reply.
Please can you give me an example code for replacing the delay with a SimleTimer, or where I can find some examples?

Here is an example I wrote for someone else’s project.

Remove the detail and study how the “callback” function works… its pretty simple.

void actionRelays(){
  relay1_ON(); // turn on relay1
  timer.setTimeout(2000, relay2_ON); // delay 2sec then turn on relay2
  timer.setTimeout(15000, relays_OFF); // delay 15sec from first action and turn off both relays
}

void relay1_ON(){
  digitalWrite(relay1,HIGH); // turn on relay1
  Blynk.virtualWrite(V10, 255); // turn on an LED widget (optional)
}

void relay2_ON(){
  digitalWrite(relay2,HIGH); // turn on relay2
  Blynk.virtualWrite(V20, 255); // turn on an LED widget (optional)
}

void relays_OFF(){
  digitalWrite(relay1,LOW); // turn off relay1
  digitalWrite(relay2,LOW); // turn off relay2
  Blynk.virtualWrite(V10, 0); // turn off an LED widget (optional)
  Blynk.virtualWrite(V20, 0); // turn off an LED widget (optional)
}

Thank you so much, I have trasformed the code like this:

void aggDisp1(){
  lcd.clear();
  lcd.print(0,0, "Il setpoint è a");
  lcd.print(0,1, Setpoint);
  timer.setTimeout(2000, aggDisp2); 
  timer.setTimeout(4000, aggDisp3); 
}

void aggDisp2(){
  lcd.clear();
  lcd.print(0,0, "Si accende a:");
  SetLoffset = Setpoint + Loffset;
  lcd.print(0,1, SetLoffset);
}

void aggDisp3(){
  lcd.clear();
  lcd.print(0,0, "Si spegne a:");
  SetHoffset = Setpoint + Hoffset;
  lcd.print(0,1, SetHoffset);
}

Now I can’t test it on the hardware but tomorrow I test it.

Good stuff dude!

One change I’d point out…

void aggDisp1(){
  lcd.clear();
  lcd.print(0,0, "Il setpoint è a");
  lcd.print(0,1, Setpoint);
  timer.setTimeout(2000, aggDisp2); 
}

void aggDisp2(){
  lcd.clear();
  lcd.print(0,0, "Si accende a:");
  SetLoffset = Setpoint + Loffset;
  lcd.print(0,1, SetLoffset);
  timer.setTimeout(2000, aggDisp3); 
}

Move the second timer to be after the completed action otherwise the second screen will be up for less than 2000ms…

1 Like

Thanks a lott Jamin :grin:

I hope I can always learn more, and ask less :joy:

Another curiosity, It’s possible the board restart also when I push very fast the button, also without delay in the code?

Sorry I don’t understand the question.

I mean, If I put thi code in the ESP8266 board:

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "*************************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "**************";
char pass[] = "***************";

#define DHTPIN 2          // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup()
{  
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);  
  digitalWrite(4, HIGH); 
  digitalWrite(5, HIGH); 
  
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();

  // Setup a function to be called every 10 second
  timer.setInterval(10000L, sendSensor);

If I press fastly the two button (gp4 and gp5) in the application, sometimes the board reset itself.
I think this happened if I push the button and the board send the value of the temperature and humidity (every 10sec).
It’s this possible?

@Jamin do you understand now the my question?