#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
const int trigPin1 = D3;
const int echoPin1 = D2;
char auth[]="97VFrfG0MUtLVpIzex_WwG09YkwDsH2Y";
char ssid[] = "DESKTOPDSTSEED";
char pass[] = "India1234";
int duration1,distance1;
void setup()
{
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = duration1 * 0.034 / 2;
Blynk.virtualWrite(V1,distance1);
if(distance1<10)
{
Blynk.notify("Blockage Alert!");
}
Blynk.run();
}
@vijaysiva please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
Pete.
updated with triple back ticks at beginning and end of the code
Thanks for your response
First of all, you should read this…
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean
You’ll then realise that you are breaking one of the golden rules of Blynk by having a Blynk.virtualWrite in your void loop.
You also have no mechanism, such as a flag variable, to prevent multiple notifications being sent for the same error situation.
Are you using Blynk Legacy, or the new Blynk IoT ?
Pete.
Thanks for your response. I am using blynk legacy. I will try to fix the issue as per your suggestion
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define trig D3
#define echo D2
long duration;
int distance;
// You should get Auth Token in the Blynk App.
char auth[]="97VFrfG0MUtLVpIzex_WwG09YkwDsH2Y";
char ssid[] = "vijayhathway";
char pass[] = "vijay@1234";
BlynkTimer timer;
void setup()
{
// Debug console
pinMode(trig, OUTPUT); // Sets the trigPin as an Output
pinMode(echo, INPUT); // Sets the echoPin as an Inpu
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, sendSensor);
}
void sendSensor()
{
digitalWrite(trig, LOW); // Makes trigPin low
delayMicroseconds(2); // 2 micro second delay
digitalWrite(trig, HIGH); // tigPin high
delayMicroseconds(10); // trigPin high for 10 micro seconds
digitalWrite(trig, LOW); // trigPin low
duration = pulseIn(echo, HIGH); //Read echo pin, time in microseconds
distance = duration * 0.034 / 2; //Calculating actual/real distance
Serial.print("Distance = "); //Output distance on arduino serial monitor
Serial.println(distance);
if(distance <10)
{
Blynk.notify("Blockage alert");
}
Blynk.virtualWrite(V1, distance);
delay(1000); //Pause for 3 seconds and start measuring distance again
}
void loop()
{
Blynk.run();
timer.run();
}
I have modified the code as per your suggestion but i still did not get the push notification. I am using blynk legacy. please let me know whether push notification service is no more available with blynk legacy version. Thanks once again
Notifications still work with Blynk Legacy, at least for the time being.
Have you added the notification widget to your app project?
Pete.
Thanks for your support pete the problem has been solved