#define BLYNK_TEMPLATE_ID "TMPL39osnfuvP"
#define BLYNK_TEMPLATE_NAME "smart waste management system"
#define BLYNK_AUTH_TOKEN "9u9wuG7yhjsaMbqtNh11CAcKcMrTV2NB"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#include <ESP32Servo.h>
BlynkTimer timer;
// Dust bin status level detection
#define echoPin 32
#define trigPin 33
// Person detection
#define PERSON_ECHO_PIN 4
#define PERSON_TRIG_PIN 19
// Servo object for the bin
Servo servo;
int pos = 20; // Initial position of the servo
// Calculate the distance in percentage
long duration;
int distance;
void checkDustbinStatus() {
// Maximum distance for the bin
int maxDistance = 400;
// Check the dustbin status
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
int percentage = (maxDistance - distance) * 100 / maxDistance;
Blynk.virtualWrite(V3, percentage);
Serial.print("Dust bin: ");
Serial.print(percentage);
if (percentage <= 80) {
Blynk.virtualWrite(V0, 0);
Serial.println(" %");
} else {
Blynk.virtualWrite(V0, 1);
Serial.println(" %, the dustbin is going to be full");
}
}
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(PERSON_TRIG_PIN, OUTPUT);
pinMode(PERSON_ECHO_PIN, INPUT);
servo.attach(23);
servo.write(pos);
Blynk.begin(auth, ssid, pass);
delay(2000);
timer.setInterval(1000L, checkDustbinStatus);
}
void controlServo(int trigPin, int echoPin, Servo &servo) {
long duration, distance;
// Send a pulse to the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo and calculate the distance
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Person Distance from the bin: ");
Serial.print(distance);
Serial.println(" cm");
// Control the servo based on the person distance
if (distance <= 30) {
servo.write(pos + 160);
} else {
servo.write(pos);
}
}
void loop() {
controlServo(PERSON_TRIG_PIN, PERSON_ECHO_PIN, servo);
delay(1000);
Blynk.run();
timer.run();
}