Need help for push notification

I am recently working on a Ultrasonic sensor HC-SR04 to determine the water level. I wanted to get notification when it is in high water level but it just dont work. My esp8266 keeps on repeating online and offline according to Blynk Apps on my phone. I dont know what’s wrong is it. Can anyone please help me in it? Im using Arduino Uno + HC-SR04 + Esp8266 (esp 01) + Blynk. This is my code. Please help me out as i dont even know what’s wrong. Thank you so much!!

#include <Blynk.h>

#define trigPin 6
#define echoPin 7
#define ledlow 8
#define ledmed 9
#define ledhigh 10
#define buzzer 11
#define BLYNK_PRINT Serial

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

char auth[] = "*******************";
char ssid[] = "*********";
char pass[] = "*******";

void setup() {
  Serial.begin (9600);
  Blynk.begin(auth, ssid, pass);
  Serial.println("Blynk Begin");
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledlow, OUTPUT);
  pinMode(ledmed, OUTPUT);
  pinMode(ledhigh, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.println ("pin done");
  Serial.println("Sensor begin");
  Blynk.notify("Sensor is working");
  Serial.println("Blynk notification sent");
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

  if (distance >= 20)
  {
    Serial.println("Water Level is Low");
    digitalWrite(ledlow, HIGH);
    Blynk.notify("Water Level: Low");
    Serial.println("Notification Sent");
  }
  else
  {
    digitalWrite(ledlow, LOW);
  }

  if (distance < 20 && distance >= 10)
  {
    Serial.println("Water level is average");
    digitalWrite(ledmed, HIGH);
    Blynk.notify("Water Level: Medium");
    Serial.println("Notification Sent");
  }
  else
{
  digitalWrite(ledmed, LOW);
}
  if (distance < 10 && distance>= 0)
  {
    Serial.println ("Water level is high");
    digitalWrite(ledhigh, HIGH);
    Blynk.notify("Alert! Water Level: High");
    Serial.println("Notification Sent");
  }
  else
{
  digitalWrite(ledhigh, LOW);
}
  if (distance <= 0)
  {
    Serial.println ("Out of range");
  }

  else
  {
    Serial.print(distance);
    Serial.println(" cm");
    
  }
  Blynk.run();
  delay(1000);
}

Go back and edit your post so that the code is correctly formatted.

When you’ve done that we’ll point-out where you’re going wrong.

Pete.

1 Like

@yaujiet I fixed your post as per the directions…

Now you need to read and study the Documents and Help Center files and get your code out of the void loop in order for it to work with Blynk properly.

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-display-any-sensor-data-in-blynk-app

@PeteKnight @Gunner
Hi after reading the examples from blynk. I edited my coding but it still dont works. My esp8266 keeps online and offline repeatly. Can anyone help me out? I have no idea whats going on


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>
#include <SimpleTimer.h>
#define trigPin 6
#define echoPin 7
#define led1 8
#define led2 9
#define led3 10
#define buzzer 11

char auth[] = "************************";
char ssid[] = "Meeting";
char pass[] = "*************";

SimpleTimer timer;


void sendSensor() {
  
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(buzzer, OUTPUT);

    

  if (distance >= 20)
  {
    digitalWrite(led1, HIGH);
    Blynk.notify("Water Level: Low");
    Serial.println("Water Level is low");
  }
  else
  {
    digitalWrite(led1, LOW);
  }


  if (distance < 20 && distance >= 10)
  {
    digitalWrite(led2, HIGH);
    Blynk.notify("Water Level: Medium");
    Serial.println("Water level is average");
  }
  else
  {
    digitalWrite(led2, LOW);
  }

  if (distance < 10 && distance >= 0)
  {
    digitalWrite(led3, HIGH);
    Blynk.notify("Alert! Water Level: HIGH!");
    Serial.println("Water level is high");
  }
  else
  {
    digitalWrite(led3, LOW);
  }

  if(distance <= 0)
  {
    Serial.println ("Out of range");
  }
  else
  {
    Serial.print(distance);
    Serial.println(" cm");
  }
}

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth,ssid,pass);
  timer.setInterval(2500L, sendSensor);
  
}

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

Check the other post you created with the exact same topic.

P.S. Don’t create multiple posts about the same topic.

1 Like

You don’t need to declare the pinModes every 2.5 seconds. Put those in void setup and you’ll save some processing time in your void sendSensor function.
Notifications are limited to one every 15 seconds. You should have a flag to keep track that a particular type of notification has been sent, and not send repeated notifications of the same type.

Pete.

Hi everyone. Im working with a ultrasonic sensor as a water level sensor project. I want to get notification when the water level is high but when I connect to blynk, the esp8266 keeps on online and offline repeatly. I dont know what happened. Can anyone help me out? This is my code…


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>
#include <SimpleTimer.h>
#define trigPin 6
#define echoPin 7
#define led1 8
#define led2 9
#define led3 10
#define buzzer 11

char auth[] = "f*************************";
char ssid[] = "Meeting";
char pass[] = "***********";

SimpleTimer timer;


void sendSensor() {
  
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(buzzer, OUTPUT);

    

  if (distance >= 20)
  {
    digitalWrite(led1, HIGH);
    Blynk.notify("Water Level: Low");
    Serial.println("Water Level is low");
  }
  else
  {
    digitalWrite(led1, LOW);
  }


  if (distance < 20 && distance >= 10)
  {
    digitalWrite(led2, HIGH);
    Blynk.notify("Water Level: Medium");
    Serial.println("Water level is average");
  }
  else
  {
    digitalWrite(led2, LOW);
  }

  if (distance < 10 && distance >= 0)
  {
    digitalWrite(led3, HIGH);
    Blynk.notify("Alert! Water Level: HIGH!");
    Serial.println("Water level is high");
  }
  else
  {
    digitalWrite(led3, LOW);
  }

  if(distance <= 0)
  {
    Serial.println ("Out of range");
  }
  else
  {
    Serial.print(distance);
    Serial.println(" cm");
  }
}

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth,ssid,pass);
  timer.setInterval(2500L, sendSensor);
  
}

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

ESP “special” pins

I suspect it may have something to do with the pins you used. Pins 6 through 11 are used for flashing. Try using the correct pins and report back.

FYI, I merged both topics.

Hmm what do you mean by “You should have a flag to keep track that a particular type of notification has been sent, and not send repeated notifications of the same type.” ?

Google programming tips with flags, counters, boolean logic and so on… it is common programming practice and nothing specific to Blynk.

This is my connection of hardware. Is that any problem with my connection?

Arduino Esp8266
Gnd Gnd
3.3v Vcc
3.3v CH_PD
RxD Rx
TxD Tx

Arduino HC-SR04
5v Vcc
Gnd Gnd
pin6 trig
pin7 echo

RX from one goes to TX of the other and vice versa… Think mouth to ear.

Or just Google proper ways to connect Arduino to ESP :stuck_out_tongue_winking_eye:

You can also search this forum for one of the dozens of topics all about connecting Arduino to ESP… every possible question & solution has already been mentioned a few times.

Use key words like Arduino, UNO, ESP-01, Shield

You currently have four types of notification message: Low, Medium, High & Out of Range.
If Blynk allowed it, you would be getting notification alerts every 2.5 seconds, or almost 1,500 notifications per hour - all day, every day.

For a push notification to be of any use, it needs to tell you that something notable has happened, not keep telling you the same thing over and over and over again. So, you only want to send a notification when the status changes - say from Medium to High. To do this, I’d use a flag to keep track of the fact that a message regarding a particular notification (Medium for example) has already been sent, and that another notification doesn’t need to be sent until the level changes to either Low or High.

This could/will still cause you some issues when the water level is very close to the threshold between two of your levels. You use 20 as the threshold between High and Medium, and a Long variable type to store the distance. 19.999999999 will give a Low message 20 will give a High message so you can expect subsequent readings to hover between Low and High.
If you used an integer for distance then the reading would need to change from 19 to 20 to trigger a notification and depending how you did your rounding it would probably require a 0.5 (cm ?) change in level to cause the flip between Low and High.

Personally, I wouldn’t even bother with a notification for Medium (which presumably is the norm) only for Low and High I fell these are critical situations. Because of my age, I’d also use a set of LED widgets in the app to show the level using an old VU meter style of display, with green LED for the middle range and red for high or low. The youngsters would use a gauge widget instead :wink:

Pete.

So if i just want to show notification when the water level goes from medium to high, what i gonna change for my coding? Im still very new to programming as i only knows basic C++

I’m not going to write your code for you. @Gunner and I have given you enough pointers (see what I did there :grinning:) to help you on your way. You need to invest some time in learning how to write your own code.
If you get stuck along the way then come back and post your latest code (correctly formatted this time) and some info about the issues you’re having and we’ll point you in the right direction.

Pete.

you can add two timer for functions. you can chech water level however you want but you must send notification 4 times in minute. That mean you must wait 15 seconds for next notification.

That’s still 240 notifications per hour. 5,760 in 24 hours. What use is that to anyone?

You don’t want to be told 2 million times per year that your house isn’t on fire, you want to deceive an alert when it is on fire. It’s called Reporting my Exception and it’s where you flag-up notable events, rather than drowning the use in useless repetitive data that requires no action.

Pete.

if you put your flags in if else condition, you get notification just your house in fire. else, not.

You must check notification state with minimum 15 seconds delay. if you increase delay time, for example 1 hour, you may get notification after 1 hour.


void sendNotif() {
if (sendNot == 1) {

 //notify

{ else {

 //drink coffe
 
}

void checkState() {

if (digitalRead(2) == HIGH) {
  sendNot = 1;
} else {
  sendNot = 0;
}

Run sendNotif() 4 times in minute, checkState 4 times in hour

813/5000
The problem you have is that you collapse the server with so many notifications and that’s why it goes offline. There are two ways to treat the problem you have:

  1. Through a timer I sent a status notification every so often. (I do not recommend it to you since it would stop you all the time seeing notifications).
  2. The best and most appropriate would be to only send the notification once just when the values of low, medium, high change. (You do not have to send repeated notifications)
    This is achieved by using state flags:
    If it goes from low to medium state, the middle flag changes to 1 and you send the message. As long as the flag is medium = 1 nothing happens until another event occurs and the deposit goes low or high and the flag would be changed and only a notification will be sent again.

El problema que tienes es que colapsas el servidor con tantas notificaciones y por eso se va a fuera de linea. Existen dos formas para tratar el problema que tienes:

  1. A través de un temporizador que envié una notificación del estado cada cierto tiempo. (No te la recomiendo ya que te pararia todo el tiempo viendo notificaciones).
  2. La mejor y mas apropiada seria que solo se envíe una vez la notificación justo cuando cambien los valores de bajo, medio, alto. (No tienes que enviar notificaciones repetidas)
    Esto se consigue utilizando flags de estado:
    Si pasa de estado bajo a medio el flag medio pasa a 1 y envías el mensaje. Mientras sea el flag medio =1 no sucede nada hasta que se produzca otro evento y el deposito pase a bajo o alto y se cambiaría el flag y se enviaría de nuevo solo una notificación.