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_TEMPLATE_ID "TMPLfaFdxJ4j"
#define BLYNK_DEVICE_NAME "TEST ESP8266"



#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define USE_NODE_MCU_BOARD

#include "BlynkEdgent.h"

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

long duration;
int distance; 
int safetyDistance;

void ultrasonic()
{
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = duration * 0.034 / 2; //formula to calculate the distance for ultrasonic sensor
    Serial.print("Distance: ");
    Serial.println(distance);
    Blynk.virtualWrite(V0, distance);
    safetyDistance=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);
     
    }
    else{
      digitalWrite(buzzer, LOW);
    }
}

void setup()
{
  Serial.begin(9600);
  pinMode(34, INPUT);
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT); 
  pinMode(buzzer, OUTPUT);
  BlynkEdgent.begin();
  delay(2000); 
}

void loop() {
  BlynkEdgent.run();
  ultrasonic();

  
}

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

Check the documentation
https://docs.blynk.io/en/getting-started/events-tutorial#use-blynk.logevent-firmware-api

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…

Pete.