#define BLYNK_TEMPLATE_ID "TMPL3zEBgQHXd"
#define BLYNK_TEMPLATE_NAME "OTP BASED DOOR LOCK SYSTEM"
#define BLYNK_AUTH_TOKEN "RejNl3aliyaPpABgkANzbHyw3zGRkQt-"
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp32.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
char auth[] = "RejNl3aliyaPpABgkANzbHyw3zGRkQt-";
char ssid[] = "relic_07"; // type your wifi name
char password[] = "TRYNOT2open"; // type your wifi password
#include <Keypad.h>
#include<ESP32Servo.h>
Servo servo;
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);
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define BUZZER 16
#define irsensor 17
int sw = 15;
int otp;
String otpstring = "";
int i = 0;
void setup()
{
Serial.begin(115200);
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);
}
Blynk.config(BLYNK_AUTH_TOKEN);
Blynk.config(auth);
Serial.println("READY");
lcd.clear();
lcd.print(" WIFI CONNECTED ");
Serial.print(" WIFI CONNECTED ");
pinMode(sw, INPUT_PULLUP);
pinMode(BUZZER,OUTPUT);
digitalWrite(BUZZER,LOW);
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 ");
if(digitalRead(irsensor)==LOW)
{
for(i=80;i>=0;i--)
{
servo.write(i);
delay(15);
lcd.clear();
lcd.print("* THANK YOU *");
}
delay(10000);
for(i=0;i<=80;i++)
{
servo.write(i);
delay(15);
}
}
if (digitalRead(sw) == LOW) {
otp = random(2000, 9999);
otpstring = String(otp);
Serial.println(otpstring);
while (digitalRead(sw) == LOW) {}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("OTP SENT TO YOUR");
lcd.setCursor(0, 1);
lcd.print(" GAMIL AND IOT ");
delay(3000);
Serial.print("OTP is ");
delay(100);
Serial.println(otpstring);
delay(100);
Blynk.logEvent("sw",otpstring);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter OTP :");
getotp();
}
Blynk.run();
}
void getotp() {
String y = "";
int a = y.length();
while (a < 4) {
char customKey = customKeypad.getKey();
if (customKey)
{
lcd.setCursor(0, 1);
y = y + customKey;
lcd.print(y);
a = y.length();
}
}
Serial.print("Entered OTP is ");
Serial.println(y);
if (otpstring == y) {
lcd.setCursor(0, 0);
lcd.print("Access Granted");
lcd.setCursor(0, 1);
lcd.print("Door Opening");
delay(2000);
lcd.clear();
lcd.print("* WELCOME *");
delay(3000);
for(i=90;i>=0;i--)
{
servo.write(i);
delay(15);
}
delay(10000);
for(i=0;i<=90;i++)
{
servo.write(i);
delay(15);
}
}
else
{
lcd.setCursor(0, 0);
lcd.print("Access Failed");
lcd.setCursor(0, 1);
lcd.print("Try Again !!!");
digitalWrite(BUZZER,HIGH);
delay(2000);
digitalWrite(BUZZER,LOW);
delay(3000);
}
}