Hi everyone.
I’ve got a project: analog data read by arduino on A0,A1,A2 and generate HIGH signal if values lower than 200(I can’t make work arduino and esp 12f as shield together), this signals read by esp 12f witty cloud and “translate” to led_widget,send notifications and email.Here’s the code:
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXXXX";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YYYYY";
char pass[] = "ZZZZZZ";
WidgetLED led1(V0);//стиралка gpio2
WidgetLED led2(V1);//ванная gpio0
WidgetLED led3(V2);//раковина gpio4
WidgetLED led4(V3);//связь с ардуино
bool stiralka_flag=0;
bool vannaya_flag=0;
bool rakovina_flag=0;
bool arduino_flag=0;
bool isFirstConnect = true;
SimpleTimer timer;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(15,INPUT);//стиралка
pinMode(5,INPUT);//ванная
pinMode(4,INPUT);//раковина
pinMode(16,INPUT);//вход от ардуино
pinMode(14,OUTPUT);//выход на ардуино
timer.setInterval(1100L, Led_stiralka);
timer.setInterval(1200L, Led_vannayaa);
timer.setInterval(1300L, Led_rakovina);
timer.setInterval(1000L, Led_arduino);
}
void Led_stiralka()
{
if (digitalRead(15)==HIGH&&stiralka_flag==0)
{
led1.on();
Blynk.email("стиралка","Датчик стиралки сработал!");
Blynk.notify("тревога!стиралка!");
stiralka_flag=1;
}
else if (digitalRead(15)==HIGH&&stiralka_flag==1)
{
led1.on();
}
else
{
led1.off();
}
if (digitalRead(15)==LOW) stiralka_flag=0;
}
void Led_vannayaa()
{
if (digitalRead(5)==HIGH&&vannaya_flag==0)
{
led2.on();
Blynk.email("ванная","Датчик под ванной сработал!");
Blynk.notify("тревога!ванная!");
vannaya_flag=1;
}
else if (digitalRead(5)==HIGH&&vannaya_flag==1)
{
led2.on();
}
else
{
led2.off();
}
if (digitalRead(5)==LOW) vannaya_flag=0;
}
void Led_rakovina()
{
if (digitalRead(4)==HIGH&&rakovina_flag==0)
{
led3.on();
Blynk.email("раковина","Датчик под раковиной сработал!");
Blynk.notify("тревога!раковина!");
rakovina_flag=1;
}
else if (digitalRead(4)==HIGH&&rakovina_flag==1)
{
led3.on();
}
else
{
led3.off();
}
if (digitalRead(4)==LOW) rakovina_flag=0;
}
void Led_arduino()
{
if (digitalRead(16)==LOW&&arduino_flag==0)
{
led4.off();
digitalWrite(14,LOW);
Blynk.email("ардуино","Связь с ардуино потеряна");
Blynk.notify("связь с ардуино потеряна");
arduino_flag=1;
}
else if (digitalRead(16)==LOW&&arduino_flag==1)
{
led4.off();
digitalWrite(14,LOW);
}
else if (digitalRead(16)==HIGH&&arduino_flag==1)
{
led4.on();
digitalWrite(14,HIGH);
Blynk.notify("связь с ардуино восстановлена");
arduino_flag=0;
}
else
{
led4.on();
digitalWrite(14,HIGH);
}
}
void loop()
{
Blynk.run();
timer.run();
}
So what’s the problem? When ,for example, gpio2 (virtual pin 0) is LOW, led widget is off and gpio0 is LOW it also off (led widget). When gpio2 changes to HIGH led widget turns on and notifications shows. But when all 3 virtual pins (gpio2,gpio0 and gpio4) changes to HIGH at the same moment I’ve got only 1 notification and esp shortly disconnected from server. Or if gpio2 changes to HIGH and next pin changes to HIGH too,but time spent less than 5-7 maybe 10 seconds I’ve got no notifications and no email. Only led widget in Blynk works every time fine and very fast.
What’s the problem with my code?
Yeah,maybe I said a lil bit incorrect But,how I can “repair” my code to send all emails and notifications? Use a delay between every notification and sending an email?
Probably your esp just can’t handle the network requests and blynk timeouts. In any case, it is good practice to limit your network requests to required only. Firstly, right after reading digital pins, I would check if its state changed. I would also move notifying logic into separate function and combine message in case few notifications need to be send (to increase chances we don’t hit blynk notify limit). Hope you’ll get the idea:
// ......
bool stiralka_notify;
bool vannaya_notify;
bool rakovina_notify;
bool arduino_notify;
void setup()
{
// ......
timer.setInterval(2000L, Notify_all);
}
// .....
void Notify_all()
{
if (!stiralka_notify && !vannaya_notify && !rakovina_notify) {
return;
}
// here you could check if 15 seconds passed from last time (return if not)
if (stiralka_notify && vannaya_notify && rakovina_notify) {
Blynk.email("Все датчики","Все датчики сработали");
Blynk.notify("Все датчики");
}
else if (stiralka_notify && vannaya_notify) {
Blynk.email("Стирала и ванная","Датчики стиралки и ванной сработали");
Blynk.notify("Стирала и ванная");
}
else if (stiralka_notify && rakovina_notify) {
}
else if (vannaya_notify && rakovina_notify) {
}
else if (stiralka_notify) {
Blynk.email("стиралка","Датчик стиралки сработал!");
Blynk.notify("тревога!стиралка!");
}
else if (vannaya_notify) {
Blynk.email("ванная","Датчик под ванной сработал!");
Blynk.notify("тревога!ванная!");
}
else if (rakovina_notify) {
Blynk.email("раковина","Датчик под раковиной сработал!");
Blynk.notify("тревога!раковина!");
}
else {
Blynk.email("Ошибка","Ошибка в моем коде");
Blynk.notify("Ошибка в моем коде");
}
stiralka_notify = false;
vannaya_notify = false;
rakovina_notify = false;
}
// .......
void Led_stiralka()
{
bool stiralka = digitalRead(15)==HIGH;
if (stiralka_flag == stiralka)
{
return; // nothing changed
}
stiralka_flag = stiralka;
if (stiralka_flag)
{
led1.on();
stiralka_notify = true;
}
else
{
led1.off();
}
}
@Eugene, can you explain this code to me: timer.setInterval(2000L, Notify_all);
What means 2000L?That function Notify_all will be performed every 2sec?
So,to avoid reaching the 15sec limitation I may edit the code like this: timer.setInterval(15000L, Notify_all);
So,the code with notifications will perform every 15 sec.Or it isn’t correct?
Your code looks fantastic now.
When I saw the code last night I was surprised to see 2000L rather than 15000L but without studying the code I can’t tell if there is a check for the 15s minimum period between notify / email messages.
As I know only email and notify widgets have a 15s limit. Other widgets like “Led” don’t,so in code I wanted only email and notify widgets perform every 15sec.
No, you didn’t get my idea.
I would call Notify_all as soon as possible, e.g. once in 2 or 3 sec so that pin reading from Led_* functions happened.
Inside the Notify_all function I’d check if 15 seconds have passed by comparing the stored time variable with the current time. Smth like:
// .....
unsigned long notifiedTime;
// .....
// here you could check if 15 seconds passed from last time (return if not)
unsigned long currentTime = millis();
if (notifiedTime > 0 && (currentTime - notifiedTime) < 15000) {
return; // 15 haven't passed yet, we'll just wait
}
notifiedTime = currentTime;