#define BLYNK_TEMPLATE_ID "TMPL3rTNloqmQ"
#define BLYNK_TEMPLATE_NAME "IOT PROJECT"
#define BLYNK_AUTH_TOKEN "jAeENxYkIqcUEIcG85MfX5zsvuS23olU" // Replace with your Blynk Auth Token
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Wi-Fi credentials
char ssid[] = "Wokwi-GUEST"; // Replace with your Wi-Fi SSID
char pass[] = ""; // Replace with your Wi-Fi password
// Pin Definitions
#define TRIG_PIN 5 // GPIO5 (D5) for Ultrasonic Trig
#define ECHO_PIN 18 // GPIO18 (D18) for Ultrasonic Echo
#define GREEN_LED_PIN 2 // GPIO2 for Green LED (safe level)
#define RED_LED_PIN 15 // GPIO15 for Red LED (nearly full)
#define BUZZER_PIN 4 // GPIO4 for Buzzer (audible alert)
// Initialize LCD (I2C address 0x27, 16 chars, 2 lines)
LiquidCrystal_I2C lcd(0x27, 16, 2);
long duration;
int distance;
void setup() {
// Initialize hardware components
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
Serial.begin(115200); // For debugging
// Initialize Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}
void loop() {
// Send a 10us pulse to start measurement
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the pulse duration
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance in cm
distance = duration * 0.034 / 2;
// Print distance on Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Display the distance on the LCD
lcd.setCursor(0, 0);
lcd.print("Waste Level:");
lcd.setCursor(0, 1);
lcd.print(distance);
lcd.print(" cm");
delay(500); // Brief delay before clearing the LCD
lcd.clear(); // Clear LCD for the next update
// Update Blynk Gauge Widget with waste level
Blynk.virtualWrite(V1, distance);
// Check waste level and control LEDs, Buzzer, and Blynk widgets
if (distance < 10) {
digitalWrite(RED_LED_PIN, HIGH); // Turn on Red LED
digitalWrite(GREEN_LED_PIN, LOW); // Turn off Green LED
digitalWrite(BUZZER_PIN, HIGH); // Turn on Buzzer
delay(500);
digitalWrite(BUZZER_PIN, LOW); // Turn off Buzzer
delay(500);
Blynk.virtualWrite(V3, 255); // Turn on Red LED in Blynk
Blynk.virtualWrite(V2, 0); // Turn off Green LED in Blynk
// Send a notification via Blynk
Blynk.logEvent("Warning", "Warning: Waste level is critically low!");
} else {
digitalWrite(RED_LED_PIN, LOW); // Turn off Red LED
digitalWrite(GREEN_LED_PIN, HIGH); // Turn on Green LED
digitalWrite(BUZZER_PIN, LOW); // Turn off Buzzer
Blynk.virtualWrite(V3, 0); // Turn off Red LED in Blynk
Blynk.virtualWrite(V2, 255); // Turn on Green LED in Blynk
}
Blynk.run(); // Run Blynk to process incoming commands
delay(1000); // Wait 1 second before next measurement
}