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

Receiving Blynk Notifications Every 5 secs

esp8266
arduino
#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.

0 Likes

#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.

0 Likes