// #include <WiFi.h>
// #include <Keypad.h>
// #include <LiquidCrystal_I2C.h>
// #include <ESP32Servo.h>
// char ssid[] = "Wokwi-GUEST";
// char password[] = "";
// Servo servo;
// LiquidCrystal_I2C lcd(0x27, 16, 2);
// const byte ROWS = 4;
// const byte COLS = 4;
// char hexaKeys[ROWS][COLS] = {
// { '1', '2', '3', 'A' },
// { '4', '5', '6', 'B' },
// { '7', '8', '9', 'C' },
// { '*', '0', '#', 'D' }
// };
// byte rowPins[ROWS] = { 12, 14, 27, 26 };
// byte colPins[COLS] = { 25, 33, 32, 35 };
// Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// #define BUZZER 15
// #define trigPin 23
// #define echoPin 19
// String otp = "5555";
// long distance;
// void setup() {
// Serial.begin(115200);
// Serial.println("hello it is running");
// lcd.init();
// lcd.backlight();
// lcd.clear();
// lcd.print("CONNECTING TO...");
// lcd.setCursor(0, 1);
// lcd.print(ssid);
// delay(1000);
// WiFi.begin(ssid, password);
// Serial.print("Connecting to WiFi ..");
// while (WiFi.status() != WL_CONNECTED) {
// Serial.print('.');
// delay(500);
// }
// lcd.clear();
// lcd.print(" WIFI CONNECTED ");
// Serial.println(" WIFI CONNECTED ");
// pinMode(BUZZER, OUTPUT);
// pinMode(trigPin, OUTPUT);
// pinMode(echoPin, INPUT);
// lcd.clear();
// lcd.print(" WIFI CONNECTED ");
// delay(2000);
// servo.attach(4);
// servo.write(90);
// delay(500);
// }
// void loop() {
// lcd.setCursor(0, 0);
// lcd.print(" OTP BASED DOOR ");
// lcd.setCursor(0, 1);
// lcd.print(" LOCK USING IOT ");
// // Check if someone is near the door using ultrasonic sensor
// measureDistance();
// if (distance <= 200) {
// activateAlarm();
// } else {
// deactivateAlarm();
// }
// if (digitalRead(customKeypad.getKey()) == LOW) {
// int otp = random(2000, 9999);
// String otpstring = String(otp);
// Serial.println(otpstring);
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("OTP SENT TO YOUR");
// lcd.setCursor(0, 1);
// lcd.print(" EMAIL AND IOT ");
// delay(3000);
// Serial.print("OTP is ");
// Serial.println(otpstring);
// delay(100);
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Enter OTP :");
// getotp();
// }
// }
// void measureDistance() {
// digitalWrite(trigPin, LOW);
// delayMicroseconds(2);
// digitalWrite(trigPin, HIGH);
// delayMicroseconds(10);
// digitalWrite(trigPin, LOW);
// distance = pulseIn(echoPin, HIGH) * 0.034 / 2; // Calculate distance in cm
// }
// void getotp() {
// String enteredOTP = "";
// while (enteredOTP.length() < 4) {
// char customKey = customKeypad.getKey();
// if (customKey) {
// lcd.setCursor(0, 1);
// enteredOTP += customKey;
// lcd.print('*');
// }
// }
// Serial.print("Entered OTP is ");
// Serial.println(enteredOTP);
// if (enteredOTP == String(otp)) {
// lcd.clear();
// lcd.print("Access Granted");
// lcd.setCursor(0, 1);
// lcd.print("Door Opening");
// delay(2000);
// lcd.clear();
// lcd.print("* WELCOME *");
// // Open the door
// for (int i = 90; i >= 0; i--) {
// servo.write(i);
// delay(15);
// }
// delay(10000);
// for (int i = 0; i <= 90; i++) {
// servo.write(i);
// delay(15);
// }
// } else {
// lcd.clear();
// lcd.print("Access Failed");
// lcd.setCursor(0, 1);
// lcd.print("Try Again !!!");
// digitalWrite(BUZZER, HIGH);
// delay(2000);
// digitalWrite(BUZZER, LOW);
// delay(3000);
// }
// }
// void activateAlarm() {
// digitalWrite(BUZZER, HIGH); // Turn on the buzzer
// }
// void deactivateAlarm() {
// digitalWrite(BUZZER, LOW); // Turn off the buzzer
// }
#include <WiFi.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
char ssid[] = "Wokwi-GUEST";
char password[] = "";
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 12, 14, 27, 26 };
byte colPins[COLS] = { 25, 33, 32, 35 };
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
#define BUZZER 15
#define trigPin 23
#define echoPin 19
String otp = "5555";
long distance;
void setup() {
Serial.begin(115200);
Serial.println("Initializing...");
lcd.init();
lcd.backlight();
lcd.clear();
lcd.print("CONNECTING TO...");
lcd.setCursor(0, 1);
lcd.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected!");
lcd.clear();
lcd.print("WiFi Connected!");
pinMode(BUZZER, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo.attach(4); // Attach servo to GPIO 4
servo.write(90); // Set servo to initial position
}
void loop() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("OTP BASED DOOR");
lcd.setCursor(0, 1);
lcd.print("LOCK USING IOT");
// Check if someone is near the door using ultrasonic sensor
measureDistance();
if (distance <= 200) {
activateAlarm();
} else {
deactivateAlarm();
}
char key = customKeypad.getKey();
if (key) {
if (isDigit(key) || key == '*') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter OTP:");
getotp();
}
}
}
void measureDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
distance = pulseIn(echoPin, HIGH) * 0.034 / 2; // Calculate distance in cm
}
void getotp() {
String enteredOTP = "";
while (enteredOTP.length() < 4) {
char customKey = customKeypad.getKey();
if (customKey && (isDigit(customKey) || customKey == '*')) {
lcd.setCursor(enteredOTP.length(), 1);
lcd.print('*');
enteredOTP += customKey;
}
}
Serial.print("Entered OTP is ");
Serial.println(enteredOTP);
if (enteredOTP == otp) {
lcd.clear();
lcd.print("Access Granted");
lcd.setCursor(0, 1);
lcd.print("Door Opening");
delay(2000);
lcd.clear();
lcd.print("* WELCOME *");
// Open the door
for (int i = 90; i >= 0; i--) {
servo.write(i);
delay(15);
}
delay(10000);
for (int i = 0; i <= 90; i++) {
servo.write(i);
delay(15);
}
} else {
lcd.clear();
lcd.print("Access Failed");
lcd.setCursor(0, 1);
lcd.print("Try Again !!!");
digitalWrite(BUZZER, HIGH);
delay(2000);
digitalWrite(BUZZER, LOW);
delay(3000);
}
}
void activateAlarm() {
digitalWrite(BUZZER, HIGH); // Turn on the buzzer
}
void deactivateAlarm() {
digitalWrite(BUZZER, LOW); // Turn off the buzzer
}