#define BLYNK_TEMPLATE_ID "TMPL3D3quby4V"
#define BLYNK_TEMPLATE_NAME "Smart Campus Waste Management"
#define BLYNK_AUTH_TOKEN "WZOSWg2ist6p7z-u0KNiX5-ofQDWM00n"
#define TRIGGER_PIN_1 12 // GPIO pin connected to the trigger pin of the first ultrasonic sensor
#define ECHO_PIN_1 14 // GPIO pin connected to the echo pin of the first ultrasonic sensor
#define TRIGGER_PIN_2 16 // GPIO pin connected to the trigger pin of the second ultrasonic sensor
#define ECHO_PIN_2 17 // GPIO pin connected to the echo pin of the second ultrasonic sensor
#define BIN_1_RED_LED 2 // GPIO pin connected to the first BIN1 RED LED
#define BIN_1_GREEN_LED 0 // GPIO pin connected to the first BIN1 GREEN LED
#define BIN_2_RED_LED 4 // GPIO pin connected to the second BIN2 RED LED
#define BIN_2_GREEN_LED 5 // GPIO pin connected to the BIN2 GREEN LED
#define BUZZER_PIN_1 32 // GPIO pin connected to the first buzzer
#define BUZZER_PIN_2 33 // GPIO pin connected to the second buzzer
#include <WiFi.h>
#include <Ultrasonic.h>
#include <BlynkSimpleEsp32.h>
Ultrasonic ultrasonic1(TRIGGER_PIN_1, ECHO_PIN_1);
Ultrasonic ultrasonic2(TRIGGER_PIN_2, ECHO_PIN_2);
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
char auth[] = "WZOSWg2ist6p7z-u0KNiX5-ofQDWM00n";
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(BIN_1_RED_LED, OUTPUT); // Set BIN1 RED LED pin as an output
pinMode(BIN_1_GREEN_LED, OUTPUT); // Set BIN1 GREEN LED pin as an output
pinMode(BIN_2_RED_LED, OUTPUT); // Set BIN2 RED LED pin as an output
pinMode(BIN_2_GREEN_LED, OUTPUT); // Set BIN2 GREEN LED pin as an output
pinMode(BUZZER_PIN_1, OUTPUT); // Set buzzer 1 pin as an output
pinMode(BUZZER_PIN_2, OUTPUT); // Set buzzer 2 pin as an output
Blynk.begin(auth, ssid, pass);
// Ensure the device connects to WiFi before proceeding
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
}
void loop() {
Blynk.run();
long distance1 = measureDistance(TRIGGER_PIN_1, ECHO_PIN_1);
long distance2 = measureDistance(TRIGGER_PIN_2, ECHO_PIN_2);
long a = 399 - distance1;
long b = 399 - distance2; // Read distance from the second ultrasonic sensor
Serial.print("Bin1 fill level: ");
Serial.print(a);
Serial.print(" cm | Bin2 fill level: ");
Serial.print(b);
Serial.println(" cm");
Blynk.virtualWrite(V1, a); // Send distance 1 to Blynk app
Blynk.virtualWrite(V2, b); // Send distance 2 to Blynk app
// Control physical and Blynk LED 1 and buzzer 1 based on distance from sensor 1
if (distance1 < 50) {
Serial.println("Bin1 is full!!. Don't put waste on it!!");
digitalWrite(BIN_1_RED_LED, HIGH); // Turn on BIN1 RED LED
digitalWrite(BIN_1_GREEN_LED, LOW); // Turn off BIN1 GREEN LED
digitalWrite(BUZZER_PIN_1, HIGH); // Turn on buzzer 1
tone(BUZZER_PIN_1, 100); // Play tone on buzzer 1
Blynk.virtualWrite(V3, 255); // Turn on Blynk RED LED for Bin 1
Blynk.virtualWrite(V4, 0); // Turn off Blynk GREEN LED for Bin 1
} else {
digitalWrite(BIN_1_RED_LED, LOW); // Turn off BIN1 RED LED
digitalWrite(BIN_1_GREEN_LED, HIGH); // Turn on BIN1 GREEN LED
digitalWrite(BUZZER_PIN_1, LOW); // Turn off buzzer 1
Blynk.virtualWrite(V3, 0); // Turn off Blynk RED LED for Bin 1
Blynk.virtualWrite(V4, 255); // Turn on Blynk GREEN LED for Bin 1
}
// Control physical and Blynk LED 2 and buzzer 2 based on distance from sensor 2
if (distance2 < 50) {
Serial.println("Bin2 is full!!. Don't put waste on it!!");
digitalWrite(BIN_2_RED_LED, HIGH); // Turn on BIN2 RED LED
digitalWrite(BIN_2_GREEN_LED, LOW); // Turn off BIN2 GREEN LED
digitalWrite(BUZZER_PIN_2, HIGH); // Turn on buzzer 2
tone(BUZZER_PIN_2, 100); // Play tone on buzzer 2
Blynk.virtualWrite(V5, 255); // Turn on Blynk RED LED for Bin 2
Blynk.virtualWrite(V6, 0); // Turn off Blynk GREEN LED for Bin 2
} else {
digitalWrite(BIN_2_RED_LED, LOW); // Turn off BIN2 RED LED
digitalWrite(BIN_2_GREEN_LED, HIGH); // Turn on BIN2 GREEN LED
digitalWrite(BUZZER_PIN_2, LOW); // Turn off buzzer 2
Blynk.virtualWrite(V5, 0); // Turn off Blynk RED LED for Bin 2
Blynk.virtualWrite(V6, 255); // Turn on Blynk GREEN LED for Bin 2
}
delay(1000); // Delay for stability
}
long measureDistance(int tp, int ep) {
digitalWrite(tp, LOW);
delay(2);
digitalWrite(tp, HIGH);
delay(10);
digitalWrite(tp, LOW);
long duration = pulseIn(ep, HIGH);
long distance = duration * 0.034 / 2;
return distance;
}