#include <WiFi.h>
#include "ThingSpeak.h"
#include <LiquidCrystal_I2C.h>
#include <HTTPClient.h>
WiFiClient client;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns, and 2 rows
const int trigPin = 5; // GPIO pin for trigger
const int echoPin = 18; // GPIO pin for echo
const int alertPin = 13; // GPIO pin for alert, adjust as needed
const char *WIFI_NAME = "Wokwi-GUEST";
const char *WIFI_PASSWORD = "";
const int myChannelNumber = 2382984;
const char *myApiKey = "AOOXZR7V0A20FPBR";
const char *server = "api.thingspeak.com";
long distance;
int waterLevelPercentage;
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.begin(16, 2); // initialize the lcd
lcd.backlight(); // turn on the backlight
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(alertPin, OUTPUT);
connectToWiFi();
ThingSpeak.begin(client);
}
void loop()
{
long duration;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
waterLevelPercentage = map(constrain(distance, 2, 400), 2, 400, 100, 0);
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm | Water Level: ");
Serial.print(waterLevelPercentage);
Serial.println("%");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Water Level:");
lcd.setCursor(0, 1);
lcd.print(waterLevelPercentage);
lcd.print("%");
if (waterLevelPercentage >= 80)
{ // Trigger alert at 80% or higher
digitalWrite(alertPin, HIGH); // Activate alert
sendSMS("Flooded, Please check");
}
else
{
digitalWrite(alertPin, LOW); // Deactivate alert
}
sendToThingSpeak();
delay(10000); // Adjust delay as needed
}
void connectToWiFi()
{
Serial.println("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20)
{
delay(1000);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("\nWiFi connected!");
Serial.println("Local IP: " + WiFi.localIP().toString());
}
else
{
Serial.println("\nFailed to connect to WiFi. Check your credentials or restart the ESP32.");
while (1)
{
delay(5000);
ESP.restart();
}
}
}
void sendToThingSpeak()
{
ThingSpeak.setField(1, waterLevelPercentage);
ThingSpeak.setField(2, distance);
int status = ThingSpeak.writeFields(myChannelNumber, myApiKey);
if (status == 200)
{
Serial.println("Data sent to ThingSpeak successfully!");
}
else
{
Serial.println("Failed to send data to ThingSpeak. HTTP error code: " + String(status));
}
}
void sendSMS(const char *message)
{
HTTPClient http;
// Construct the Twilio API URL
String url = "https://api.twilio.com/2010-04-01/Accounts/ACb8b18f3ad122b4e3f18293089346b940/Messages.json";
// Set up HTTP request headers
http.begin(url);
http.setAuthorization("ACb8b18f3ad122b4e3f18293089346b940", "14fff219e33b799454f5a8c5d5de418e");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Construct the message body
String body = "To=+18777804236"; // Replace with your destination phone number
body += "&From=+12059906114"; // Replace with your Twilio phone number
body += "&Body=";
body += message;
// Send HTTP POST request
int httpCode = http.POST(body);
// Check the response
if (httpCode > 0)
{
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
String payload = http.getString();
Serial.println(payload);
}
else
{
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
// Close the connection
http.end();
}