Here to learn how to build an IoT Based Flood Monitoring and Email, SMS alert System using Ultrasonic, NodeMCU ESP8266 12E, myDevices Cayenne. In more detail, this IoT tutorial discovers how to use an ESP8266 to send data to Cayenne using the MQTT protocol.
![](https://static.wixstatic.com/media/b31662_783a0d58fc5f44739f1a7f65c6eed6be~mv2.jpg/v1/fill/w_980,h_455,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/b31662_783a0d58fc5f44739f1a7f65c6eed6be~mv2.jpg)
Abstract: Flood is one of the natural disasters that cannot be avoided. It happens too fast and affected so many lives and properties. Before this, most of the existing system that has been developed are only focus on certain areas. Other than that, majority of the public cannot monitor and have no idea when the flood going to be happened since they do not have any information and data about the weather condition. This system is suitable for cities and village areas. Furthermore, if the public has an internet access, they can monitor what is happening and predict if there is any upcoming flood at the web server. This project will update the water level at the web server and the system will issue an alert signal to the citizens for evacuation so that fast necessary actions can be taken.
This is a complete step-by-step tutorial on building an IoT system using Cayenne and ESP.
On the other hand, Cayenne is an IoT cloud platform that provides several cloud services, such as:
Data visualization
IoT cloud
Alerts
Scheduling Events
We will focus our attention on data visualization and on the IoT cloud services.
Cayenne IoT Platform accelerates the development of IoT-based solutions, including quick design, prototyping and other commercialized projects. It is a drag-and-drop IoT project builder that can help developers build complete, ready-to-use IoT solutions with little to no coding.
Cayenne IoT Platform contains a vast catalogue of certified IoT-ready devices and connectivity options. This allows users to easily add any device to the library utilizing MQTT API. All devices in Cayenne are interoperable and benefit from features such as rules engine, asset tracking, remote monitoring and control, and tools to visualize real-time and historical data.
MQTT is a lightweight messaging protocol for sensors and mobile devices.
Circuit Diagram
![](https://static.wixstatic.com/media/b31662_16c739096a8e4328b9f89c9a2ba6b2ac~mv2.png/v1/fill/w_780,h_954,al_c,q_90,enc_auto/b31662_16c739096a8e4328b9f89c9a2ba6b2ac~mv2.png)
Components Required
ESP8266 NodeMCU
Ultrasonic Sensor
LEDs (Red & Green)
Breadboard
Jumpers
NodeMCU
NodeMCU ESP8266-12E MCU is a development board with one analogue and many general-purpose input output (GPIO) pins. It has 4MB flash memory, and can operate at a default clock frequency of 80MHz. In this project, digital pin D1 of NodeMCU is used to read Data of Dht11 temperature sensor.
Ultrasonic:
The HC-SR04 ultrasonic module is a module that can provide non-contact measurement within the range of 2cm to 400cm with ranging accuracy that can reach 3mm. It works on the principle of echolocation.
The ultrasonic sensor as a trigger and an echo pin. The arduino provides a high signal of 10microseconds to this pin. After the HC-SR04 is triggered, it sends out eight 40Khz sound waves to the surface of the water. On getting to the surface of the water, the wave is echoed back to the sensor and the ESP8266 reads the echo pin to determine time spent between triggering and receiving of the echo. Since we know that the speed of sound is around 340m/s then we can calculate the distance using;
Distance = (time/2)*speed of sound
Ultrasonic HC-SR04 wiring to ESP8266
Ultrasonic HC-SR04 ESP8266
Vcc Pin Vin Pin
Trig Pin D1 (GPIO 5)
Echo Pin D2 (GPIO 4)
GND Pin GND
Installing the ESP8266_Arduino_Library
To get readings from the Ultrasonic HC-SR04 sensor module you need to have the next library installed:
Download Ultrasonic Library
Download the Cayenne-MQTT-ESP-master library from this link.
Click on Add ZIP Library and add Cayenne-MQTT-ESP-master zip file, or directly copy the folder (Cayenne-MQTT-ESP-master) and paste it in Libraries folder of Arduino IDE.
![](https://static.wixstatic.com/media/b31662_b2823712cae04bf4b0590163f0b2be4a~mv2.jpg/v1/fill/w_980,h_654,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/b31662_b2823712cae04bf4b0590163f0b2be4a~mv2.jpg)
After installing the library, go to your Arduino IDE. Make sure you have the Nodemcu 1.0 ESP-12E board selected, and then, Copy and Paste code in Arduino IDE.
Subscribe and Download code.
Arduino Code
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <Ultrasonic.h>
#include <ESP8266WiFi.h>
Ultrasonic ultrasonic(D1, D2);
const int AlertLED = D3;
#define NormalLED D4
int distance;
int val = 0;
#include <CayenneMQTTESP8266.h>
char ssid[] = "TP-Link_3200"; // your SSID
char password[]="9500112137"; // your WIFi password
char username[] = "2031dd30-5414-11eb-b767-3f1a8f1211ba";
char mqtt_password[] = "f2ce829d98df3a768328ac6936eae9fd47d28289";
char client_id[] = "b15f5e30-5ba9-11eb-b767-3f1a8f1211ba";
void setup() {
Serial.begin(9600);
Serial.print("Setup");
Cayenne.begin(username,mqtt_password,client_id,ssid,password);
pinMode(NormalLED, OUTPUT);
pinMode(AlertLED, OUTPUT);
digitalWrite(NormalLED, LOW);
digitalWrite(AlertLED, LOW);
}
void loop()
{
//Run Cayenne Functions
Cayenne.loop();
}
CAYENNE_OUT(V0){
do {
distance = ultrasonic.read();
delay(2000);
} while (isnan(distance));
Serial.print("Distance in CM: ");
Serial.println(distance);
if (distance <= 3)
{
digitalWrite(D3, HIGH);
digitalWrite(D4, LOW);
}
else
{
digitalWrite(D4, HIGH);
digitalWrite(D3, LOW);
}
Cayenne.virtualWrite(V0, distance);
}
CAYENNE_OUT(V1){
Serial.print(digitalRead(D3));
Cayenne.virtualWrite(V1, digitalRead(D3));
}
Result:
Water drop in bowl raises, the flood alert RED LED to glow and Normal flood condition Green LED OFF.
![](https://static.wixstatic.com/media/b31662_53fe1413725d437a993d3ab6d3a52197~mv2.jpg/v1/fill/w_980,h_654,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/b31662_53fe1413725d437a993d3ab6d3a52197~mv2.jpg)
Hardware interfacing with Cayenne IoT platform
To interface the circuit with the IoT platform, open this link on any browser.
Refer previous tutorial for more deatil about cayenne
Click on Sign Up. Fill all the fields and click on Get Started
Click on Add new and then Device/Widget in Settings, Add New Device here and select Generic ESP8266 for in this project.
Configure device Generic ESP8266, MQTT username, password and client ID from Create App
Paste these respective details under username, password and client ID in Arduino source code , along with your Wi-Fi name and password.
After successfully compiling and uploading the code to NodeMCU, You will see ESP8266 connected to Wi-Fi. After the connection is established, the previous page is automatically updated on Cayenne. A new dashboard opens in the browser. Cayenne generates an ID and a device icon for your device.
![](https://static.wixstatic.com/media/b31662_e136386bea8a4b94bfe34af49cdce060~mv2.jpg/v1/fill/w_980,h_527,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/b31662_e136386bea8a4b94bfe34af49cdce060~mv2.jpg)
Click on Custom Widgets and then value, and populate all fields . The channel number should be 1. (Make sure the channel number is same as in code.) Now, click on Add Widget.
When a connection is made, sensor data gets uploaded to Cayenne. Distance and digital output LED state data on Cayenne.
You can get a graphical representation of flood monitor data by clicking on the Graph icon.
![](https://static.wixstatic.com/media/b31662_4d4405cf2c024a6d9d346f710692a008~mv2.jpg/v1/fill/w_980,h_526,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/b31662_4d4405cf2c024a6d9d346f710692a008~mv2.jpg)
You can set Trigger alert using if and then condition, and add Mobile number and email for receiving alert message.
![](https://static.wixstatic.com/media/b31662_3d6f0c954c06404085fbde8934b2cd52~mv2.jpg/v1/fill/w_980,h_526,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/b31662_3d6f0c954c06404085fbde8934b2cd52~mv2.jpg)
You can set Trigger alert ON/OFF
![](https://static.wixstatic.com/media/b31662_e28f346a96fa4d8789584c55aaf13129~mv2.jpg/v1/fill/w_980,h_526,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/b31662_e28f346a96fa4d8789584c55aaf13129~mv2.jpg)
If Once sensor data gets Reached to Threshold then digital output LED state data to Cayenne.
then Flood alert notify value changed to 1.00.
![](https://static.wixstatic.com/media/b31662_e074565d24e64077a008a399c149c6b3~mv2.jpg/v1/fill/w_980,h_528,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/b31662_e074565d24e64077a008a399c149c6b3~mv2.jpg)
You can get a graphical representation of flood Alert data by clicking on the Graph icon.
![](https://static.wixstatic.com/media/b31662_ae482c90461144799eb525fc6af8b944~mv2.jpg/v1/fill/w_980,h_526,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/b31662_ae482c90461144799eb525fc6af8b944~mv2.jpg)
And also Trigger send alert message to Email.
![](https://static.wixstatic.com/media/b31662_fa7b295df3c94348b166c726f3d7b776~mv2.jpg/v1/fill/w_980,h_508,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/b31662_fa7b295df3c94348b166c726f3d7b776~mv2.jpg)
And Trigger send alert message to Mobile.
![](https://static.wixstatic.com/media/b31662_d0900f08065842f6a57f3ac1c0a2fc54~mv2.jpg/v1/fill/w_980,h_2123,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/b31662_d0900f08065842f6a57f3ac1c0a2fc54~mv2.jpg)
Comments