NodeMCU ESP8266 Flood Monitoring System with GSM Alert and Ultrasonic Sensor
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
The circuit is designed to monitor water levels and provide alerts in case of flooding. It consists of an ultrasonic sensor for measuring water levels, a float switch for detecting water presence, an I2C LCD screen for displaying information, a GSM module for sending SMS alerts, and a NodeMCU V3 ESP8266 microcontroller for controlling the system.
Component List
I2C LCD 16x2 Screen
- Description: A 16x2 character LCD display that uses the I2C communication protocol.
- Pins: SCL, SDA, VCC (5V), GND, VDD, VO, RS, RW, E, D0-D7, BLA, BLK
SIM800L GSM Module
- Description: A GSM/GPRS module that allows the microcontroller to communicate over a mobile network.
- Pins: 5V, GND, VDD, SIM_TXD, SIM_RXD, RST
HC-SR04 Ultrasonic Sensor
- Description: An ultrasonic ranging module that provides 2cm to 400cm non-contact measurement functionality.
- Pins: VCC, TRIG, ECHO, GND
Float Switch
- Description: A switch that is triggered by the rising or falling level of liquid.
- Pins: GND, Digital Pin
NodeMCU V3 ESP8266
- Description: An open-source Lua based firmware for the ESP8266 WiFi SOC from Espressif and uses the Lua scripting language.
- Pins: A0, GND, VU, S3-S0, SK, 3V3, EN, RST, Vin, D0-D8, RX, TX
Wiring Details
I2C LCD 16x2 Screen
- SCL: Connected to D1 of NodeMCU V3 ESP8266
- SDA: Connected to D2 of NodeMCU V3 ESP8266
- VCC (5V): Connected to Vin of NodeMCU V3 ESP8266
- GND: Connected to GND of NodeMCU V3 ESP8266
SIM800L GSM Module
- 5V: Connected to Vin of NodeMCU V3 ESP8266
- GND: Connected to GND of NodeMCU V3 ESP8266
- SIM_TXD: Connected to D4 of NodeMCU V3 ESP8266
- SIM_RXD: Connected to D3 of NodeMCU V3 ESP8266
HC-SR04 Ultrasonic Sensor
- VCC: Connected to 3V3 of NodeMCU V3 ESP8266
- TRIG: Connected to D5 of NodeMCU V3 ESP8266
- ECHO: Connected to D6 of NodeMCU V3 ESP8266
- GND: Connected to GND of NodeMCU V3 ESP8266
Float Switch
- Digital Pin: Connected to A0 of NodeMCU V3 ESP8266
- GND: Connected to GND of NodeMCU V3 ESP8266
Documented Code
NodeMCU V3 ESP8266
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>
#include <SoftwareSerial.h>
// LCD Display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Ultrasonic Sensor
#define TRIGGER_PIN 12 // NodeMCU V3 ESP8266 D5
#define ECHO_PIN 11 // NodeMCU V3 ESP8266 D6
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
// Float Sensor
#define FLOAT_SENSOR_PIN 10 // NodeMCU V3 ESP8266 A0
// GSM Module
SoftwareSerial gsmSerial(8, 9); // NodeMCU V3 ESP8266 D4 (TX), D3 (RX)
#define GSM_BAUDRATE 9600
// Thresholds
#define FLOOD_THRESHOLD 50 // Example threshold in cm
// Phone Numbers
String phoneNumbers[] = { "+639568667239" }; // Example phone numbers
void setup() {
// Initialize LCD Display
lcd.begin(16, 2);
lcd.backlight();
// Initialize GSM Module
gsmSerial.begin(GSM_BAUDRATE);
delay(2000); // Give GSM module time to initialize
sendCommand("AT"); // Check communication
sendCommand("AT+CMGF=1"); // Set SMS text mode
// Display Initialization Message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Flood Monitoring");
lcd.setCursor(0, 1);
lcd.print("System");
delay(3000); // Display initialization message for 3 seconds
}
void loop() {
// Read Ultrasonic Sensor
unsigned int distance = sonar.ping_cm();
// Read Float Sensor
int floatSensorValue = digitalRead(FLOAT_SENSOR_PIN);
// Calculate Flood Level
int floodLevel = distance;
// Update LCD Display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Water Level: ");
lcd.print(floodLevel);
lcd.print("cm");
// Check Flood Threshold
if (floodLevel > FLOOD_THRESHOLD && floatSensorValue == HIGH) {
// Send Alert SMS
sendAlertSMS(floodLevel);
}
delay(500); // Delay for stability
}
void sendAlertSMS(int floodLevel) {
String message = "Flood Alert! Water level is ";
message += floodLevel;
message += "cm. Take necessary actions.";
for (int i = 0; i < sizeof(phoneNumbers) / sizeof(phoneNumbers[0]); i++) {
sendCommand("AT+CMGS=\"" + phoneNumbers[i] + "\"");
delay(1000);
sendCommand(message);
delay(100);
sendCommand((String) char(26)); // End AT command with a CTRL+Z
delay(1000);
}
}
void sendCommand(String command) {
gsmSerial.println(command);
delay(1000);
while (gsmSerial.available()) {
gsmSerial.read(); // Clear the buffer
}
}
This code is responsible for initializing the components, reading sensor data, updating the LCD display, and sending SMS alerts when the flood level exceeds a predefined threshold. The sendAlertSMS function constructs the alert message and sends it to the predefined phone numbers using the GSM module. The sendCommand function is a utility to send AT commands to the GSM module and handle the response.