#define BLYNK_TEMPLATE_ID "TMPL58ms69n1I"
#define BLYNK_TEMPLATE_NAME "IOT ALARM SYSTEM"
#define BLYNK_AUTH_TOKEN "4JgM3oTFLm3AiIKazTjuEXAkk3xqqd-e" // Replace with your Blynk Auth Token
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#define TRIGGER_PIN 5
#define ECHO_PIN 18
#define MAX_DISTANCE 200
#define RED_LED 15
#define GREEN_LED 16
#define BUZZER 4
char ssid[] = "Galaxy A02s9481"; // Replace with your WiFi SSID
char pass[] = "mhug3102"; // Replace with your WiFi Password
LiquidCrystal_I2C lcd(0x27, 16, 2);
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {19, 23, 14, 27}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {13, 32, 33, 25}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String inputPassword;
String correctPassword = "3434";
bool alarmArmed = true;
void setup() {
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Alarm System");
Blynk.virtualWrite(V0, alarmArmed);
}
void loop() {
Blynk.run();
checkUltrasonicSensor();
char key = keypad.getKey();
if (key) {
lcd.setCursor(0, 1);
lcd.print("Enter Password: ");
inputPassword += key;
lcd.print(inputPassword);
if (inputPassword.length() == 4) {
if (inputPassword == correctPassword) {
digitalWrite(GREEN_LED, HIGH);
delay(2000);
digitalWrite(GREEN_LED, LOW);
alarmArmed = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Disarmed");
} else {
digitalWrite(RED_LED, HIGH);
tone(BUZZER, 1000);
delay(2000);
digitalWrite(RED_LED, LOW);
noTone(BUZZER);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong Password");
delay(1000);
}
inputPassword = "";
}
}
}
void checkUltrasonicSensor() {
if (alarmArmed) {
unsigned int distance = sonar.ping_cm();
if (distance > 0 && distance < 100) {
lcd.setCursor(0, 1);
lcd.print("Intruder Alert! ");
}
}
}
BLYNK_WRITE(V0) {
int pinValue = param.asInt();
if (pinValue == 1) {
alarmArmed = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alarm Armed");
} else {
alarmArmed = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alarm Disarmed");
}
}