• Ankita Sharma

Gmail Alert using IOT - Esp8266

Updated: Dec 16, 2020

The Internet of Things ( IoT) aims towards making life simpler by automating every small task around us. Alerts play an important role in IoT developments to notify the user about something important. It can be either in the form of Gmail notifications, LED indication / SMS alerts, or any other form.


In this article, we will learn How to get Gmail notifications using NodeMCU Esp8266. The simplest method to send and receive Gmail alerts from an ESP8266 WiFi module is by using the Blynk app.


Hardware Components :


1. Breadboard


breadboard
Breadboard

They are basically a construction base used for prototyping electronic circuits. Breadboards allow us to make quick connections between hardware components, without soldering.


Buy Breadboard from here.


2. Esp8266 NodeMCU


NodeMCU
Esp8266 NodeMCU

It is an open-source development board specially targeted for IoT developments. It is having integrated support for WiFi connection. Also, it is very cost-efficient as well as consumes less power. It allows us to use it as a micro-controller and manipulate input and output.


Buy Esp8266 from here.


3. Ultrasonic Sensor HC-SR04


Ultrasonic Sensor
HC-SR 04 Ultrasonic Sensor

The sensor contains an eye-like structure that acts as a transmitter (T) and receiver (R). The Ultrasonic transmitter transmits an ultrasonic wave that travels in the air and gets reflected towards the Ultrasonic receiver module.


It works with the basic formula for calculating distance in terms of speed and time i.e. ( Distance = Speed × Time ). Here, the time of flight includes the distance traveled back and forth, so we need to divide the time of flight by 2 while calculating the distance. Thus, D = S x T/2 where T is the time taken for the waves to hit the object and reflect. It consists of 4 pins: Vcc ( Power supply ), Trigger ( input pin ), Echo( output pin ), GND (ground).


Buy HC-SR04 Sensor from here.


4. Jumper Wires


jumper wires
Jumper Wires

Jumper wires are the connecting wires that have connector pins at each end, allowing them to be used to connect the two points without soldering. They typically come in three versions - male-to-male, male-to-female, and female-to-female. Here, we will be using male-to-male jumper wires to interface Ultrasonic Sensor with NodeMCU.


Buy Jumper Wires from here.


Softwares :


1. Arduino IDE


Arduino Integrated Development Environment is open-source software that facilitates the development and uploading of codes to the microcontroller. Program written in Arduino IDE is called Sketch. These Sketch files are saved with the file extension ino.


Click on the link to download the software



2. Blynk App


Blynk is an IoT platform that allows you to quickly build user interfaces for controlling and monitoring your hardware projects from your android phones.


Circuit Connections :


circuit diagram
Interfacing Ultrasonic sensor with NodeMCU

Here, connections are pretty simple. The four pins ( VCC, GND, trigger, echo ) of HC SR 04 #Ultrasonic Sensor is connected as mentioned below -


  1. The VCC pin of HC SR 04 is connected to the VCC pin of Esp8266.

  2. GND pin of HC SR 04 is connected to the GND pin of Esp8266.

  3. The trigger pin of HC SR 04 is connected to the D1 pin of Esp8266.

  4. Echo pin of HC SR 04 is connected to the D2 pin of Esp8266.


Blynk App settings :


1. Create a new project.

Blynk App Menu
Blynk App Menu

2. Name your project ( say Gmail ) then select the hardware device ( #NodeMCU in this case ). Here we will be using a WiFi network, so choose the connection type as Wi-Fi

New Project Menu on Blynk App
New Project Menu

Now, click on Create, you will get an authentication token to your id.


3. Go to Widget Box by clicking on the top right corner, Now select gauge settings to measure the distance from the Ultrasonic #Sensor.

Gauge settings on Blynk App
Gauge Settings

Name your gauge as Distance and then select V5 pin as an input pin. Give Label as cm.


4. Now go to notifications and select #gmail.

Notification Settings
Notification Settings

5. Go to Email settings and check your mail-id.

Email Settings
Email Settings

Code Explanation :


The code explanation is as follows -

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

Include all the necessary header files.

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

Defining trigger and Echo pin of NodeMCU as pin 5 and pin 4 respectively.

long duration;
int distance;

Taking variables duration and distance of long and int type respectively.

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

Taking auth[ ], ssid[ ], pass[ ] as char arrays for storing authentication token( received in the mail after creating New Project on Blynk App ), WiFi username, and WiFi password respectively.

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
}

Defining pinMode( triggerPin as output and echo as input ).

void loop(){
  Blynk.run();
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

Making triggerPin low at first for 2 micro secs and then making it high for 10 micro secs and again making it as low.

duration = pulseIn(echo, HIGH);

Calculating the duration using the pulseIn function with parameter echo and making it HIGH.

distance= duration*0.034/2;

Calculating distance by the basic formula ( i.e. Distance = Speed x Duration ).

Blynk.virtualWrite(V5, distance);

Taking distance and reflecting on the Blynk app ( V5 is the pin which we selected as the output pin on the Blynk App. )

if (distance <= 10){
    Blynk.email("************@gmail.com", "ESP8266 Alert", "distance <= 10");
    Blynk.notify("ESP8266 Alert -> distance <= 10");
  }

Whenever the distance measured is less than 10 cm, #Blynk. email and Blynk. notify functions will be invoked.

  Serial.print("Distance: ");
  Serial.println(distance);
}

To show the distance from the serial monitor.


Complete Code :

#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.email("************@gmail.com", "ESP8266 Alert", "distance <= 10");
    Blynk.notify("ESP8266 Alert -> distance <= 10");
  }
  Serial.print("Distance: ");
  Serial.println(distance);
}

Note:

  1. Do not forget to change the file extension to .ino(Arduino IDE) file format before you upload the code to #ESP8266 Board.

  2. Change the auth[ ], ssid[ ], pass[ ] as authentication token (received on mail), Wi-Fi Username and Wi-Fi password respectively.

  3. Replace the Blynk. email function parameter in the code with your email id.

  4. Also, you need to install the Esp8266 library and BlynkSimpleEsp8266 library in Arduino IDE before uploading the code to NodeMCU.

  5. For installing Esp8266 : Go to Tools > Board > Boards Manager > Esp8266 > Click on install .

  6. For installing Blynk library: Go to Sketch > Include libraries > Manage libraries > Search for Blynk and press ENTER > Click on install.


Download the document given below, copy the code, and then paste it into the Arduino IDE code window.

code
.txt
TXT • 989B

Once the hardware connections are completed and the code is written, connect NodeMCU to the system and insert the code. Now, Run the code.


Working :

Circuit Design of Gmail alert using Esp8266
Gmail alert using Esp8266

The distance of the barrier from the sensor is measured and sent to the Blynk app. The gauge on the Blynk app shows distance measured using an ultrasonic sensor. Whenever the distance measured is less than or equal to 10 cm ( <= 10 cm ), an alert message is received via Gmail.


Do watch the video given below to get a better idea through practical implementation.


Video By - Abhishek Goud


Conclusion :


This article gives an overview of #interfacing NodeMCU with an Ultrasonic sensor to get Gmail alerts, depending on the distance measured. This kind of interfacing can be used in many IoT projects and developments including Water level indicator systems, and Smart Containers.

See also