#define BLYNK_TEMPLATE_ID "TMPL3UH4SyGpC"
#define BLYNK_TEMPLATE_NAME "Smart waste management"
#define BLYNK_AUTH_TOKEN "bPZ3UxDXszeh9_n_UY3-by-EusTM7Rxs"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
#include <HTTPClient.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
#define echoPin 32
#define trigPin 33
Servo servo;
long duration;
int distance;
int binLevel = 80;
const int threshold = 75; // Threshold for notification
void sendNotification(String message) {
HTTPClient http;
String serverPath = "http://blynk-cloud.com/" + String(auth) + "/notify";
http.begin(serverPath.c_str());
http.addHeader("Content-Type", "application/json");
String jsonPayload = "{\"body\":\"" + message + "\"}";
int httpResponseCode = http.POST(jsonPayload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
}
void ultrasonic() {
// Simulate different distances
distance = 10; // Example: Change this value to simulate different distances
binLevel = map(distance, 21, 0, 0, 100); // Adjust bin height here
Blynk.virtualWrite(V0, distance);
Blynk.virtualWrite(V1, binLevel);
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm, Bin Level: ");
Serial.print(binLevel);
Serial.println(" %");
if (binLevel >= threshold) {
sendNotification("Dustbin is full. Please empty it!");
}
// Control the servo based on bin level
if (binLevel >= threshold) {
servo.write(90); // Move the servo to 90 degrees
} else {
servo.write(0); // Move the servo back to 0 degrees
}
}
void setup() {
Serial.begin(9600); // Initialize serial communication
servo.attach(13);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Blynk.begin(auth, ssid, pass);
delay(2000);
timer.setInterval(1000L, ultrasonic);
}
void loop() {
Blynk.run();
timer.run();
}