ESP32-Based Disaster Management System with Wi-Fi Connectivity and Sensor Integration
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to monitor environmental conditions such as temperature, humidity, water level, rainfall, and seismic activity. It uses an ESP32 microcontroller to collect data from various sensors and send notifications via Telegram. The circuit also fetches weather data from an API and displays the information on a web server.
Component List
ESP32
- Description: A powerful microcontroller with built-in WiFi and Bluetooth capabilities.
- Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, VIN, 3V3, D15, D2, D4, RX2, TX2, D5, D18, D19, D21, RX0, TX0, D22, D23, BOOT
KY-015 DHT11 Temperature-Humidity Sensor Module
- Description: A sensor module used to measure temperature and humidity.
- Pins: VCC, GND, DOUT
Water Sensor
- Description: A sensor used to detect the presence of water.
- Pins: Signal, VCC, GND
RAIN SENSOR
- Description: A sensor used to detect rainfall.
- Pins: AO, DO, GRD, VCC
Battery
- Description: A power source for the circuit.
- Pins: -, +
SW-420 Vibration Sensor
- Description: A sensor used to detect vibrations.
- Pins: vcc, Ground, Digital output
Wiring Details
ESP32
- VIN: Connected to the positive terminal of the battery.
- GND: Connected to the negative terminal of the battery.
- D35: Connected to the Signal pin of the Water Sensor.
- D32: Connected to the Digital output pin of the SW-420 Vibration Sensor.
- D4: Connected to the DOUT pin of the KY-015 DHT11 Temperature-Humidity Sensor Module.
- D5: Connected to the DO pin of the RAIN SENSOR.
KY-015 DHT11 Temperature-Humidity Sensor Module
- VCC: Connected to the positive terminal of the battery.
- GND: Connected to the negative terminal of the battery.
- DOUT: Connected to the D4 pin of the ESP32.
Water Sensor
- VCC: Connected to the positive terminal of the battery.
- GND: Connected to the negative terminal of the battery.
- Signal: Connected to the D35 pin of the ESP32.
RAIN SENSOR
- VCC: Connected to the positive terminal of the battery.
- GRD: Connected to the negative terminal of the battery.
- DO: Connected to the D5 pin of the ESP32.
Battery
- +: Connected to the VIN pin of the ESP32, VCC pin of the KY-015 DHT11 Temperature-Humidity Sensor Module, VCC pin of the Water Sensor, VCC pin of the RAIN SENSOR, and vcc pin of the SW-420 Vibration Sensor.
- -: Connected to the GND pin of the ESP32, GND pin of the KY-015 DHT11 Temperature-Humidity Sensor Module, GND pin of the Water Sensor, GRD pin of the RAIN SENSOR, and Ground pin of the SW-420 Vibration Sensor.
SW-420 Vibration Sensor
- vcc: Connected to the positive terminal of the battery.
- Ground: Connected to the negative terminal of the battery.
- Digital output: Connected to the D32 pin of the ESP32.
Documented Code
#include <WiFi.h>
#include <WebServer.h>
#include <DHT.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <UniversalTelegramBot.h>
#include <WiFiClientSecure.h>
// Pin definitions
#define DHTPIN 4
#define DHTTYPE DHT11
#define WATERLEVEL_PIN 35
#define RAINFALL_PIN 5
#define SEISMIC_PIN 32
// Setup DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// WiFi credentials
const char* ssid = "ne";
const char* password = "qwer4321";
// Telegram credentials
const char* botToken = "7898731428:AAE6uWu7NHZq69ta7VptOZG3Ap-ETQQsjx0";
const char* chatID = "6365554831";
// Weather API credentials
const char* city = "mangalore";
const char* apiKey = "ffc79bc9d96b20005a62a24e1f39113a";
String weatherCondition = "";
float temperatureAPI = 0.0;
float humidityAPI = 0.0;
float dewPointAPI = 0.0;
float windSpeedAPI = 0.0;
int cloudinessAPI = 0;
int visibilityAPI = 0;
int rainForecastInHours = 0; // Forecast time for rain in hours
// Create Telegram bot instance
WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);
// Web server instance
WebServer server(80);
// Notification control
unsigned long lastNotificationTime = 0;
const unsigned long notificationInterval = 60000;
// Threshold values (calculated from dataset)
const float TEMP_THRESHOLD = 30.0;
const float HUMIDITY_THRESHOLD = 80.0;
const float WATERLEVEL_THRESHOLD = 500.0;
const int RAINFALL_THRESHOLD = 1;
const float SEISMIC_THRESHOLD = 1000.0;
// Function to predict flood and earthquake
String predictFlood(float temp, float humidity, float waterLevel, int rainfall) {
if (waterLevel > WATERLEVEL_THRESHOLD && humidity > HUMIDITY_THRESHOLD && temp > TEMP_THRESHOLD && rainfall > RAINFALL_THRESHOLD) {
return "Flood: Yes";
}
return "Flood: No";
}
String predictEarthquake(float seismic) {
if (seismic > SEISMIC_THRESHOLD) {
return "Earthquake: Yes";
}
return "Earthquake: No";
}
String predictFloodTime(int forecastRainInHours) {
if (forecastRainInHours > 0) {
return String(forecastRainInHours) + " hours";
}
return "Not predicted";
}
// Function to fetch weather data from API
void fetchWeatherData() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String weatherApiUrl = "http://api.openweathermap.org/data/2.5/weather?q=" + String(city) + "&appid=" + String(apiKey) + "&units=metric";
http.begin(weatherApiUrl);
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
String payload = http.getString();
StaticJsonDocument<1024> doc;
deserializeJson(doc, payload);
// Extracting weather data
const char* condition = doc["weather"][0]["main"];
weatherCondition = String(condition);
temperatureAPI = doc["main"]["temp"];
humidityAPI = doc["main"]["humidity"];
dewPointAPI = doc["dew_point"];
windSpeedAPI = doc["wind"]["speed"];
cloudinessAPI = doc["clouds"]["all"];
visibilityAPI = doc["visibility"];
rainForecastInHours = doc["rain"]["1h"] ? 1 : 0; // Estimate forecast for rain (simplified)
Serial.println("Weather Condition: " + weatherCondition);
Serial.println("Temperature: " + String(temperatureAPI));
Serial.println("Humidity: " + String(humidityAPI));
Serial.println("Dew Point: " + String(dewPointAPI));
Serial.println("Wind Speed: " + String(windSpeedAPI));
Serial.println("Cloudiness: " + String(cloudinessAPI));
Serial.println("Visibility: " + String(visibilityAPI));
Serial.println("Rain Forecast in hours: " + String(rainForecastInHours));
} else {
Serial.println("Error fetching weather data: " + String(httpResponseCode));
}
http.end();
} else {
Serial.println("WiFi not connected. Cannot fetch weather data.");
}
}
// Function to send Telegram notifications
void sendTelegramNotification(String message) {
if (millis() - lastNotificationTime > notificationInterval) {
if (WiFi.status() == WL_CONNECTED) {
bot.sendMessage(chatID, message, "");
lastNotificationTime = millis();
} else {
Serial.println("WiFi not connected. Cannot send Telegram message.");
}
} else {
Serial.println("Notification suppressed to avoid spamming.");
}
}
// Function to handle root web server request
void handleRoot() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
float waterLevel = analogRead(WATERLEVEL_PIN);
int rainfall = digitalRead(RAINFALL_PIN);
float seismic = analogRead(SEISMIC_PIN);
Serial.println("Sensor Data:");
Serial.println("Temperature: " + String(temperature) + " °C");
Serial