#include <WiFi.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// ===== Pins =====
#define PIR 4
#define BUZZER 18
#define LED 19
// ===== LCD =====
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ===== WiFi (Simulation) =====
const char* ssid = "Wokwi-GUEST";
// ===== Keypad Setup =====
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] = {13, 12, 14, 27};
byte colPins[COLS] = {26, 25, 33, 32};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// ===== Password =====
String password = "1234";
String input = "";
// ===== Setup =====
void setup() {
pinMode(PIR, INPUT);
pinMode(BUZZER, OUTPUT);
pinMode(LED, OUTPUT);
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.print("System Ready");
// WiFi (simulation)
WiFi.begin(ssid);
Serial.print("Connecting WiFi...");
}
// ===== Loop =====
void loop() {
int motion = digitalRead(PIR);
if (motion == HIGH) {
lcd.clear();
lcd.print("Motion Detected!");
delay(1000);
lcd.clear();
lcd.print("Enter Password:");
input = "";
unsigned long startTime = millis();
// ===== 10 seconds to enter password =====
while (millis() - startTime < 10000) {
char key = keypad.getKey();
if (key) {
input += key;
lcd.setCursor(0, 1);
lcd.print(input);
}
// ✅ Correct Password
if (input == password) {
lcd.clear();
lcd.print("Access Granted");
digitalWrite(BUZZER, LOW);
digitalWrite(LED, LOW);
delay(2000);
return;
}
}
// ❌ Wrong Password → Alarm
lcd.clear();
lcd.print("ALERT!!!");
digitalWrite(BUZZER, HIGH);
digitalWrite(LED, HIGH);
Serial.println("🚨 Motion Detected!");
Serial.println("📲 Sending WiFi Alert...");
delay(5000);
}
}