#define BLYNK_TEMPLATE_ID "TMPL6WCF3t-Je"
#define BLYNK_TEMPLATE_NAME "relay"
#define BLYNK_AUTH_TOKEN "oBaIgguIa-Q1ZI8NuJCvvlXfh1BVDA1d"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <BlynkSimpleEsp32.h>
#include <Keypad.h>
#define PIR_PIN 19
#define LED_PIN 5
#define TRIG_PIN 13
#define ECHO_PIN 12
#define BUZZER_PIN 18
// #define SERVO_PIN 18
#define RELAY_PIN 2
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int servoPin = 18;
int attempts = 0;
const int maxAttempts = 3;
Servo servo;
int pos = 0;
const int Password_Length = 7;
String Data;
String Master = "1234567";
byte data_count = 0;
char customKey;
int lockOutput = 5;
// Define keypad layout
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{ '1', '2', '3' },
{ '4', '5', '6' },
{ '7', '8', '9' },
{ '*', '0', '#' }
};
byte rowPins[ROWS] = { 32, 33, 34, 35 };
byte colPins[COLS] = { 36, 37, 38, 39};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup()
{
Serial.begin(115200);
Serial.println("Hello, Raafat Al3a9y Smart Home 🏡!");
pinMode(PIR_PIN, INPUT);
// pinMode(LED_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
lcd.init();
lcd.backlight();
// Servo.attach(SERVO_PIN);
// attach servo pin with a min/max timings of 500 microseconds and 2400 microseconds
servo.attach(servoPin, 500, 2400);
}
void loop() {
light();
Access();
}
void Access()
{
long duration, distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
if (distance < 50)
{
digitalWrite(LED_PIN, HIGH);
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
customKey = customKeypad.getKey();
if (customKey)
{
Data += customKey;
lcd.setCursor(data_count, 1);
lcd.print("*");
data_count++;
}
if (data_count == Password_Length)
{
if (Data == Master)
{
opendoor();
delay(1000);
lockdoor();
}
else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Incorrect");
attempts++;
delay(1000);
lcd.clear();
}
clearData();
if (attempts >= 2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Max Attempts");
delay(6000);
lcd.clear();
}
}
delay(1000);
digitalWrite(LED_PIN, LOW);
}
}
void Face() {
lcd.print("Enter success");
digitalWrite(lockOutput, HIGH);
delay(3000);
digitalWrite(lockOutput, LOW);
delay(2000);
}
void clearData()
{
data_count = 0;
Data = "";
}
void opendoor()
{
for (pos = 0; pos <= 180; pos += 1)
{
servo.write(pos);
delay(10);
}
}
void lockdoor()
{
for (pos = 180; pos >= 0; pos -= 1)
{
servo.write(pos);
delay(10);
}
}
void light()
{
int pirState = digitalRead(PIR_PIN);
if (pirState == HIGH)
{
// digitalWrite(LED_PIN, HIGH);
digitalWrite(RELAY_PIN, HIGH);
lcd.setCursor(0,0);
lcd.print("Motion Detected");
delay(500);
}
else
{
// digitalWrite(LED_PIN, LOW);
digitalWrite(RELAY_PIN, LOW);
lcd.clear();
}
}