#define BLYNK_TEMPLATE_ID "TMPL3zRinEfJm"
#define BLYNK_TEMPLATE_NAME "Smart Dustbin"
#define BLYNK_AUTH_TOKEN "yAn71d0BnEEAVHF0t8WC3pEszAHfj_7o"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Set your WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Define ultrasonic sensor pins
#define trigPin 4
#define echoPin 2
// Define thresholds for water levels in percentage and corresponding distances
#define FULL_LEVEL_PERCENTAGE 10
#define EMPTY_LEVEL_PERCENTAGE 80
//#define MOTOR_START_PERCENTAGE 5
// Maximum measurable distance by the ultrasonic sensor in cm
#define MAX_DISTANCE 400
// Initialize Blynk
BlynkTimer timer;
void setup() {
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Timer to check water level periodically
timer.setInterval(10000L, checkWaterLevel); // Check every 10 seconds
}
void loop() {
Blynk.run();
timer.run();
}
void checkWaterLevel() {
long duration, distance;
// Clear the trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send a 10 microsecond pulse to trigger pin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the duration of pulse on echo pin
duration = pulseIn(echoPin, HIGH);
// Calculate distance in cm
distance = duration * 0.034 / 2;
// Calculate thresholds in cm
int emptyLevelDistance = MAX_DISTANCE * EMPTY_LEVEL_PERCENTAGE / 100;
int fullLevelDistance = MAX_DISTANCE * FULL_LEVEL_PERCENTAGE / 100;
// int motorStartDistance = MAX_DISTANCE * MOTOR_START_PERCENTAGE / 100;
// Debug output
//Serial.print("Distance: ");
//Serial.println(distance);
// Check water level and send appropriate message
if (distance >= emptyLevelDistance) {
Serial.println("Water Level --> Going to Empty");
Blynk.virtualWrite(V0, "Water Level --> Going to Empty");
Serial.println("Please --> Turn On the Motor");
Blynk.virtualWrite(V0, "Please --> Turn On the Motor");
} else if (distance < fullLevelDistance) {
Serial.println("Water Tank Full");
Blynk.virtualWrite(V0, "Water Tank Full");
}
}