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

Receiving Blynk Notifications Every 5 secs

arduino
esp8266
#1

i am working on a moisture sensor using NodeMCU and Blynk. A Blynk notification is sent so that when the moisture is above 50, the soil needs water. i have checked other topics and it seems it’s not working for me. i am receiving Blynk notification every 5 secs, so how can i fix the delay between each notification, here is my code below:

#define BLYNK_PRINT Serial
#include <SPI.h>;
#include <SimpleTimer.h>;
#include <ESP8266WiFi.h>;
#include <BlynkSimpleEsp8266.h>;

char auth[] = "**********************************************";
char ssid[] = "**********************";
char pass[] = "*****************************";
SimpleTimer timer;
const int sensorPin = A0;   
int sensorState = 0;
int lastState = 0;
int sensorStatePercent = 0;

void resetNotified(){
 lastState = 0;
}
void sendSensor(){
  sensorState = analogRead(sensorPin);
  sensorState = map(sensorState, 0, 1023, 0, 100);
Serial.println(sensorState);
Blynk.virtualWrite(A0, sensorState);

if (sensorState >= 50 && lastState == 0) {         //send notification
  lastState = 1;
  Blynk.notify("Water your plants");
  timer.setTimeout(60000L, resetNotified);

    } 
  else {
    Serial.println("does not need water");
    lastState = 0;
    delay(1000);
  }

}


void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(sensorPin, INPUT);
  timer.setInterval(1000L, sendSensor);
}

void loop()
{ 
  Blynk.run();
  timer.run();

}

0 Likes

#2

i tried timer.setinterval (600000L, sendSensor); and removed the timer.setTimeout, it works but the 1st notification is sent after 10 mins then the process works perfectly. But i want to receive the notification immediately at the 1st 1 sec, then to wait for example 10 mins or more until i get the 2nd notification, how can i do so, thanks in advance!

0 Likes

#3

Curious what’s the purpose of the delay in the last void loop?

0 Likes

#4

Probably better ways of doing it but what about a read and send in the setup?

0 Likes

#5

can you please explain more to me? , thanks!

0 Likes

#6

Experiment with the following

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(sensorPin, INPUT);
Delay(10);
sendSensor();
  timer.setInterval(1000L, sendSensor);
}
0 Likes

#8

is this correct if i want to receive only 1 notification?, i only need 1 notification when it needs to be watered and 1 when it’s watered.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "********************************************";  
char ssid[] = "************************";
char pass[] = "******************************";
const int sensorPin = A0; 
int sensorState = 0;
int lastState = 0;


void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(sensorPin, INPUT);
}

void loop()
{ 
  Blynk.run();

  sensorState = analogRead(sensorPin);
  sensorState = map(sensorState, 0, 1023, 0, 100);
  Blynk.virtualWrite(A0, sensorState);

if (sensorState >= 50 && lastState == 0) {
  Serial.println("needs water, send notification");
  Blynk.notify("Water your plants");
  lastState = 1;
    
  } 
  else if (sensorState <= 50 && lastState == 1) {
  Blynk.notify("Plants Watered");
  lastState = 0;
  delay(1000);
  }  
}
0 Likes

#9

First of all, you should read this:

You’ll see that the code that reads your sensor and sends the notifications should be in a function of its own, called at an appropriate frequency by a timer. Personally, I wouldn’t have thought that this needed to be any more frequently than once every hour.

Secondly, your ‘if’ statements overlap on the 50 reading, so you would get two notifications.
Personally, I’d probably leave a small gap between the sensor value tests in the ‘if’ statements, so that I don’t get multiple readings if the results fluctuate around the 50 mark. Maybe a <49 plus a >51 set of tests.

Pete.

1 Like

#10

Ok I miss understood your intention. Try adding another condition to toggle your “sent notification” flag.

#define BLYNK_PRINT Serial
#include <SPI.h>;
#include <SimpleTimer.h>;
#include <ESP8266WiFi.h>;
#include <BlynkSimpleEsp8266.h>;

char auth[] = "**********************************************";
char ssid[] = "**********************";
char pass[] = "*****************************";
SimpleTimer timer;
const int sensorPin = A0;   
int sensorState = 0;
int lastState = 0;
int lastStateWet = 0;

void sendSensor(){
 sensorState = analogRead(sensorPin);
 sensorState = map(sensorState, 0, 1023, 0, 100);
Serial.println(sensorState);
Blynk.virtualWrite(A0, sensorState);

if (sensorState >= 50 && lastState == 0) { 
 lastState = 1;                         //toggles message sent state
 lastStateWet = 0;                  //toggles message sent stateWET
 Blynk.notify("Water your plants”);
   } 
else if (sensorState <= 48 && lastStateWet == 0)
{
   lastState = 0;
   lastStateWet = 1;
   Blynk.notify(“Plants were watered”);
 }

}


void setup()
{
 Serial.begin(115200);
 Blynk.begin(auth, ssid, pass);
 pinMode(sensorPin, INPUT);
 timer.setInterval(1000L, sendSensor);
}

void loop()
{ 
 Blynk.run();
 timer.run();

}

Does this work like you expect? This is supposing your senor gives a bigger number when dry.

1 Like

#11

Thanks for answering Pete, this link was really useful; if i made a function that reads the sensor and sends the notification, then i added timer.setInterval(1000L, sendSensor); , but this will call the function every 1 sec, so i will get the notification every 1 sec right? but as you said, 1 hour would be good, but if i called the function every 1 hour, the data in this hour will not be displayed or it will stay as it is when 1st read before going to a delay of 1 hour, right?

0 Likes

#12

Yes, if you don’t use a flag to indicate that a notification has already been sent.
Set the flag to false when your board boots, then to true when an alert has been sent.
Clear the flag (set it to false) when the temperature dops/rises into the ‘acceptable’ temperature range.

Pete.

0 Likes

#13

i have a gauge on Blynk that displays the soil moisture data, so for example if the data was 55, then it went for a 1 hour delay, the data will stay 55, right?, because the board is in a delay now, but i want to keep everything working meanwhile sending notification every hour, can this happen?

0 Likes

#14

It’s possible, but I don’t see th point in sending a notification every hour, 24 hours per day.
In my opinion, notifications should be use to alert the user to exceptional situations. If you want to know what the mosistuer level is every hour then look at the gauge in the app!

Pete.

1 Like

#15

exactly, you’re right, sending a notification every 24 hours is better, so i have to set the time interval to 24 hours or what?, what should be changed in code to get this 24 hour notification, any tip please?, thanks in advance

0 Likes

#16

I still don’t think a notification every 24 hours is the best approach. An email summary might be a better approach.

If you want to do something every 24 hours then the RTC widget is the tool that I’d use.

Pete.

0 Likes

#17

The point is that i don’t want to get spammed by notifications, i just want a delay between each notification, for example sending notification to water plants each hour just for a reminder if he forgot to water his plants.
so coding wise, timer.setinterval will solve this?

0 Likes

#18

Yes, you could do that, maybe with a button to cancel the alert if you remember to water the plants.

Maybe an automatic plant watered would be a better project?

Pete.

0 Likes

#19

i want to work 1st with notifications, the next thing is that i want to automatically do it, but for now, i just don’t want to get spammed with notifications, just need like 1 hour or anything between each notification, how can i do it?

0 Likes

#20

In that case I’d just have a standard timer that runs every 60 minutes. Presumably the function that it calls will take some moisture readings and only send an alert if the readings are out of the acceptable range, otherwise you could simply use the timer function on the code without any need for Blynk.

Pete.

0 Likes

#21

as suggested before from @daveblynk, the code below is fine?:

#define BLYNK_PRINT Serial
#include <SPI.h>;
#include <SimpleTimer.h>;
#include <ESP8266WiFi.h>;
#include <BlynkSimpleEsp8266.h>;

char auth[] = "**********************************************";
char ssid[] = "**********************";
char pass[] = "*****************************";
SimpleTimer timer;
const int sensorPin = A0;   
int sensorState = 0;
int lastState = 0;
int lastStateWet = 0;

void sendSensor(){
 sensorState = analogRead(sensorPin);
 sensorState = map(sensorState, 0, 1023, 0, 100);
Serial.println(sensorState);
Blynk.virtualWrite(A0, sensorState);

if (sensorState >= 50 && lastState == 0) { 
 lastState = 1;                         //toggles message sent state
 lastStateWet = 0;                  //toggles message sent stateWET
 Blynk.notify("Water your plants”);
   } 
else if (sensorState <= 48 && lastStateWet == 0)
{
   lastState = 0;
   lastStateWet = 1;
   Blynk.notify(“Plants were watered”);
 }

}


void setup()
{
 Serial.begin(115200);
 Blynk.begin(auth, ssid, pass);
 pinMode(sensorPin, INPUT);
 timer.setInterval(1000L, sendSensor);
}

void loop()
{ 
 Blynk.run();
 timer.run();

}
0 Likes