Blynk Event limitation

hi @PeteKnight i think there is a problem in my else if command because after sending the notification one time, i tried to jumpwire an led on the output to ground to check if the pir sensor is high or low, the flag command is not being reset because i saw that the led stopped lighting up but the comment Motion is now absent and New motion detection events will trigger a new alert notification is not sent in the serial.
can you help me? thankyou

The existing serial print statements should show you whether the if statements are evaluating as true - when the do the serial print statements inside the if statements will print to the serial monitor.

If youā€™re not sure why the if statements arenā€™t giving the results you expect then add more serial print commands to print the values of various variables that you are using in your if comparisons. Put tehse in your MotionSensorTimer just after this line of code:

Pete.

Hi @PeteKnight thankyou for the fast response, iā€™ve just reflashed the same code and it seems to be working but the problem now is the ā€œMotion is now absentā€ and ā€œNew motion detection events will trigger a new alert notificationā€ is flooding the serial monitor where as the notification has been sent is only triggered once

iā€™ve just checked i added ā€œ;ā€ after the

else if(sensor = MotionSensor && alert_sent == true)

that might be the reason why it is alternating from no detected to motion detected.
i got flagged again after adding " ; " which means that the flag does not work after adding the ;
i might try again tomorrow

Hi @PeteKnight i think the problem is that the alert sent is not being resetted, can you help me?

Not without your fill sketch and the info that appears in your serial monitor, and som additional information explaining what you were doing at the time so that the serial data makes some sense.

Pete.

hi @PeteKnight

this is my whole sketch

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "REDACTED"
#define BLYNK_DEVICE_NAME "REDACTED"
#define BLYNK_AUTH_TOKEN "REDACTED"

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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "REDACTED";


// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "REDACTED";  //Enter your WIFI Name
char pass[] = "REDACTED";  //Enter your WIFI Password

int Status = 12;  // Digital pin D6
int sensor = 13;  // Digital pin D7

float MotionSensor = LOW; // We will send an alert if the sensor outputs HIGH
bool alert_sent = false; // Flag to track if an alert has been sent for a motion detected event
 
BlynkTimer timer; // This function creates the timer object. It's part of Blynk library

void MotionSensorTimer() 
{
  float sensor = digitalRead(13);

  Blynk.virtualWrite(V0, sensor); 
  if(sensor != MotionSensor && alert_sent == false)
  {
    Blynk.logEvent("motion_detected");  // trigger the notification
    alert_sent = true;  // set the flag to indicate that an alert has been sent for this motion detected situation
    Serial.println("Blynk Alert Notification sent!");
    Serial.println();
  }
  else if(sensor = MotionSensor && alert_sent == true)
  {
    alert_sent = false; // reset the flag so that a future motion detection situation will trigger an alert
    Serial.println("Motion is now absent");
    Serial.println("New motion detection events will trigger a new alert notification");
    Serial.println();
  }
}



void setup() 
{
  Serial.begin(9600);
  //Connecting to Blynk Cloud
  Blynk.begin(auth, ssid, pass); 
  
  // Setting interval to send data to Blynk Cloud to 1000ms. 
  // It means that data will be sent every second
  timer.setInterval(10000L, MotionSensorTimer); // take a reading from the PIR sensor every 10 seconds

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

after turning on the ESP8266 or nodemcu, i am greeted with
image

after that blynk alert notification sent, it wont show that the motion is absent and that new motion will trigger the event. and after receiving the first notification, i wont receive anymore notification which justifies that the alert_sent is not being resetted which blocks any notifications anymore.

also, iā€™ve tried to walk out in the room in order to check if the sensor will reset or reads a low value to make motion absent but it wont.

please disregard the dht library since i will add it later on after i figure out this pir sensor :))

Okay, I expected to see a sketch where youā€™d taken my advice and added additional serial print statements, and the values of your variables appearing in the serial monitor as a result.
Without that info Iā€™m as much in the dark as you are.

Also, when you share serial output data please copy the text from your serial monitor and paste it, using triple backticks, into the forum rather than posting screenshots.

Pete.

Hi @PeteKnight iā€™ve now just understand what you mean by adding more serials after the float. Im trying it now and will update what happens. Thankyou verymuch for you patience in helping me :))

@PeteKnight hereā€™s what happened:
hereā€™s the code that iā€™ve used

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "REDACTED"
#define BLYNK_DEVICE_NAME "REDACTED"
#define BLYNK_AUTH_TOKEN "REDACTED"

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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "REDACTED";


// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "REDACTED";  //Enter your WIFI Name
char pass[] = "REDACTED";  //Enter your WIFI Password

int Status = 12;  // Digital pin D6
int sensor = 13;  // Digital pin D7

float MotionSensor = 0; // We will send an alert if the sensor outputs HIGH
bool alert_sent = false; // Flag to track if an alert has been sent for a motion detected event
 
BlynkTimer timer; // This function creates the timer object. It's part of Blynk library

void MotionSensorTimer() 
{
  float sensor = digitalRead(13);
  Serial.print("sensor = ");
  Serial.println(digitalRead(13));
  Blynk.virtualWrite(V0, sensor); 
  if(sensor = 1 && alert_sent == false)
  {
    Blynk.logEvent("motion_detected");  // trigger the notification
    alert_sent = true;  // set the flag to indicate that an alert has been sent for this motion detected situation
    Serial.println("Blynk Alert Notification sent!");
    Serial.println();
  }
  else if(sensor = 0 && alert_sent == true)
  {
    alert_sent = false; // reset the flag so that a future motion detection situation will trigger an alert
    Serial.println("Motion is now absent");
    Serial.println("New motion detection events will trigger a new alert notification");
    Serial.println();
  }
}



void setup() 
{
  Serial.begin(9600);
  //Connecting to Blynk Cloud
  Blynk.begin(auth, ssid, pass); 
  
  // Setting interval to send data to Blynk Cloud to 1000ms. 
  // It means that data will be sent every second
  timer.setInterval(10000L, MotionSensorTimer); // take a reading from the PIR sensor every 10 seconds

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

and heres the output in serial monitor

[6393] IP: 192.168.1.130
[6393] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.1.0 on ESP8266

 #StandWithUkraine    https://bit.ly/swua


[6519] Connecting to blynk.cloud:80
[6653] Ready (ping: 40ms).
sensor = 1
Blynk Alert Notification sent!
sensor = 1
sensor = 1
sensor = 0
sensor = 0
sensor = 0
sensor = 0
sensor = 0
sensor = 0
sensor = 1
sensor = 1
sensor = 1
sensor = 1
sensor = 1
sensor = 1
sensor = 1
sensor = 1

here in my code, i put when sensor is = 1, it will send a notification and set the alert_sent = true and in the else if, i put that if the sensor is 0, it will set the alert_sent to false. and send a message in the serial that motion is now absent and new motion detection events will trigger a new alert notification. but in the serial monitor, the first reading of sensor = 1 sent the blynk alert notification sent message but when it became = 0, it didnt sent the motion is now absent. which makes me believe that the alert_sent is not working properly

hi @PeteKnight
i have it working now. i get what you mean now and it is now working properly. i will test it tomorrow with real time notification. but in the serial monitor, i am assuming that it is working now

this is the code that i have been using now

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "REDACTED"
#define BLYNK_DEVICE_NAME "REDACTED"
#define BLYNK_AUTH_TOKEN "REDACTED"

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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "REDACTED";


// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "REDACTED";  //Enter your WIFI Name
char pass[] = "REDACTED";  //Enter your WIFI Password

int Status = 12;  // Digital pin D6
int sensor = 13;  // Digital pin D7

float MotionSensor = 0; // We will send an alert if the sensor outputs HIGH
bool alert_sent = false; // Flag to track if an alert has been sent for a motion detected event
 
BlynkTimer timer; // This function creates the timer object. It's part of Blynk library

void MotionSensorTimer() 
{
  float sensor = digitalRead(13);
  Serial.print("sensor = ");
  Serial.println(digitalRead(13));
  Blynk.virtualWrite(V0, sensor); 
  if(sensor > MotionSensor && alert_sent == false)
  {
    Blynk.logEvent("motion_detected");  // trigger the notification
    alert_sent = true;  // set the flag to indicate that an alert has been sent for this motion detected situation
    Serial.println("Blynk Alert Notification sent!");
    Serial.println();
  }
  else if(sensor <= MotionSensor && alert_sent == true)
  {
    alert_sent = false; // reset the flag so that a future motion detection situation will trigger an alert
    Serial.println("Motion is now absent");
    Serial.println("New motion detection events will trigger a new alert notification");
    Serial.println();
  }
}



void setup() 
{
  Serial.begin(9600);
  //Connecting to Blynk Cloud
  Blynk.begin(auth, ssid, pass); 
  
  // Setting interval to send data to Blynk Cloud to 1000ms. 
  // It means that data will be sent every second
  timer.setInterval(10000L, MotionSensorTimer); // take a reading from the PIR sensor every 10 seconds

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

and this is the output in the serial monitor

[6393] Connected to WiFi
[6394] IP: 192.168.1.130
[6394] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.1.0 on ESP8266

 #StandWithUkraine    https://bit.ly/swua


[6519] Connecting to blynk.cloud:80
[6850] Ready (ping: 37ms).
sensor = 1
Blynk Alert Notification sent!
sensor = 1
sensor = 0
Motion is now absent
New motion detection events will trigger a new alert notification

sensor = 1
Blynk Alert Notification sent!

thankyou for all your help and patience :)))

Well, first of all, this:

should say:

Serial.println(sensor);

because youā€™ve already done a digital read on pin 13 and saved the value to sensor and it is this value, not any subsequent digital read results, that will be used in the if statement.

but, there are three variables used in your if/else statements and to fully understand why a particular if/else statement is evaluating as true you need to serial print all of them, not just one.

Pete.

Hi @PeteKnight Thankyou for pointing that out. iā€™ll make sure to change it and will update tomorrow if the notification is working properly.

Thanks!

Hi @PeteKnight
the code seems to work properly and i am getting the desired output. thankyou very much for all the help and patience. I learned a lot from this discussion thankyou!!

1 Like