#include <Arduino.h>  // Include the Arduino library for standard functions

// Define GPIO pins for various components
#define BUZZER_PIN   14  // GPIO pin for the buzzer
#define GREEN_LED    26  // GPIO pin for the green LED (access granted indicator)
#define RED_LED      25  // GPIO pin for the red LED (access denied indicator)
#define RELAY_PIN    27  // GPIO pin for controlling the relay (door mechanism)

void setup() {
  Serial.begin(115200); // Initialize serial communication at 115200 baud rate
  
  // Set the defined GPIO pins as output
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(RELAY_PIN, OUTPUT);

  // Set initial state: turn off buzzer, LEDs, and relay
  digitalWrite(BUZZER_PIN, LOW);
  digitalWrite(GREEN_LED, LOW);
  digitalWrite(RED_LED, LOW);
  digitalWrite(RELAY_PIN, LOW);

  // Prompt user to enter an e-KTP ID
  Serial.println("Enter e-KTP ID (format: XX XX XX XX):");
}

void loop() {
  // Check if there is any incoming serial data
  if (Serial.available()) {
    String input = Serial.readStringUntil('\n'); // Read the input until a newline character is received
    input.trim(); // Remove leading and trailing whitespace

    // Validate the format of the entered ID
    if (isValidFormat(input)) {
      // Check if the entered ID matches the registered ID
      if (input.equals("12 34 56 78")) { // Replace with the correct registered e-KTP ID
        accessGranted(); // Call function to grant access
      } else {
        accessDenied(); // Call function to deny access
      }
    } else {
      // Print an error message if the input format is incorrect
      Serial.println("Invalid format. Enter e-KTP ID (format: XX XX XX XX):");
    }
  }
}

// Function to validate if input follows the "XX XX XX XX" format
bool isValidFormat(String input) {
  // Check if the length matches the expected format (11 characters including spaces)
  if (input.length() == 11 && input.charAt(2) == ' ' && input.charAt(5) == ' ' && input.charAt(8) == ' ') {
    // Loop through each character to ensure valid digits in the correct places
    for (int i = 0; i < input.length(); i++) {
      if (i != 2 && i != 5 && i != 8) { // Ignore spaces at positions 2, 5, and 8
        if (!isDigit(input.charAt(i))) { // Check if character is a digit
          return false; // Return false if a non-digit is found
        }
      }
    }
    return true; // Return true if the format is valid
  }
  return false; // Return false if length or spacing is incorrect
}

// Function to handle access granted scenario
void accessGranted() {
  Serial.println("Access granted!"); // Print access granted message
  
  // Activate buzzer, green LED, and relay (door unlocking mechanism)
  digitalWrite(BUZZER_PIN, HIGH);
  digitalWrite(GREEN_LED, HIGH);
  digitalWrite(RELAY_PIN, HIGH);
  
  delay(5000); // Keep buzzer and LED on for 5 seconds
  
  // Turn off buzzer, green LED, and relay after delay
  digitalWrite(BUZZER_PIN, LOW);
  digitalWrite(GREEN_LED, LOW);
  digitalWrite(RELAY_PIN, LOW);
}

// Function to handle access denied scenario
void accessDenied() {
  Serial.println("Access denied!"); // Print access denied message
  
  // Activate buzzer and red LED to indicate denial
  digitalWrite(BUZZER_PIN, HIGH);
  digitalWrite(RED_LED, HIGH);
  
  delay(5000); // Keep buzzer and LED on for 5 seconds
  
  // Turn off buzzer and red LED after delay
  digitalWrite(BUZZER_PIN, LOW);
  digitalWrite(RED_LED, LOW);
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
led1:A
led1:C
led2:A
led2:C
bz1:1
bz1:2
NOCOMNCVCCGNDINLED1PWRRelay Module
relay1:VCC
relay1:GND
relay1:IN
relay1:NC
relay1:COM
relay1:NO
RFID-RC522Breakout
chip1:SDA
chip1:SCK
chip1:MOSI
chip1:MISO
chip1:RST
chip1:GND
chip1:VCC