#include <Keypad.h>

#define RELAY_PIN   19 // ESP32 pin GPIO19 connected to the relay
#define ROW_NUM     4  // keypad four rows
#define COLUMN_NUM  3  // keypad three columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte pin_rows[ROW_NUM] = {12, 14, 27, 26};   // ESP32 pin: GPIO12, GPIO14, GPIO27, GPIO26 connected to the row pins
byte pin_column[COLUMN_NUM] = {25, 33, 32};  // ESP32 pin: GPIO25, GPIO33, GPIO32 connected to the column pins


Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

const String password_1 = "1234567"; // change your password here
const String password_2 = "987654";  // change your password here
const String password_3 = "55555";   // change your password here
String input_password;

void setup() {
  Serial.begin(9600);
  input_password.reserve(32);   // maximum input characters is 33, change if needed
  pinMode(RELAY_PIN, OUTPUT);   // initialize pin as an output.
  digitalWrite(RELAY_PIN, LOW); // lock the door
}

void loop() {
  char key = keypad.getKey();

  if (key){
    Serial.println(key);

    if(key == '*') {
      input_password = ""; // reset the input password
    } else if(key == '#') {
      if(input_password == password_1 || input_password == password_2 || input_password == password_3) {
        Serial.println("The password is correct, unlocking the door in 20 seconds");
        digitalWrite(RELAY_PIN, HIGH);  // unlock the door for 20 seconds
        delay(20000);
        digitalWrite(RELAY_PIN, LOW); // lock the door
      } else {
        Serial.println("The password is incorrect, try again");
      }

      input_password = ""; // reset the input password
    } else {
      input_password += key; // append new character to input password string
    }
  }
}

esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
keypad1:R1
keypad1:R2
keypad1:R3
keypad1:R4
keypad1:C1
keypad1:C2
keypad1:C3
keypad1:C4
led1:A
led1:C
r1:1
r1:2
NOCOMNCVCCGNDINLED1PWRRelay Module
relay1:VCC
relay1:GND
relay1:IN
relay1:NC
relay1:COM
relay1:NO