#define BLYNK_TEMPLATE_ID "TMPL6mgffu1tv"
#define BLYNK_TEMPLATE_NAME "Smart Door Lock"
#define BLYNK_AUTH_TOKEN "KIhQ1ZyWaKd50DA0gsnLAm-Ip796nHnh"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
// Prototipe fungsi
void unlockDoor();
void lockDoor();
void handleKeypadInput(char key);
void updateLockStatus();
// Blynk settings
char auth[] = BLYNK_AUTH_TOKEN;
// WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Define keypad layout and pins
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {34, 35, 32, 33};
byte colPins[COLS] = {25, 26, 27, 14};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define servo settings
Servo myServo;
const int servoPin = 22;
const int unlockPosition = -90; // Set servo position to -90 degrees
const int lockPosition = 90; // Set servo position to +90 degrees
// Define LCD settings
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Variables
bool locked = true;
char enteredCode[5]; // To store the entered code
int codeIndex = 0;
// Blynk virtual pin handler
BLYNK_WRITE(V0) {
int switchState = param.asInt();
if (switchState == 1) { // Jika switch ON
unlockDoor();
} else { // Jika switch OFF
lockDoor();
}
}
void setup() {
Serial.begin(9600);
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Initialize servo
myServo.attach(servoPin);
// Initialize LCD
lcd.begin(16, 2);
lcd.backlight();
// Display initial lock status
updateLockStatus();
}
void loop() {
Blynk.run();
// Handle keypad input
char key = keypad.getKey();
if (key) {
handleKeypadInput(key);
}
}
void updateLockStatus() {
if (locked) {
lcd.clear();
lcd.print("Door Locked");
Blynk.virtualWrite(V0, 0); // Update Blynk switch to OFF
} else {
lcd.clear();
lcd.print("Door Unlocked");
Blynk.virtualWrite(V0, 1); // Update Blynk switch to ON
}
}
void handleKeypadInput(char key) {
if (locked) {
if (key == '#' && codeIndex > 0) { // Check for Enter key
enteredCode[codeIndex] = '\0'; // Null-terminate the entered code
codeIndex = 0;
if (strcmp(enteredCode, "1234") == 0) { // Check if the entered code is correct
unlockDoor();
lcd.clear();
lcd.print("Door Unlocked");
} else {
lcd.clear();
lcd.print("Incorrect Pin!");
delay(2000); // Display the message for 2 seconds
lcd.clear();
lcd.print("Door Locked");
}
// Clear entered code
memset(enteredCode, 0, sizeof(enteredCode));
} else if (key == 'C' && codeIndex > 0) { // Check for 'C' key to delete
lcd.setCursor(codeIndex - 1, 1);
lcd.print(' ');
codeIndex--;
enteredCode[codeIndex] = '\0';
} else if (key != '#' && key != 'C' && codeIndex < sizeof(enteredCode) - 1) {
enteredCode[codeIndex] = key;
lcd.setCursor(codeIndex, 1);
lcd.print('*');
codeIndex++;
}
} else {
if (key == '*') {
lockDoor();
lcd.clear();
lcd.print("Door Locked");
}
}
}
void unlockDoor() {
locked = false;
myServo.write(unlockPosition);
updateLockStatus();
}
void lockDoor() {
locked = true;
myServo.write(lockPosition);
updateLockStatus();
}