Notify and email widgets-too slow

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?

I haven’t looked at your code, however emails and notifications have a limit of 1 per 15 seconds.

http://docs.blynk.cc/#widgets-notifications

So they are not too slow, I think your requests are too fast :wink:

Yeah,maybe I said a lil bit incorrect :grin: 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();
  }
}
2 Likes

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

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

Yes, that is correct.

1 Like

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?

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

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

@Costas, sorry. Edited

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.

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?

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

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.

You can have separate timers if you want.

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

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;

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

timer.setInterval(15000L, Notify_all);

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.