#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL3PfaFYqc8"
#define BLYNK_TEMPLATE_NAME "IOT PROJECT"
#define BLYNK_AUTH_TOKEN "LSk4tM_RmLWlZ_ENInskBIhNsUF4hwLt"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// sda 21
// scl 22
#define dePin 4
#define dtPin 16
#define lePin 5
#define ltPin 17
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define depth 200
BlynkTimer timer;
long duration_d;
int distance_d = 200;
double percent;
long remaining;
long duration_l;
long distance_l;
Servo myservo;
int lidValue;
BLYNK_WRITE (V0){
}
BLYNK_WRITE (V1){
lidValue = param.asInt();
}
void setup()
{
pinMode(dtPin,OUTPUT);
pinMode(dePin, INPUT);
pinMode(ltPin,OUTPUT);
pinMode(lePin, INPUT);
lcd.init();
lcd.backlight();
myservo.attach(19);
pinMode(26, OUTPUT);
pinMode(25, OUTPUT);
Serial.begin(9600);
myservo.write(0);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000, sendSensorData);
}
void loop()
{
Blynk.run();
lcd.setCursor(0, 0);
digitalWrite(dtPin, LOW);
delayMicroseconds(2);
digitalWrite(dtPin,HIGH);
delayMicroseconds(10);
digitalWrite(dtPin,LOW);
// sent pulse
duration_d = pulseIn(dePin, HIGH);
// pulseIn() waits for the pin to go from LOW to HIGH
distance_d = duration_d * 0.0344 / 2;
if(distance_d<40)
{
digitalWrite(26, HIGH);
digitalWrite(25, HIGH);
// tone(25, 500);
delay(500);
digitalWrite(26, LOW);
digitalWrite(25, LOW);
// noTone(25);
delay(500);
}
else
{
// noTone(25);
digitalWrite(25, LOW);
}
remaining = 400-distance_d;
percent = ((double)remaining/400)*100;
lcd.print(percent);
digitalWrite(ltPin, LOW);
delayMicroseconds(2);
digitalWrite(ltPin,HIGH);
delayMicroseconds(10);
digitalWrite(ltPin,LOW);
// sent pulse
duration_l = pulseIn(lePin, HIGH); // time it takes for sound to reflet back
distance_l = duration_l * 0.0344 / 2;
if(distance_l<20)
{
myservo.write(90);
}
else
{
if(lidValue == 1){
myservo.write(90);
}
else{
myservo.write(0);
}
}
timer.run();
// digitalWrite(12, LOW);
}
void sendSensorData() {
Blynk.virtualWrite(V0, percent); // Send percent data to V0
if(percent>90){
Blynk.logEvent("trash_bin_completely_filled", String("Trash Bin Completely Filled") + percent);
}
}