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

Notify and email widgets-too slow


#5

Thanks,I will complete the code and test it) But already it looks good and more professional than my :sunglasses:


#6

@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?


#7

Yes, that is correct.


#8

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?


#9

@Eugene, I edited the code. If you’ve got some free time,can you look at it?:mortar_board:

> #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[] = "XXX";

> // Your WiFi credentials.
> // Set password to "" for open networks.
> char ssid[] = "YYY";
> char pass[] = "ZZZ";
> WidgetLED led1(V0);//стиралка gpio2
> WidgetLED led2(V1);//ванная gpio0
> WidgetLED led3(V2);//раковина gpio4
> WidgetLED led4(V3);//связь с ардуино
> bool stiralka_notify;
> bool vannaya_notify;
> bool rakovina_notify;
> bool arduino_notify;
> bool stiralka_flag;
> bool vannaya_flag;
> bool rakovina_flag;
> bool arduino_flag;

> 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);//выход на ардуино
>   stiralka_flag=0;
>   vannaya_flag=0;
>   rakovina_flag=0;
>   arduino_flag=0;
>   timer.setInterval(2000L, Notify_all);
> }

> // .....
> void Notify_all()
> {
>   if (!stiralka_notify && !vannaya_notify && !rakovina_notify && arduino_notify) 
>   {
> 	led1.off;
> 	led2.off;
> 	led3.off;
> 	led4.on;
> 	digitalWrite(14,LOW);
>     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) {
> 	   Blynk.email("Стиралка и раковина","Датчики стиралки и раковины сработали");
>        Blynk.notify("Стиралка и раковина");
>   }
>   else if (vannaya_notify && rakovina_notify) {
> 	   Blynk.email("Ванная и раковина","Датчики ванной и раковины сработали");
>        Blynk.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;
>   
>   if (arduino_notify)
>     {
>         Blynk.email("ардуино","Связь с ардуино потеряна");
>         Blynk.notify("связь с ардуино потеряна");
>     }
>     else
>     {
>       Blynk.notify("связь с ардуино восстановлена");
>     }

> }

> void Led_stiralka()
> {
>   if (digitalRead(15)==HIGH && stiralka_flag==0) 
>     {
>        stiralka_notify=1;
> 	   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) 
>     {
>        vannaya_notify=1;
> 	   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) 
>     {
>        rakovina_notify=1;
> 	   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) 
>     {
> 		arduino_notify=1;
>         led4.off();
>         digitalWrite(14,LOW);
>         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);
>       arduino_notify=0;
>       arduino_flag=0;
>     }
>     else
>     {
>        led4.on();
>        digitalWrite(14,HIGH);
>     }
> }

#10

@legionercheg please edit your last post to make the sketch legible. See [README] Welcome to Blynk Community! for details of how to format code.


#12

@Costas, sorry. Edited


#13

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.


#14

There are a lot of functions in code,but 90% are the same,only pins to digitalRead changes;)
But there are no other check for the 15s limit except

timer.setInterval(2000L, Notify_all);

So, it will be a good idea to change it to 15000L?


#15

I would use 15000L unless there is anything that really needs to be done more frequently than 15s intervals.


#16

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.


#17

You can have separate timers if you want.


#18

Yeah, I realised that. At my first message at this topic at code there are 4 timers (1 timer for each pin/notificator).


#19

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;


#20

@Eugene, but what would be if I did this,instead of your variant?

timer.setInterval(15000L, Notify_all);


#21

Yep, Notify_all would be called once in 15 sec. If such delay is ok for you, you can keep it and don’t check for it inside.


#22

I’m only wrote this code in notepad++ and didn’t test it in my project,so now I can’t clearly said that it’s ok or not for me :joy: But if it is a good variant and there are no any exceptions,I think I will use it if the tests will be ok :blush:


#23

Loaded my sketch to IDE but when I try to compile it there is an error:
exit status 1
Ошибка компиляции для платы NodeMCU 1.0 (ESP-12E Module).


#24

@legionercheg post the latest version of your sketch and I’ll run it through the compiler for a NODEMCU clone.


#25
#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[] = "xxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "yyy";
char pass[] = "zzz";
WidgetLED led1(V0);//стиралка gpio2
WidgetLED led2(V1);//ванная gpio0
WidgetLED led3(V2);//раковина gpio4
WidgetLED led4(V3);//связь с ардуино
SimpleTimer timer;
bool stiralka_notify;
bool vannaya_notify;
bool rakovina_notify;
bool arduino_notify;
bool stiralka_flag;
bool vannaya_flag;
bool rakovina_flag;
bool arduino_flag;

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);//выход на ардуино
  stiralka_flag=0;
  vannaya_flag=0;
  rakovina_flag=0;
  arduino_flag=0;
  timer.setInterval(15000L, Notify_all);//Notify_all будет срабатывать раз в 15 сек
}

void Notify_all()
{
  if (stiralka_notify==1) 
  {
       Blynk.email("стиралка","Датчик стиралки сработал!");
       Blynk.notify("тревога!стиралка!");
  }
  else if (vannaya_notify==1) 
  {
       Blynk.email("ванная","Датчик под ванной сработал!");
       Blynk.notify("тревога!ванная!");
  }
  else if (rakovina_notify==1) 
  {
       Blynk.email("раковина","Датчик под раковиной сработал!");
       Blynk.notify("тревога!раковина!");
  } 
  if (arduino_notify==1)
    {
        Blynk.email("ардуино","Связь с ардуино потеряна");
        Blynk.notify("связь с ардуино потеряна");
    }
  else
    {
      Blynk.notify("связь с ардуино восстановлена");
    }

}

void Led_stiralka()
{
  if (digitalRead(15)==HIGH && stiralka_flag==0) 
    {
       stiralka_notify=1;
	   stiralka_flag=1;
	   led1.on();
    } 
    else if (digitalRead(15)==HIGH && stiralka_flag==1)
    {
	   led1.on();    
	}
    else
    {
		led1.off();
    }
    if (digitalRead(15)==LOW) 
	{
		stiralka_flag=0;
		led1.off();
	}
}
void Led_vannayaa()
{
  if (digitalRead(5)==HIGH && vannaya_flag==0) 
    {
       vannaya_notify=1;
	   vannaya_flag=1;
	   led2.on();
    } 
    else if (digitalRead(5)==HIGH && vannaya_flag==1)
    {
	   led2.on();    
	}
    else
    {
		led2.off();
    }
    if (digitalRead(5)==LOW)
	{
		vannaya_flag=0;
		led2.off();
	}
}
void Led_rakovina()
{
  if (digitalRead(4)==HIGH && rakovina_flag==0) 
    {
       rakovina_notify=1;
	   rakovina_flag=1;
	   led3.on();
    } 
    else if (digitalRead(4)==HIGH && rakovina_flag==1)
    {
	   led3.on();    
	}
    else
    {
		led3.off();
    }
    if (digitalRead(4)==LOW)
	{
		rakovina_flag=0;
		led3.off();
	}
}
void Led_arduino()
{
  if (digitalRead(16)==LOW && arduino_flag==0) 
    {
		arduino_notify=1;
        led4.off();
        digitalWrite(14,LOW);
        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);
      arduino_notify=0;
      arduino_flag=0;
    }
    else
    {
       led4.on();
       digitalWrite(14,HIGH);
    }
}