#define BLYNK_TEMPLATE_ID "TMPLgCeV0y1b"
#define BLYNK_TEMPLATE_NAME "Home"
#define BLYNK_AUTH_TOKEN "93h-1b23ewIQooDTdB2y2COGacfYkbdO"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHTesp.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
// Libraries for Smart Home
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Libraries for Door Lock
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
// Definitions and Global Variables for Smart Home
BlynkTimer timer;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
int val = 0, va1,va2,va3,va4,va5,ge, t =15 ;
float tmp,hum = 0;
int ledPin = 33;
int inputPin = 27;
int pirState,k;
int v = 0;
// 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, 15}; // Connect these to the row pinouts of the keypad
byte colPins[COLS] = {32, 17, 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;
// Declare DHTesp object
DHTesp temps;
// Declare custom characters
byte Lck[8] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
};
byte house1[8] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
};
byte house2[8] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
};
byte house3[8] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
};
byte house4[8] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
};
void setup()
{
// Setup for Smart Home
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(5, OUTPUT);
pinMode(18, OUTPUT);
pinMode(19, OUTPUT);
pinMode(4, OUTPUT);
pinMode(23,INPUT);
pinMode(2,OUTPUT);
temps.setup(t, DHTesp::DHT22);
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT_PULLUP);
lcd.init();
lcd.backlight();
digitalWrite(5, LOW);
digitalWrite(18, LOW);
digitalWrite(19, LOW);
digitalWrite(21, LOW);
lcd.setCursor(0,0);
lcd.print("CircuitDesignContest");
lcd.setCursor(8,1);
lcd.print("2022");
lcd.setCursor(0,2);
lcd.print("--------------------");
lcd.setCursor(9,3);
lcd.print("- eDiYLaBs");
delay(3000);
lcd.clear();
lcd.createChar(6, Lck);
lcd.createChar(1,house1);
lcd.createChar(2,house2);
lcd.createChar(3,house3);
lcd.createChar(4,house4);
lcd.setCursor(1,2);
lcd.write(1);
lcd.setCursor(1,3);
lcd.write(2);
lcd.setCursor(2,2);
lcd.write(3);
lcd.setCursor(2,3);
lcd.write(4);
lcd.setCursor(17,2);
lcd.write(1);
lcd.setCursor(17,3);
lcd.write(2);
lcd.setCursor(18,2);
lcd.write(3);
lcd.setCursor(18,3);
lcd.write(4);
lcd.setCursor(19,0);
lcd.write(6);
lcd.setCursor(9,0);
lcd.print("connected-");
lcd.setCursor(2,1);
lcd.print("HOME AUTOMATION");
lcd.setCursor(6,2);
lcd.print("USING IOT");
delay(3000);
Blynk.virtualWrite(V7, pirState);
timer.setInterval(1000L, myTimer);
// Setup for Door Lock
myServo.attach(servoPin);
lcd.init();
lcd.backlight();
updateLockStatus();
}
void loop()
{
// Loop for Smart Home
Blynk.run();
timer.run();
val = digitalRead(23);
if(val == 1)
{
digitalWrite(2,va5);
}
else{
digitalWrite(2,LOW);
}
// Loop for Door Lock
char key = keypad.getKey();
if (key) {
handleKeypadInput(key);
}
}
// Additional functions for Smart Home
void myTimer()
{
Blynk.virtualWrite(V5,tmp);
Blynk.virtualWrite(V6,hum);
}
// Additional functions for Door Lock
void updateLockStatus() {
if (locked) {
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, "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(0); // Adjust the angle as per your requirement
}
void lockDoor() {
locked = true;
myServo.write(90); // Adjust the angle as per your requirement
}