/*
 * This ESP32 code is created by esp32io.com
 *
 * This ESP32 code is released in the public domain
 *
 * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-door-lock-system-using-password
 */

#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>


#define OLED_RESET 16
Adafruit_SSD1306 display(OLED_RESET);

#define ROW_NUM     4  // four rows
#define COLUMN_NUM  4  // four columns

#define RELAYIN   15 // ESP32 pin GIOP15 - D15 connected to the IN pin of relay
#define BT1 18   // ESP32 pin GIOP2 - D2
#define PIR 4   // ESP32 pin GIOP4 - D4
// LED RBG
#define PIN_GREEN  5 // GIOP16 - RX2 on Board

int val = 0;
int i = 0;
int num =0;
char password[6];
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte pin_rows[ROW_NUM]      = {13, 12, 14, 27}; // GIOP19, GIOP18, GIOP5, GIOP17 connect to the row pins
byte pin_column[COLUMN_NUM] = {26, 25, 33, 32};   // GIOP16, GIOP4, GIOP2, GIOP15 connect to the column pins

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
LiquidCrystal_I2C lcd(0x27, 20, 4);
char localKey = keypad.getKey();
const String password_1 = "284855"; // change your password here
const String password_2 = "757488";  // change your password here
const String password_3 = "010859";  // change your password here
String inputassword;

void setup() {
  Serial.begin(115200);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3c); //สั่งให้จอ OLED เริ่มทำงานที่ Address 0x3C
  inputassword.reserve(32); // maximum input characters is 32
  pinMode(RELAYIN, OUTPUT); 
  digitalWrite(RELAYIN, HIGH); // lock the door
  pinMode(BT1, INPUT_PULLUP); 
  pinMode(PIR, INPUT);
  lcd.init();
  // turn on LCD backlight                      
  lcd.backlight();
  lcd.setCursor(3, 0); 
  lcd.print("Welcome to");
  lcd.setCursor(0, 1); 
  lcd.print("Door-Lock-System");
  delay(3000);
  lcd.setCursor(0, 1); 
  lcd.print("                ");
  pinMode(PIN_GREEN, OUTPUT);

display.clearDisplay(); // ลบภาพในหน้าจอทั้งหมด
display.setTextSize(1); // กำหนดขนาดตัวอักษร
display.setTextColor(WHITE);
display.setCursor(16,0); // กำหนดตำแหน่ง x,y ที่จะแสดงผล
display.println("Welcome to");
display.setCursor(0,15);
display.setTextSize(2);
display.setTextColor(BLACK, WHITE); //กำหนดข้อความสีขาว ฉากหลังสีดำ
display.println(" Door-Lock");
display.setCursor(0,25);
display.setTextSize(2);
display.setTextColor(BLACK, WHITE); //กำหนดข้อความสีขาว ฉากหลังสีดำ
display.println("System ");
display.display();
}

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

    if(key != NO_KEY && (key=='1'||key=='2'
      ||key=='3'||key=='4'||key=='5'||key=='6'
      ||key=='7'||key=='8'||key=='9'||key=='0')){
    password[i++] = key;
    lcd.setCursor(i, 1);
    lcd.print("*");
    Serial.println("Count is : " + String(i));
          inputassword += key; // append new character to input password string
       Serial.println(inputassword);
    num += 1;
    // lcd.setCursor(15, 1);
    // lcd.print(num);
    Serial.println("Num is : " + String(num));
  }

  lcd.setCursor(1, 0); 
  lcd.print("Enter Password");
  if( digitalRead(PIR) == HIGH){
       lcd.backlight();
  }else{
    lcd.noBacklight();
  }
  if( digitalRead(BT1) == LOW){
        digitalWrite(RELAYIN, LOW);  // unlock the door for 20 seconds
        digitalWrite(PIN_GREEN,HIGH);
        i = 0;
        num = 0;
        lcd.clear();
        lcd.setCursor(0, 0); 
        lcd.print("Unlock The  Door"); 
        delay(10000);
        lcd.clear();  
        delay(10000);
        digitalWrite(RELAYIN, HIGH); // lock the door}
        digitalWrite(PIN_GREEN,LOW); 
  }
  if (key) {
    Serial.println(key);

    if (key == '*') {
      inputassword = ""; // reset the input password
      lcd.setCursor(0, 1); 
      lcd.print("                ");
      i = 0;
      num = 0;
    } else if (num == 6) {
      if (inputassword == password_1 || inputassword == password_2 || inputassword == password_3) {
        Serial.println("Valid Password => unlock the door");
        digitalWrite(RELAYIN, LOW);  // unlock the door for 20 seconds
        digitalWrite(PIN_GREEN,HIGH);
        i = 0;
        num = 0;
        lcd.clear();
        lcd.setCursor(0, 0); 
        lcd.print("Unlock The  Door"); 
        delay(10000);
        lcd.clear();
        digitalWrite(PIN_GREEN,LOW);
        digitalWrite(RELAYIN, HIGH); // lock the door
      } else {
        i = 0;
        num = 0;
        lcd.clear();     
        lcd.setCursor(5, 0); 
        lcd.print("!!!!!!");
        lcd.setCursor(0, 1); 
        lcd.print("Invalid Password");
        Serial.println("Invalid Password => Try again");
        delay(1500);
        lcd.setCursor(0, 1); 
        lcd.print("                ");
      }

      inputassword = ""; // reset the input password
    } else {
    }
  }
}
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
NOCOMNCVCCGNDINLED1PWRRelay Module
relay1:VCC
relay1:GND
relay1:IN
relay1:NC
relay1:COM
relay1:NO
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
pir1:VCC
pir1:OUT
pir1:GND
r1:1
r1:2
led1:A
led1:C
led2:A
led2:C
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA