#define BLYNK_TEMPLATE_ID "TMPL5pmYcTOx1"
#define BLYNK_TEMPLATE_NAME "Smart Home Security System"
#define BLYNK_AUTH_TOKEN "hJ6EP3vQsNewToWmONxFOARZPZfmfJug"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
// Libraries for Smart Home
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
int relay1=4;
const int motionsensor=33; // the pin that OUTPUT pin of sensor is connected to
int pinStateCurrent = LOW; // current state of pin
int pinStatePrevious = LOW; // previous state of pin
int status1 = LOW;
int inputPin =27;//doorlock
BlynkTimer timer;
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
Blynk.syncVirtual(V0); // will cause BLYNK_WRITE(V0) to be executed
Blynk.syncVirtual(V1); // will cause BLYNK_WRITE(V1) to be executed
}
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Definitions and Global Variables for Door Lock
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] = {12, 13, 14, 19}; // Connect these to the row pinouts of the keypad
byte colPins[COLS] = {32, 18, 25, 26}; // Connect these to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const int potPin = A0;
Servo myServo;
const int servoPin = 27; // Connect this to the PWM pin of the servo
bool locked = true;
char enteredCode[5]; // To store the entered code
int codeIndex = 0;
void setup()
{
// Debug console
Serial.begin(115200); // initialize serial
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);//connect to blynk
pinMode(motionsensor, INPUT); // set arduino pin to input mode to read value from OUTPUT pin of sensor
pinMode(relay1, OUTPUT); // Initialise digital pin GPIO 16 as an output pin
digitalWrite(relay1, HIGH);
delay(1000);// ensure relay starts off (LED is off)
digitalWrite(relay1, LOW);
// Setup for Door Lock
myServo.attach(servoPin);
lcd.init();
lcd.backlight();
updateLockStatus();
}
void loop()
{
Blynk.run();
timer.run();
pinStatePrevious = pinStateCurrent; // store old state
pinStateCurrent = digitalRead(motionsensor); // read new state
if (pinStatePrevious == LOW && pinStateCurrent == HIGH) { // pin state change: LOW -> HIGH
Serial.println("Motion detected!");
// TODO: turn on alarm, light or activate a device ... here
digitalWrite(relay1, HIGH);// trun on relay (and LED)
Blynk.virtualWrite(V0, 1);
Blynk.virtualWrite(V1, "Motion Detected");
Blynk.logEvent("Alert","Motion Detected");
lcd.clear();
lcd.print("Motion Detected"); // Display message on LCD
}
else
if (pinStatePrevious == HIGH && pinStateCurrent == LOW) { // pin state change: HIGH -> LOW
Serial.println("Motion stopped!");
// TODO: turn off alarm, light or deactivate a device ... here
digitalWrite(relay1, LOW);
Blynk.virtualWrite(V0, 0);
Blynk.virtualWrite(V1, "Motion Stopped");
lcd.clear();
lcd.print("Motion Stopped"); // Display message on LCD
}
// Loop for Door Lock
char key = keypad.getKey();
if (key) {
handleKeypadInput(key);
}
}
// Additional functions for Door Lock
void updateLockStatus() {
if (locked) {
lcd.print("Welcome Home "); // Print the first part of the welcome message
delay(2000);
lcd.clear();
lcd.print("Door Locked");
} else {
lcd.clear();
lcd.print("Door Unlocked");
}
}
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, "125D") == 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(0); // Adjust the angle as per your requirement
}
void lockDoor() {
locked = true;
myServo.write(90); // Adjust the angle as per your requirement
}