#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <FirebaseESP32.h>

const char * mqtt_id = "8393d86d-5435-4bb9-b49d-4821ca47b6b8";



int postId=0;
#define FIREBASE_HOST "https://testnew-smart-default-rtdb.asia-southeast1.firebasedatabase.app/"
#define FIREBASE_AUTH "AIzaSyCgBXV34hvx-o-Y1A0d-iXaeRphktwGj0Y"


FirebaseData firebaseData;
FirebaseJson json;

String empID= "/empID";

void setup_wifi() 
{
  Serial.println("Connecting to SSID: Wokwi-GUEST");
  WiFi.begin("Wokwi-GUEST", "");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected");
  Serial.print(", IP address: ");
  Serial.println(WiFi.localIP());
}

LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);

const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

uint8_t colPins[COLS] = { 26, 25, 33, 32 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 13, 12, 14, 27 }; // Pins connected to R1, R2, R3, R4

Keypad keypad1 = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

char userpress[4];

int inputPin = 15;               
int pirState = LOW;             
int val = 0; 

const int servoPin = 19;

Servo servo;

String inputString = "";

void setup() {
  setup_wifi();
  FirebaseConfig firebaseConfig;
  Firebase.begin(&firebaseConfig, nullptr);

 
  Firebase.reconnectWiFi(true);

  pinMode(inputPin, INPUT);

  servo.attach(servoPin, 500, 2400);

  Serial.begin(115200);
  LCD.init();
  LCD.backlight();
  LCD.print("Welcome");
  delay(1000);
  LCD.clear();
  LCD.print("Press *");
  LCD.setCursor(0, 1);
}

int pos = 0;

void loop() {
  val = digitalRead(inputPin);
  byte keyCount = 0;
  char key = keypad1.getKey();
  if(key != NO_KEY) {
    if (key == '*'){
      boolean a=1;
      LCD.clear();
      LCD.print("Enter ID: ");
      LCD.setCursor(0,1);
      while (a){
        key = keypad1.getKey();
        if (key != NO_KEY){
          userpress[keyCount] = key;
          LCD.print("*");
          inputString += key;
          keyCount++;
          if (keyCount==4) a=0;
        }
    }
    check_password();
    }
  }
}



void check_password(){
  byte b=0;
  for(int i=0; i<4; i++){
    b++;
  }



  if (b==4 && val == HIGH){
    LCD.clear();
    LCD.print("Attendence Saved");
    delay(1000);
  

    postId=postId+1;
    Firebase.setString(firebaseData,"/"+String(postId)+"/empID", inputString);

    Serial.println("set data done!");
    delay(2000);
    
    inputString = "";
    
    LCD.clear();
    LCD.print("Door Open");
      
    for (pos = 0; pos <= 90; pos += 1) {
      servo.write(pos);
      delay(25);
    }

    
     
    for (pos = 90; pos >= 0; pos -= 1) {
      servo.write(pos);
       delay(25);
    }


    LCD.clear();
    LCD.print("Door Closed");
    delay(1000);
    LCD.clear();
    LCD.print("Press *");

  }
  else{
    LCD.clear();
    LCD.print("No Motion !");
    delay(1000);
    LCD.setCursor(3,1);
    LCD.print("Blocked!!");
    delay(1000);
    LCD.clear();
    LCD.print("Press *");
    
  }
}