Intruder alert notification blynk 2.0

i need help on how to send a notification on my android phone whenever my sensor detect below 5 cm. im using esp 8266 + ultrasonic sensor + buzzer.

 * this program is under editing for push notification
// Fill-in information from your Blynk Template here

#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial

#include "BlynkEdgent.h"

#define echoPin D7
#define trigPin D6
#define buzzer D1

long duration;
int distance; 
int safetyDistance;

void ultrasonic()
    digitalWrite(trigPin, LOW);
    digitalWrite(trigPin, HIGH);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = duration * 0.034 / 2; //formula to calculate the distance for ultrasonic sensor
    Serial.print("Distance: ");
    Blynk.virtualWrite(V0, distance);

    if (safetyDistance <= 5 && safetyDistance != 0) // You can change safe distance from here changing value Ex. 20 , 40 , 60 , 80 , 100, all in cm
      digitalWrite(buzzer, HIGH);
      digitalWrite(buzzer, LOW);

void setup()
  pinMode(34, INPUT);
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT); 
  pinMode(buzzer, OUTPUT);

void loop() {;


pls do provide me with the code since i dont know very much about coding. thank you :slight_smile:

Check the documentation

You are calling your ultrasonic() function in your void loop, and that function contains this line of code:

This is the same as putting that Blynk.virtualWrite() command directly into your void loop, and this breaks the golden rule of Blynk - no Blynk.virtualWrite() commands in the void loop.

I’ve already pointed you to the documentation which explains why this is a really bad idea in your previous topic…

so, once again, DO NOT RUN THIS CODE! it will flood the Blynk server with far too many Blynk.virtualWrites per second.

You need to call your your ultrasonic() function with a BlynkTimer, not from the void loop.

You will also need to use a “Flag” variable so that you only send one notification each time the level falls below 5cm, otherwise you will quickly run out of notifications as they are limited to 100 per 24 hour period. You should read this for more information…