// Define the pins used in the system #define TRIG_PIN 12 // Ultrasonic sensor trigger pin #define ECHO_PIN 13 // Ultrasonic sensor echo pin #define BUZZER_PIN 5 // Buzzer pin #define LED_PIN 17 // LED pin #define LED1_PIN 18 // Second LED pin
// GSM Module Serial Communication #define RX_PIN 22 // RX pin for GSM #define TX_PIN 23 // TX pin for GSM SoftwareSerial gsmSerial(RX_PIN, TX_PIN);
// Variables to hold sensor readings and IoT cloud properties float waterLevel = 0; // Measured water level distance in cm bool floodAlert = false; // Boolean to check if flood alert is active
void setup() { // Initialize Serial for debugging Serial.begin(115200);
delay(2000); // Delay for 2 seconds before next reading }
// Function to get the water level distance using ultrasonic sensor float getUltrasonicDistance() { // Send a 10 microsecond pulse to TRIG_PIN to initiate measurement digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW);
// Read the duration of the echo pulse in microseconds long duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance in cm (speed of sound is 343 m/s or 0.0343 cm/us) float distance = duration * 0.0343 / 2;
return distance; // Return the measured distance }
// Function to activate the buzzer and LEDs when flood is detected void triggerAlert() { digitalWrite(BUZZER_PIN, HIGH); // Turn on the buzzer digitalWrite(LED_PIN, HIGH); // Turn on LED digitalWrite(LED1_PIN, HIGH); // Turn on second LED sendAlertSMS(); // Send an SMS alert }
// Function to reset the buzzer and LEDs when no flood is detected void resetAlert() { digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer digitalWrite(LED_PIN, LOW); // Turn off LED digitalWrite(LED1_PIN, LOW); // Turn off second LED }
// Function to send SMS alert using GSM module void sendAlertSMS() { gsmSerial.print("AT+CMGF=1\r"); // Set SMS mode to text delay(100); gsmSerial.print("AT+CMGS=\"+1234567890\"\r"); // Replace with your phone number delay(100); gsmSerial.print("Flood alert! Water level is critically high.\r"); // Alert message delay(100); gsmSerial.write(26); // Send the message (ASCII code for CTRL+Z) delay(1000); }
Tech Zaid
FLOOD Monitoring system code below
#include "thingProperties.h"
#include <SoftwareSerial.h>
// Define the pins used in the system
#define TRIG_PIN 12 // Ultrasonic sensor trigger pin
#define ECHO_PIN 13 // Ultrasonic sensor echo pin
#define BUZZER_PIN 5 // Buzzer pin
#define LED_PIN 17 // LED pin
#define LED1_PIN 18 // Second LED pin
// GSM Module Serial Communication
#define RX_PIN 22 // RX pin for GSM
#define TX_PIN 23 // TX pin for GSM
SoftwareSerial gsmSerial(RX_PIN, TX_PIN);
// Variables to hold sensor readings and IoT cloud properties
float waterLevel = 0; // Measured water level distance in cm
bool floodAlert = false; // Boolean to check if flood alert is active
void setup() {
// Initialize Serial for debugging
Serial.begin(115200);
// Initialize GSM module
gsmSerial.begin(9600);
Serial.println("GSM module initialized.");
// Initialize pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(LED1_PIN, OUTPUT);
// Initialize IoT Cloud properties
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
// Initialize IoT Cloud Debug
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
Serial.println("System initialized.");
}
void loop() {
// Update Arduino IoT Cloud
ArduinoCloud.update();
// Measure distance using the ultrasonic sensor
waterLevel = getUltrasonicDistance();
// Set flood alert if water level is below a certain distance (e.g., < 10 cm)
if (waterLevel > 0 && waterLevel < 10) {
floodAlert = true;
triggerAlert();
} else {
floodAlert = false;
resetAlert();
}
// Send data to IoT Cloud
waterLevelDistance = waterLevel; // Update cloud property for water level
floodDetected = floodAlert; // Update cloud property for flood alert
Serial.print("Water Level (cm): ");
Serial.println(waterLevel);
delay(2000); // Delay for 2 seconds before next reading
}
// Function to get the water level distance using ultrasonic sensor
float getUltrasonicDistance() {
// Send a 10 microsecond pulse to TRIG_PIN to initiate measurement
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the duration of the echo pulse in microseconds
long duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance in cm (speed of sound is 343 m/s or 0.0343 cm/us)
float distance = duration * 0.0343 / 2;
return distance; // Return the measured distance
}
// Function to activate the buzzer and LEDs when flood is detected
void triggerAlert() {
digitalWrite(BUZZER_PIN, HIGH); // Turn on the buzzer
digitalWrite(LED_PIN, HIGH); // Turn on LED
digitalWrite(LED1_PIN, HIGH); // Turn on second LED
sendAlertSMS(); // Send an SMS alert
}
// Function to reset the buzzer and LEDs when no flood is detected
void resetAlert() {
digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
digitalWrite(LED_PIN, LOW); // Turn off LED
digitalWrite(LED1_PIN, LOW); // Turn off second LED
}
// Function to send SMS alert using GSM module
void sendAlertSMS() {
gsmSerial.print("AT+CMGF=1\r"); // Set SMS mode to text
delay(100);
gsmSerial.print("AT+CMGS=\"+1234567890\"\r"); // Replace with your phone number
delay(100);
gsmSerial.print("Flood alert! Water level is critically high.\r"); // Alert message
delay(100);
gsmSerial.write(26); // Send the message (ASCII code for CTRL+Z)
delay(1000);
}
1 year ago | [YT] | 4
View 1 reply
Tech Zaid
techzaiduu.blogspot.com/2024/08/automatic-cloth-pr…
1 year ago | [YT] | 2
View 3 replies