#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include "HX711.h"
// Blynk credentials
char auth[] = "vfcdAOxUfk5wwpQOhhIlPgNzf70Krjio";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Pins
#define TRIG 5
#define ECHO 18
#define MQ2_PIN 34
#define DOUT 19
#define CLK 21
#define METAL_PIN 23
HX711 scale;
void setup() {
Serial.begin(115200);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(METAL_PIN, INPUT_PULLUP);
scale.begin(DOUT, CLK);
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
// --- Fill Level (Ultrasonic) ---
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long duration = pulseIn(ECHO, HIGH);
float distance = duration * 0.034 / 2; // cm
int binHeight = 100; // assume 100cm bin
int fillLevel = map(distance, 0, binHeight, 100, 0);
Serial.print("Fill Level: ");
Serial.println(fillLevel);
Blynk.virtualWrite(V0, fillLevel);
// --- Gas Sensor ---
int gasValue = analogRead(MQ2_PIN);
Serial.print("Gas: ");
Serial.println(gasValue);
Blynk.virtualWrite(V1, gasValue);
// --- Load Cell ---
long weight = scale.get_units();
Serial.print("Weight: ");
Serial.println(weight);
Blynk.virtualWrite(V2, weight);
// --- Metal Detection ---
int metalDetected = digitalRead(METAL_PIN) == LOW;
Serial.print("Metal: ");
Serial.println(metalDetected);
if (metalDetected) {
Blynk.virtualWrite(V3, "Metal Detected");
Blynk.logEvent("metal_alert", "♻️ Metal waste detected");
} else {
Blynk.virtualWrite(V3, "No Metal");
}
// --- Alerts ---
if (fillLevel > 80) {
Blynk.logEvent("bin_full", "⚠️ Bin is nearly full");
}
if (gasValue > 500) {
Blynk.logEvent("gas_alert", "⚠️ Hazardous gas detected");
}
delay(2000);
}