#define BLYNK_TEMPLATE_ID "TMPL6pHGpAtVl"
#define BLYNK_TEMPLATE_NAME "RFID Based Door Lock System"
#define BLYNK_AUTH_TOKEN "hpgnPaPYhXK4dDt7AS_CyXHzxONuG1WM"

#include <ESP32Servo.h>
#include <LiquidCrystal.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

// LCD pins
LiquidCrystal lcd(19, 18, 17, 16, 15, 14);

// Define the servo pin
const int servoPin = 12;
Servo myServo;

// LED and Buzzer pins
const int RedLED = 5;
const int GreenLED = 4;
const int Buzzer = 21;

// Blynk authentication token
char auth[] = "hpgnPaPYhXK4dDt7AS_CyXHzxONuG1WM";

// Your WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";

// Variables for RFID reading
int count = 0;
char c;
String id;

// Delay time
int wait = 5000;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Initialize pins
  pinMode(RedLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);
  pinMode(Buzzer, OUTPUT);
  
  // Attach servo
  myServo.attach(servoPin);
  myServo.write(90); // Assume 0 is the locked position

  // Initialize LCD
  lcd.begin(20, 4);
  lcd.setCursor(0, 0);
  lcd.print("  THE BRIGHT LIGHT ");
  lcd.setCursor(0, 1);
  lcd.print(" RFID BASED LOCK SYS");
  lcd.setCursor(0, 2);
  lcd.print(" CARD:   ");
  lcd.setCursor(0, 3);
  lcd.print(" NAME:   ");

  // Connect to WiFi and Blynk
  Blynk.begin(auth, ssid, pass);

  Serial.println("Welcome to Your Sweet Home");
  Serial.println("Please scan your RFID Card");
}

void loop() {
  Blynk.run(); // Run Blynk

  while (Serial.available() > 0) {
    c = Serial.read();
    count++;
    id += c;
    if (count == 12) {
      Serial.print(id);
      if (id == "E280689401A9") {
        handleValidCard("ADWA");
      } else if (id == "E2000019060C") {
        handleValidCard("IPAN");
      } else if (id == "G46RD9V40F3A") {
        handleValidCard("BAIM");
      } else if (id == "B71001B76894") {
        handleValidCard("ALIF");
      } else if (id == "1C81159073FD") {
        handleValidCard("AREP");
      } else if (id == "B710D0186022") {
        handleValidCard("AZAM");
      } else {
        handleInvalidCard();
      }
    }
  }
  count = 0;
  id = "";
  delay(1000);
}

void handleValidCard(String name) {
  Serial.println("Valid Card");
  lcd.setCursor(0, 2);
  lcd.print(" CARD: VALID   ");
  lcd.setCursor(0, 3);
  lcd.print(" NAME: " + name + " ");
  digitalWrite(GreenLED, HIGH);
  myServo.write(0); // Assume 90 is the unlocked position

  // Log event to Blynk
  Blynk.logEvent("valid_card", "Valid Card: " + name);
  Blynk.virtualWrite(V0, "Valid Card: " + name); // Display on Blynk

  delay(wait);
  myServo.write(90); // Lock the door again
  digitalWrite(GreenLED, LOW);
  lcd.setCursor(0, 2);
  lcd.print(" CARD:           ");
  lcd.setCursor(0, 3);
  lcd.print(" NAME:           ");
}

void handleInvalidCard() {
  Serial.println("Invalid Card");
  lcd.setCursor(0, 2);
  lcd.print(" CARD: INVALID   ");
  lcd.setCursor(0, 3);
  lcd.print(" NAME: UNKNOWN     ");
  digitalWrite(RedLED, HIGH);
  digitalWrite(Buzzer, HIGH);

  // Log event to Blynk
  Blynk.logEvent("invalid_card", "Invalid Card");
  Blynk.virtualWrite(V0, "Invalid Card"); // Display on Blynk

  delay(wait);
  digitalWrite(RedLED, LOW);
  digitalWrite(Buzzer, LOW);
  lcd.setCursor(0, 2);
  lcd.print(" CARD:           ");
  lcd.setCursor(0, 3);
  lcd.print(" NAME:           ");
}
NOCOMNCVCCGNDINLED1PWRRelay Module