• Saurabh Harihar

Notification Alert using ESP8266 - IoT

Updated: Dec 2, 2020

In different IoT projects and applications, notifications or alerts play an important role. Notification/alarms are used to notify the user about something important. Alerts can be in different forms such as LED indication, buzzer indication, vibration, a mobile notification. In this project, we will be exploring ' how to push notification alert using esp8266 and the Blynk app'.


Hardware requirements

Esp8266 / Node MCU

Node MCU

NodeMCU is an open-source firmware and development kit. It has SoC WiFi.

  • Input voltage: 7 V to 12 V.

  • 16 digital GPIO pins

  • 1 SPI

  • 1 I2C

  • 2 UART

  • On-chip RAM - 128kb

  • On-chip ROM - 4Mb


Ultrasonic sensor.

Ultrasonic sensor

The ultrasonic sensor transmits sound waves at high frequencies.

  • The ultrasonic sensor works on the principle of speed = distance/time.

  • 5V DC power supply requirement.

  • Angle deviation - 15 degrees.

  • Range - 3 cm to 4 m.


Breadboard

Breadboard

Breadboards are electronic circuit prototyping boards. Breadboards are available in various sizes. Many electronic components such as sensors, actuators, display modules, IoT boards can be interfaced using a breadboard.


Jumper wires

Jumper wires

Jumper wires are used for connecting terminals of electronic devices. Jumper wires are used to connect header pins on the circuit boards to each other and the breadboard. Male to male, female to female, and male to female are the types of jumper wires available.


Software


Blynk android application

  • Blynk is an open-source IoT platform used for monitoring, data analytics, machine learning.

  • Blynk allows creating amazing interfaces for your projects using different widgets.

  • Blynk server establishes a connection between hardware and the Blynk app. WiFi, Bluetooth, GSM, USB, Ethernet can be used for connecting to the Blynk Cloud.


Arduino IDE

  • Arduino IDE is open source software used to write Arduino code, compile and upload it to any Arduino board. Program written in Arduino IDE is called a sketch.

  • Arduino sketch files are saved with extension .ino. Before compiling, uploading any Arduino sketch, the correct development board and COM port need to be selected.

  • Many inbuilt libraries and example sketches are also available in Arduino IDE.


Circuit diagram

Circuit diagram

There are 4 pins on the ultrasonic sensor HC SR 05.

  • VCC pin of HC SR 04 is connected to VCC pin of esp8266.

  • GND pin of HC SR 04 is connected to the GND pin of esp8266.

  • The trigger pin of HC SR 04 is connected to the D2 pin of esp8266.

  • Echo pin of HC SR 04 is connected to the D1 pin of esp8266.


Procedure on the Blynk app


Blynk app homescreen
  • First of all, create a new project and enter the project name. Now you have to select a widget from the widget box.

New project settings
  • Select hardware Node MCU. Select connection type as WiFi. Choose any theme. Now click on 'Create'.

Gauge settings
  • To get the widget box click on the place icon available in the top right corner.

  • Select a gauge to represent distance. Give a name to gauge as distance, select pin V5 as an input pin. Give label as cm. Select refresh rate, save option is not there these setting get automatically saved.

  • Select a notification alert from the widget box and keep its settings as default. You will get an authentication token in your mail.

Code

First of all, you need to install the esp8266 library and BlynkSimpleEsp8266 library in Arduino IDE by using the library manager.


The code description is as follows.

#include <ESP8266WiFi.h>

As we are using the ESP8266 chip for WiFi, the corresponding header file is included.

#include <BlynkSimpleEsp8266.h>

We are going to use the Blynk app for monitoring distance measured. So the corresponding Blynk library needs to be included in the code.

const int triggerPin = 5;
const int echo = 4;

Set digital pin 5 as trigger pin and digital pin 4 as echo pin.

long duration;
int distance;

Declared duration and distance variables.

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

Set the SSID, authentication id, and password.

void setup(){
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(triggerPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echo, INPUT); // Sets the echoPin as an Input
}

In the setup method, serial communication is initiated, Blynk is initiated. The trigger pin is set to OUTPUT. The echo pin is set as INPUT.

  Blynk.run();

In the loop method, the run function is used to start the Blynk app.

  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);

The trigger pin is set to LOW initially. A Delay of 2 microseconds is given.

  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

Trigger pin is set to HIGH for 10 microseconds.

  duration = pulseIn(echo, HIGH);
  distance= duration*0.034/2;

Now pulse in function is used to obtain duration.

Now using the formula distance = speed x time / 2, distance is calculated.

 Blynk.virtualWrite(V5, distance);

Virtualwrite function is used to fetch the distance and display it on the gauge on the Blynk app.

  if (distance <= 10){
    Blynk.notify("ESP8266 Alert -> distance <= 10");
  }
  Serial.print("Distance: ");
  Serial.println(distance);

According to logic in code, if the distance is less than or equal to 10 cm. a notification alert will be displayed using the notify function. Distance is displayed on the serial monitor using serial.print() function.


COMPLETE CODE:

First of all, you need to install the esp8266 library and BlynkSimpleEsp8266 library in Arduino IDE by using the library manager.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
const int triggerPin = 5;
const int echo = 4;

long duration;
int distance;

char auth[] = "***********************";
char ssid[] = "****************";
char pass[] = "**************";
void setup(){
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(triggerPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echo, INPUT); // Sets the echoPin as an Input
}
void loop(){
  Blynk.run();
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  duration = pulseIn(echo, HIGH);
  distance= duration*0.034/2;
  Blynk.virtualWrite(V5, distance);
  if (distance <= 10){
    Blynk.notify("ESP8266 Alert -> distance <= 10");
  }
  Serial.print("Distance: ");
  Serial.println(distance);
}
code
.txt
Download TXT • 910B

Note: Please change the file extension to .ino (Arduino IDE) file format before uploading the code to ESP8266 Board.


Working:

As we start to run our project. Distance is calculated and sent to the Blynk server. Then the data is sent from the Blynk server to the Blynk app. The gauge on the Blynk app shows distance measured using an ultrasonic sensor. According to our logic in the code, when the distance becomes less than or equal to 10 cm. an alert ( notification ) is shown on the Blynk app. In the alert message, the argument passed in Blynk. notify() function is displayed.


Video By - Abhishek Goud


Conclusion

In this article, we measured and monitored distance. We used the Blynk IoT platform to push notifications. Similarly, this concept can be used to push notifications in different applications. In different IoT projects, notifications are required to be sent. We can use notifications in projects like temperature measurement, water level monitoring, gas detection, smoke detection, and many more.

See also

  1. Controlling GPIO pins of esp8266 using Telegram app.

  2. Smart lighting system - IOT using esp8266.

  3. Controlling LED using Blynk app.


  • Facebook
  • YouTube
  • Instagram
  • LinkedIn
LearnElectronics
Contact Us
© Copyright - LearnElectronics 2021