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
}
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)
}
#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?