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.
#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.