#define BLYNK_TEMPLATE_ID "TMPL3GNxb03K0"
#define BLYNK_TEMPLATE_NAME "Security system"
#define BLYNK_AUTH_TOKEN "fdgClwwP32IOSfZLFzfX9u6U4UUIKBOP"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
#include <Keypad.h>
char auth[] = "fdgClwwP32IOSfZLFzfX9u6U4UUIKBOP";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
int pirPin1 = 21;
int pirPin2 = 22;
int ledPin1 = 18;
int ledPin2 = 19;
int maxAttempts = 3;
int attemptCounter = 0;
Servo doorServo;
int servoPin = 5;
bool doorOpen = false;
bool notifyEnabled = false;
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] = {32, 33, 25, 26};
byte colPins[COLS] = {15, 4, 16, 17};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String password = "4523";
String inputPassword = "";
BLYNK_WRITE(V1) {
notifyEnabled = param.asInt();
Serial.println(notifyEnabled ? "Notifications Enabled" : "Notifications Disabled");
}
void setup() {
Serial.begin(115200);
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Blynk.begin(auth, ssid, pass);
pinMode(pirPin1, INPUT);
pinMode(pirPin2, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
doorServo.attach(servoPin);
doorServo.write(0); //
}
void loop(){
Blynk.run();
int motionDetected1 = digitalRead(pirPin1);
int motionDetected2 = digitalRead(pirPin2);
if (notifyEnabled) {
if (motionDetected1) {
Serial.println("Motion detected in Room 1!");
Blynk.logEvent("motion1");
digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin1, LOW);
}
if (motionDetected2) {
Serial.println("Motion detected in Room 2!");
Blynk.logEvent("motion2");
digitalWrite(ledPin2, HIGH);
} else {
digitalWrite(ledPin2, LOW);
}
} else {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '#') {
if (doorOpen) {
Serial.println("Door is already open!");
} else if (inputPassword == password) {
Serial.println("Access granted!");
doorServo.write(90);
doorOpen = true;
Blynk.logEvent("door_opened");
} else {
Serial.println("Access denied!");
attemptCounter++;
if (attemptCounter >= maxAttempts) {
Serial.println("Maximum attempts reached! Notifying Blynk...");
Blynk.logEvent("wrongpass");
attemptCounter = 0;
}
}
inputPassword = "";
} else if (key == '*') {
if (doorOpen) {
doorServo.write(0);
doorOpen = false;
Serial.println("Door closed!");
Blynk.logEvent("door_closed");
}
inputPassword = "";
} else {
inputPassword += key;
}
}
delay(100);
}