#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HX711.h> // Library for the Weight Sensor
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_NAME "Smart waste management system"
#define BLYNK_AUTH_TOKEN "AhOEf-V_OSuyVYy1ZelN4733m88RvGk9"
// WiFi credentials
char ssid[] = "Wokwi-mk mani";
char pass[] = "";
// Blynk authentication token
char auth[] = "AhOEf-V_OSuyVYy1ZelN4733m88RvGk9";
// Pin definitions
#define TRIGGER_PIN_1 32
#define ECHO_PIN_1 33
#define TRIGGER_PIN_2 25
#define ECHO_PIN_2 26
#define TRIGGER_PIN_3 27
#define ECHO_PIN_3 14
#define TRIGGER_PIN_4 12
#define ECHO_PIN_4 13
#define TRIGGER_PIN_5 15
#define ECHO_PIN_5 2
#define BUZZER_PIN 4
#define WEIGHT_SENSOR_DOUT 17
#define WEIGHT_SENSOR_SCK 16
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Initialize OLED display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// HX711 initialization
HX711 scale;
// Define constants for calculation
#define MAX_DISTANCE 200 // Maximum distance for ultrasonic sensor in cm
#define TRIGGER_INTERVAL 1000 // Trigger interval in milliseconds
#define BUZZER_THRESHOLD 80 // Threshold percentage for buzzer activation
// Blynk Virtual Pins
#define VIRTUAL_PIN_DISTANCE_1 V0
#define VIRTUAL_PIN_DISTANCE_2 V1
#define VIRTUAL_PIN_DISTANCE_3 V2
#define VIRTUAL_PIN_DISTANCE_4 V3
#define VIRTUAL_PIN_DISTANCE_5 V4
#define VIRTUAL_PIN_PERCENTAGE V5
// Global variables
long distance_1, distance_2, distance_3, distance_4, distance_5;
float weight;
bool buzzerActive = false;
// Ultrasonic sensor initialization
void setupUltrasonic() {
pinMode(TRIGGER_PIN_1, OUTPUT);
pinMode(ECHO_PIN_1, INPUT);
pinMode(TRIGGER_PIN_2, OUTPUT);
pinMode(ECHO_PIN_2, INPUT);
pinMode(TRIGGER_PIN_3, OUTPUT);
pinMode(ECHO_PIN_3, INPUT);
pinMode(TRIGGER_PIN_4, OUTPUT);
pinMode(ECHO_PIN_4, INPUT);
pinMode(TRIGGER_PIN_5, OUTPUT);
pinMode(ECHO_PIN_5, INPUT);
}
// Weight sensor initialization
void setupWeightSensor() {
scale.begin(WEIGHT_SENSOR_DOUT, WEIGHT_SENSOR_SCK);
scale.set_scale(1.f); // Set the calibration factor
scale.tare(); // Reset the scale to 0
}
// Buzzer initialization
void setupBuzzer() {
pinMode(BUZZER_PIN, OUTPUT);
}
// OLED display initialization
void setupOLED() {
if(!display.begin(SSD1306_I2C_ADDRESS, OLED_RESET)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.display();
}
// Setup function
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
setupUltrasonic();
setupWeightSensor();
setupBuzzer();
setupOLED();
}
// Function to read distance from ultrasonic sensor
long readDistance(int triggerPin, int echoPin) {
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
return pulseIn(echoPin, HIGH) / 58; // Convert pulse duration to distance in cm
}
// Function to read weight from the load cell
float readWeight() {
return scale.get_units(); // Read weight in kg
}
// Function to calculate percentage filled
float calculatePercentage(float currentWeight, float maxWeight) {
return (currentWeight / maxWeight) * 100.0;
}
// Function to activate buzzer if percentage exceeds threshold
void checkBuzzer(float percentage) {
if (percentage >= BUZZER_THRESHOLD && !buzzerActive) {
digitalWrite(BUZZER_PIN, HIGH);
buzzerActive = true;
} else if (percentage < BUZZER_THRESHOLD && buzzerActive) {
digitalWrite(BUZZER_PIN, LOW);
buzzerActive = false;
}
}
// Function to send data to Blynk
void sendSensorData() {
distance_1 = readDistance(TRIGGER_PIN_1, ECHO_PIN_1);
distance_2 = readDistance(TRIGGER_PIN_2, ECHO_PIN_2);
distance_3 = readDistance(TRIGGER_PIN_3, ECHO_PIN_3);
distance_4 = readDistance(TRIGGER_PIN_4, ECHO_PIN_4);
distance_5 = readDistance(TRIGGER_PIN_5, ECHO_PIN_5);
weight = readWeight();
float percentage = calculatePercentage(weight, 5.0); // Max weight assumption: 5 kg
Blynk.virtualWrite(VIRTUAL_PIN_DISTANCE_1, distance_1);
Blynk.virtualWrite(VIRTUAL_PIN_DISTANCE_2, distance_2);
Blynk.virtualWrite(VIRTUAL_PIN_DISTANCE_3, distance_3);
Blynk.virtualWrite(VIRTUAL_PIN_DISTANCE_4, distance_4);
Blynk