#include <SPI.h>
#include <MFRC522.h>
#include <ESP32Servo.h>
#include <HCSR04.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin Definitions
#define RFID_SDA 5
#define RFID_RST 27
#define SERVO_PIN 2
#define TRIG_PIN 26
#define ECHO_PIN 25
#define BUZZER_PIN 4
#define GLED 13
#define RLED 14
// Authorized RFID UID (Replace with your card's UID)
byte authorizedUID[4] = {0x12, 0x34, 0x56, 0x78};
// Component Initialization
MFRC522 mfrc522(RFID_SDA, RFID_RST);
Servo lidServo;
HCSR04 sonar(TRIG_PIN, ECHO_PIN); // Max distance 30cm
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27
// Global Variables
bool isBinFull = false;
unsigned long lidCloseTime = 0;
unsigned int level;
void setup() {
Serial.begin(9600);
SPI.begin();
Wire.begin();
mfrc522.PCD_Init();
lidServo.attach(SERVO_PIN);
lidServo.write(0);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RLED, OUTPUT);
pinMode(GLED, OUTPUT);
pinMode(33, INPUT_PULLUP); //for simulation only
digitalWrite(RLED,HIGH);
digitalWrite(GLED,LOW);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.print(" Smart Dustbin ");
delay(2000);
lcd.clear();
}
void loop() {
Serial.println("Enter rfid ID tag (format: XX XX XX XX):");
checkBinLevel();
homeDisplay();
//for simulation call simulationProcess() for real process call realProcess()
simulationProcess();
//realProcess();
/*// Auto-close lid after 5 seconds
if (millis() > lidCloseTime && lidServo.read() == 90) {
lidServo.write(0);
lcd.clear();
lcd.print(" Lid Closed ");
}*/
}
void simulationProcess()
{
if (Serial.available()) {
String input = Serial.readStringUntil('\n');
input.trim(); // Remove any leading or trailing whitespace
if (isValidFormat(input))
{
// Check if the entered ID matches the expected ID
if (input.equals("12 34 56 78") && !isBinFull) { // Replace with your registered e-KTP ID
grantAccess();
} else {
denyAccess();
}
} else {
Serial.println("Invalid format. Enter e-KTP ID (format: XX XX XX XX):");
}
}
}
void realProcess()
{
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
bool accessGranted = true;
// Verify UID
for (byte i = 0; i < 4; i++) {
if (mfrc522.uid.uidByte[i] != authorizedUID[i]) {
accessGranted = false;
break;
}
}
// Process access
if (accessGranted && !isBinFull) {
grantAccess();
} else {
denyAccess();
}
mfrc522.PICC_HaltA();
delay(500); // Debounce
}
}
void checkBinLevel() {
level = sonar.dist();
if (level < 10 && level > 0) { // Adjust threshold as needed
isBinFull = true;
digitalWrite(RLED,HIGH);
digitalWrite(GLED,LOW);
lcd.clear();
lcd.print(" BIN FULL! ");
lcd.setCursor(0, 1);
lcd.print(" Access Denied ");
digitalWrite(BUZZER_PIN, HIGH);
delay(1000);
digitalWrite(BUZZER_PIN, LOW);
} else {
isBinFull = false;
digitalWrite(RLED,LOW);
digitalWrite(GLED,HIGH);
}
}
void grantAccess() {
lidServo.write(90);
lidCloseTime = millis() + 5000;
lcd.clear();
lcd.print(" Access Granted ");
lcd.setCursor(0, 1);
lcd.print(" Bin Level: OK ");
delay(5000);
lidServo.write(0);
}
void denyAccess() {
lcd.clear();
lcd.print(isBinFull ? " Bin Full! " : " Access Denied ");
digitalWrite(BUZZER_PIN, HIGH);
delay(500);
digitalWrite(BUZZER_PIN, LOW);
}
bool isValidFormat(String input) {
// Check if the input matches the format "XX XX XX XX"
if (input.length() == 11 && input.charAt(2) == ' ' && input.charAt(5) == ' ' && input.charAt(8) == ' ') {
for (int i = 0; i < input.length(); i++) {
if (i != 2 && i != 5 && i != 8) {
if (!isDigit(input.charAt(i))) {
return false;
}
}
}
return true;
}
return false;
}
void homeDisplay()
{
lcd.setCursor(0,0);
lcd.print("-SMART DUSTBIN WITH-");
lcd.setCursor(0,1);
lcd.print("RF-ID ACCESS CONTROL");
lcd.setCursor(0,2);
lcd.print("PLEASE! SCAN A CARD");
lcd.setCursor(0,3);
lcd.print("WASTE LEVEL:");
lcd.setCursor(12,3);
int levelX=map(level,0,400,0,100);
lcd.print(levelX);
lcd.print(" %");
delay(500);
}
SIMULATE
IOT BASED SMART DUSTBIN