#include <ESP32Servo.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
//Define things to get connected to Blynk
#define BLYNK_TEMPLATE_ID "TMPL6v5k9Vygg"
#define BLYNK_TEMPLATE_NAME "IoT Mini Project ESP32"
#define BLYNK_AUTH_TOKEN "W56k1HcELq9lGbQnEw4dJ5lwx5XLxb6t"
#include <BlynkSimpleEsp32.h>
//Include library to connect to internet
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
//Define modul
#define PIN_TRIG 34
#define PIN_ECHO 35
#define timeDelay 5000
const uint8_t ROWS = 4;
const uint8_t COLS = 3;
char keys[ROWS][COLS] = {
{ '1', '2', '3',},
{ '4', '5', '6',},
{ '7', '8', '9',},
{ '*', '0', '#',}
};
uint8_t rowPins[ROWS] = { 23, 19, 18, 15 }; // Pins connected to R1, R2, R3, R4
uint8_t colPins[COLS] = { 5, 4, 2 }; // Pins connected to C1, C2, C3
Servo myservo;
LiquidCrystal_I2C lcd(0x27, 20, 4);
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String password = "7777";
String enterPass = "";
bool passEntered;
void setup() {
Serial.begin(9600);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
myservo.attach(11);
lcd.init();
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
}
void loop() {
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
Blynk.run();
int duration = pulseIn(PIN_ECHO, HIGH);
int distance = duration / 58;
if(distance < 150) { // Sensor activated
char key = keypad.getKey();
lcd.backlight();
if (passEntered == false) { // Input password
lcd.setCursor(1, 1);
lcd.print("ENTER PASSWORD :");
passEntered = true;
}
if (key != NO_KEY) {
if (key == '#') {
if (password == enterPass) { // Password correct
beep();
//digitalWrite(greenLED, HIGH);
//digitalWrite(redLED, LOW);
myservo.write(0);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("PASSWORD CORRECT");
lcd.setCursor(1, 1);
lcd.print("DOOR OPENED");
delay(timeDelay);
lcd.clear();
reset();
}
else { // Password incorrect
//digitalWrite(greenLED, LOW);
//digitalWrite(redLED, HIGH);
myservo.write(90);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("PASSWORD INCORRECT");
reset();
}
}
else { // Input password
enterPass += key;
lcd.setCursor(1, 3);
lcd.print(enterPass);
}
}
} else { // Sensor inactive
//digitalWrite(greenLED, LOW);
//digitalWrite(redLED, LOW);
myservo.write(90);
lcd.clear();
lcd.noBacklight();
reset();
}
}
void beep() {
tone(8, 1000, 50);
}
void reset() {
passEntered = false;
enterPass = "";
}