#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6YggKQ5Qs"
#define BLYNK_TEMPLATE_NAME "Diploma project"
#define BLYNK_AUTH_TOKEN "7za_m3I5myaXoQHoFMMD5U7aWf_02POt"
#include <Arduino.h>
#include <LiquidCrystal.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <HTTPClient.h>
// Pin Definitions
const int ultrasonicTriggerPin = 12; // Trigger pin of ultrasonic sensor
const int ultrasonicEchoPin = 14; // Echo pin of ultrasonic sensor
const int potentiometerPin = 34; // Analog pin for potentiometer
const int ledRedPin = 25; // Red LED pin
const int buzzerPin = 13; // Buzzer pin
const int ledGreenPin = 26; // Green LED pin
const int rs = 19, en = 18, d4 = 15, d5 = 5, d6 = 4, d7 = 2; // LCD pins
// Variables
float waterLevel = 0; // Variable to store water level in centimeters
// LCD Object
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Blynk authentication token
char auth[] = "7za_m3I5myaXoQHoFMMD5U7aWf_02POt"; // Your Blynk authentication token
// WiFi credentials
char ssid[] = "Wokwi-GUEST"; // Your Wi-Fi SSID
char pass[] = ""; // Your Wi-Fi password
// Blynk timer object
BlynkTimer timer;
// Function declarations
void measureWaterLevel();
void sendNotification(const char* message);
void setup() {
Serial.begin(115200);
// Setup pins
pinMode(ultrasonicTriggerPin, OUTPUT);
pinMode(ultrasonicEchoPin, INPUT);
pinMode(potentiometerPin, INPUT);
pinMode(ledRedPin, OUTPUT);
pinMode(ledGreenPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Initialize LCD
lcd.begin(16, 2);
lcd.clear();
// Connect to WiFi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Setup a function to be called every second
timer.setInterval(1000L, measureWaterLevel);
}
void loop() {
Blynk.run();
timer.run();
int potValue = analogRead(potentiometerPin);
potValue = map(potValue, 0, 4095, 0, 100); // Map analog range to 0-100
Serial.print("Potentiometer Value: ");
Serial.println(potValue); // Print potentiometer value
if (waterLevel > 399) {
digitalWrite(ledRedPin, HIGH);
digitalWrite(ledGreenPin, LOW);
tone(buzzerPin, 1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Danger - Water Level");
sendNotification("Danger! Water level is critical.");
} else if (potValue > 50) {
digitalWrite(ledRedPin, HIGH);
digitalWrite(ledGreenPin, LOW);
tone(buzzerPin, 1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Danger - Potentiometer");
sendNotification("Danger! Potentiometer value is critical.");
} else {
digitalWrite(ledRedPin, LOW);
digitalWrite(ledGreenPin, HIGH);
noTone(buzzerPin);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Good");
}
Serial.print("Water Level: ");
Serial.print(waterLevel);
Serial.println(" cm");
// Send water level and potentiometer values to Blynk
Blynk.virtualWrite(V1, waterLevel); // Assuming V1 is for water level
Blynk.virtualWrite(V3, potValue); // Assuming V3 is for potentiometer value
delay(1000);
}
void measureWaterLevel() {
digitalWrite(ultrasonicTriggerPin, LOW);
delayMicroseconds(2);
digitalWrite(ultrasonicTriggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicTriggerPin, LOW);
float duration = pulseIn(ultrasonicEchoPin, HIGH);
waterLevel = duration * 0.034 / 2;
// Assuming you have some method to measure water flow and assign it to waterFlow variable
// waterFlow = someFunctionToMeasureWaterFlow();
}
void sendNotification(const char* message) {
// You can implement your notification mechanism here
// For example, you can use Blynk's email or notification widget
// Or implement your own notification service
}