#include <WiFi.h>
#include "DHT.h"
// DHT Sensor
#define DHTPIN 27 // GPIO pin where the DHT22 is connected
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// Ultrasonic Sensor
#define trigPin 26
#define echoPin 25
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// put your setup code here, to run once:
dht.begin();
pinMode(33, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(115200); // Initialize serial communication for debugging
Serial.println("Pendeteksi Banjir / Flood Detection");
delay(10);
}
void loop() {
// put your main code here, to run repeatedly:
float humidity = dht.readHumidity();
long duration, distance;
float waterLevel;
// Clear the trigger pin and wait for a short delay
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigger pin to high for 10 microseconds to transmit the ultrasonic pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo pin to measure the duration of the pulse and calculate the distance
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.0343) / 2; // Calculate the distance in cm (speed of sound is 0.0343 cm/microsecond)
// Assuming height from the ground is 1 meter, calculate the water level in percentage
waterLevel = 100 - (distance * 100 / 200);
if(humidity == 100 && waterLevel >= 60) {
digitalWrite(33, HIGH);
Serial.println("!! HIGH FlOOD DETECTED !!");
Serial.print("Humidity Sent is:");
Serial.print(humidity);
Serial.println(" %");
}
else if(humidity == 100 && waterLevel < 60) {
digitalWrite(33, HIGH);
Serial.println("! LOW FLOOD DETECTED !");
Serial.print("Humidity Sent is:");
Serial.print(humidity);
Serial.println(" %");
}
else if(humidity < 100) {
Serial.print("No Flood Detected, Safe ");
Serial.print("Humidity Sent is:");
Serial.print(humidity);
Serial.println(" %");
digitalWrite(33, LOW);
}
delay(50); // this speeds up the simulationdelay(10); // this speeds up the simulation
}