#define BLYNK_TEMPLATE_ID "TMPL451Ei5fz3"
#define BLYNK_TEMPLATE_NAME "Smart Home Security"
#define BLYNK_AUTH_TOKEN "vyCsEylY7lyvtTx1pypvRcOw_ovEHHlU"
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Blynk Auth Token (replace with your token)
char auth[] = BLYNK_AUTH_TOKEN;
// WiFi credentials (replace with your SSID and password)
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// LCD setup
#define LCD_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLUMNS, LCD_ROWS);
// Define pins for ESP32
int led = 13; // LED connected to GPIO 13
int buzzer = 12; // Buzzer connected to GPIO 12
int sensor = 2; // PIR sensor connected to GPIO 2
bool motionDetected = false; // To prevent sending multiple notifications for the same event
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the LCD
lcd.init();
lcd.backlight();
// Set up the GPIO pins
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(sensor, INPUT);
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Initial LCD message
lcd.setCursor(0, 0);
lcd.print("System Starting...");
delay(2000);
lcd.clear();
}
void loop() {
Blynk.run(); // Run Blynk
int physicalSensorValue = digitalRead(sensor); // Read the physical PIR sensor value
if (physicalSensorValue == HIGH) {
// If motion is detected by the PIR sensor
if (!motionDetected) {
// Trigger the Blynk event to send a notification
Blynk.logEvent("MotionDetected", "Motion detected!");
// Blynk.setProperty(V0,"label", "Motion Detected!");
Blynk.virtualWrite(V0, HIGH); // Set V0 to HIGH to indicate motion
motionDetected = true; // Set the flag to true to prevent further notifications
}
digitalWrite(led, HIGH);
tone(buzzer, 1000, 300);
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("MOTION");
lcd.setCursor(4, 1);
lcd.print("DETECTED!");
} else {
// If no motion is detected
digitalWrite(led, LOW);
noTone(buzzer);
lcd.clear();
lcd.setCursor(7, 0);
lcd.print("NO");
lcd.setCursor(5, 1);
lcd.print("MOTION");
Blynk.virtualWrite(V0, LOW); // Set V0 to LOW to indicate no motion
motionDetected = false; // Reset the flag to allow a new notification
}
delay(500); // Small delay to stabilize sensor readings and avoid flicker
}
// #define BLYNK_TEMPLATE_ID "TMPL451Ei5fz3"
// #define BLYNK_TEMPLATE_NAME "Smart Home Security"
// #define BLYNK_AUTH_TOKEN "vyCsEylY7lyvtTx1pypvRcOw_ovEHHlU"
// #include <LiquidCrystal_I2C.h>
// #include <WiFi.h>
// #include <WiFiClient.h>
// #include <BlynkSimpleEsp32.h>
// // Blynk Auth Token (replace with your token)
// char auth[] = BLYNK_AUTH_TOKEN;
// // WiFi credentials (replace with your SSID and password)
// char ssid[] = "Wokwi-GUEST";
// char pass[] = "";
// // LCD setup
// #define LCD_ADDR 0x27
// #define LCD_COLUMNS 16
// #define LCD_ROWS 2
// LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLUMNS, LCD_ROWS);
// // Define pins for ESP32
// int led = 13; // LED connected to GPIO 13
// int buzzer = 12; // Buzzer connected to GPIO 12
// int sensor = 2; // PIR sensor connected to GPIO 2
// bool sensorEnabled = true; // Flag to check if sensor is enabled
// int sensorValue = 0; // Variable to store simulated sensor value (from V0)
// bool motionDetected = false; // To prevent sending multiple notifications for the same event
// // Blynk Virtual Pin V0 will control the PIR sensor and provide simulated sensor input
// BLYNK_WRITE(V0) {
// sensorValue = param.asInt(); // Get the simulated sensor value from Blynk
// }
// void setup() {
// // Initialize serial communication
// Serial.begin(9600);
// // Initialize the LCD
// lcd.init();
// lcd.backlight();
// // Set up the GPIO pins
// pinMode(led, OUTPUT);
// pinMode(buzzer, OUTPUT);
// pinMode(sensor, INPUT);
// // Initialize Blynk
// Blynk.begin(auth, ssid, pass);
// // Initial LCD message
// lcd.setCursor(0, 0);
// lcd.print("System Starting...");
// delay(2000);
// lcd.clear();
// }
// void loop() {
// Blynk.run(); // Run Blynk
// if (sensorEnabled) {
// int physicalSensorValue = digitalRead(sensor); // Read the physical PIR sensor value
// if (physicalSensorValue == HIGH || sensorValue == 1) {
// // If motion is detected either by the physical sensor or by V0
// if (!motionDetected) {
// // Send notification only once when motion is first detected
// Blynk.notify("Motion detected!");
// motionDetected = true; // Set the flag to true to prevent further notifications
// }
// digitalWrite(led, HIGH);
// tone(buzzer, 1000, 300);
// lcd.clear();
// lcd.setCursor(5, 0);
// lcd.print("MOTION");
// lcd.setCursor(4, 1);
// lcd.print("DETECTED!");
// } else {
// // If no motion is detected
// digitalWrite(led, LOW);
// noTone(buzzer);
// lcd.clear();
// lcd.setCursor(7, 0);
// lcd.print("NO");
// lcd.setCursor(5, 1);
// lcd.print("MOTION");
// motionDetected = false; // Reset the flag to allow a new notification
// }
// } else {
// // If sensor is disabled
// digitalWrite(led, LOW);
// noTone(buzzer);
// lcd.clear();
// lcd.setCursor(4, 0);
// lcd.print("SENSOR");
// lcd.setCursor(4, 1);
// lcd.print("DISABLED");
// }
// delay(500); // Small delay to stabilize sensor readings and avoid flicker
// }
// #define BLYNK_TEMPLATE_ID "TMPL451Ei5fz3"
// #define BLYNK_TEMPLATE_NAME "Smart Home Security"
// #define BLYNK_AUTH_TOKEN "vyCsEylY7lyvtTx1pypvRcOw_ovEHHlU"
// #include <LiquidCrystal_I2C.h>
// #include <WiFi.h>
// #include <WiFiClient.h>
// #include <BlynkSimpleEsp32.h>
// // Blynk Auth Token (replace with your token)
// char auth[] = BLYNK_AUTH_TOKEN;
// // WiFi credentials (replace with your SSID and password)
// char ssid[] = "Wokwi-GUEST";
// char pass[] = "";
// // LCD setup
// #define LCD_ADDR 0x27
// #define LCD_COLUMNS 16
// #define LCD_ROWS 2
// LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLUMNS, LCD_ROWS);
// // Define pins for ESP32
// int led = 13; // LED connected to GPIO 13
// int buzzer = 12; // Buzzer connected to GPIO 12
// int sensor = 2; // PIR sensor connected to GPIO 2
// bool sensorEnabled = true; // Flag to check if sensor is enabled
// int sensorValue = 0; // Variable to store simulated sensor value (from V0)
// // Blynk Virtual Pin V0 will control the PIR sensor and provide simulated sensor input
// BLYNK_WRITE(V0) {
// sensorValue = param.asInt(); // Get the simulated sensor value from Blynk
// }
// void setup() {
// // Initialize serial communication
// Serial.begin(9600);
// // Initialize the LCD
// lcd.init();
// lcd.backlight();
// // Set up the GPIO pins
// pinMode(led, OUTPUT);
// pinMode(buzzer, OUTPUT);
// pinMode(sensor, INPUT);
// // Initialize Blynk
// Blynk.begin(auth, ssid, pass);
// // Initial LCD message
// lcd.setCursor(0, 0);
// lcd.print("System Starting...");
// delay(2000);
// lcd.clear();
// }
// void loop() {
// Blynk.run(); // Run Blynk
// if (sensorEnabled) {
// int physicalSensorValue = digitalRead(sensor); // Read the physical PIR sensor value
// if (physicalSensorValue == HIGH || sensorValue == 1) {
// // If motion is detected either by the physical sensor or by V0
// digitalWrite(led, HIGH);
// tone(buzzer, 1000, 300);
// lcd.clear();
// lcd.setCursor(5, 0);
// lcd.print("MOTION");
// lcd.setCursor(4, 1);
// lcd.print("DETECTED!");
// } else {
// // If no motion is detected
// digitalWrite(led, LOW);
// noTone(buzzer);
// lcd.clear();
// lcd.setCursor(7, 0);
// lcd.print("NO");
// lcd.setCursor(5, 1);
// lcd.print("MOTION");
// }
// } else {
// // If sensor is disabled
// digitalWrite(led, LOW);
// noTone(buzzer);
// lcd.clear();
// lcd.setCursor(4, 0);
// lcd.print("SENSOR");
// lcd.setCursor(4, 1);
// lcd.print("DISABLED");
// }
// delay(500); // Small delay to stabilize sensor readings and avoid flicker
// }
// #define BLYNK_TEMPLATE_ID "TMPL451Ei5fz3"
// #define BLYNK_TEMPLATE_NAME "Smart Home Security"
// #define BLYNK_AUTH_TOKEN "vyCsEylY7lyvtTx1pypvRcOw_ovEHHlU"
// #include <LiquidCrystal_I2C.h>
// #include <WiFi.h>
// #include <WiFiClient.h>
// #include <BlynkSimpleEsp32.h>
// // Blynk Auth Token (replace with your token)
// char auth[] = BLYNK_AUTH_TOKEN;
// // WiFi credentials (replace with your SSID and password)
// char ssid[] = "Wokwi-GUEST";
// char pass[] = "";
// // LCD setup
// #define LCD_ADDR 0x27
// #define LCD_COLUMNS 16
// #define LCD_ROWS 2
// LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLUMNS, LCD_ROWS);
// // Define pins for ESP32
// int led = 13; // LED connected to GPIO 13
// int buzzer = 12; // Buzzer connected to GPIO 12
// int sensor = 2; // PIR sensor connected to GPIO 2
// bool sensorEnabled = true; // Flag to check if sensor is enabled
// int val = 0; // Variable to store sensor value
// // Blynk Virtual Pin V0 will control the PIR sensor
// BLYNK_WRITE(V0) {
// sensorEnabled = param.asInt(); // If 1, sensor is enabled; if 0, sensor is disabled
// }
// void setup() {
// // Initialize serial communication
// Serial.begin(9600);
// // Initialize the LCD
// lcd.init();
// lcd.backlight();
// // Set up the GPIO pins
// pinMode(led, OUTPUT);
// pinMode(buzzer, OUTPUT);
// pinMode(sensor, INPUT);
// // Initialize Blynk
// Blynk.begin(auth, ssid, pass);
// // Initial LCD message
// lcd.setCursor(0, 0);
// lcd.print("System Starting...");
// delay(2000);
// lcd.clear();
// }
// void loop() {
// Blynk.run(); // Run Blynk
// if (sensorEnabled) {
// // Read the sensor value
// val = digitalRead(sensor);
// if (val == HIGH) {
// // If motion is detected
// digitalWrite(led, HIGH);
// tone(buzzer, 1000, 300);
// lcd.clear();
// lcd.setCursor(5, 0);
// lcd.print("MOTION");
// lcd.setCursor(4, 1);
// lcd.print("DETECTED!");
// } else {
// // If no motion is detected
// digitalWrite(led, LOW);
// noTone(buzzer);
// lcd.clear();
// lcd.setCursor(7, 0);
// lcd.print("NO");
// lcd.setCursor(5, 1);
// lcd.print("MOTION");
// }
// } else {
// // If sensor is disabled
// digitalWrite(led, LOW);
// noTone(buzzer);
// lcd.clear();
// lcd.setCursor(4, 0);
// lcd.print("SENSOR");
// lcd.setCursor(4, 1);
// lcd.print("DISABLED");
// }
// delay(500); // Small delay to stabilize sensor readings and avoid flicker
// }
// #include <LiquidCrystal_I2C.h>
// #define LCD_ADDR 0x27
// #define LCD_COLUMNS 16
// #define LCD_ROWS 2
// LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLUMNS, LCD_ROWS);
// int led = 13;
// int buzzer = 12;
// int sensor = 2;
// int state = LOW;
// int val = 0;
// void setup() {
// // put your setup code here, to run once:
// pinMode(led, OUTPUT);
// pinMode(buzzer, OUTPUT);
// pinMode(sensor, INPUT);
// Serial.begin(9600);
// }
// void loop() {
// // put your main code here, to run repeatedly:
// val = digitalRead(sensor);
// if (val == HIGH) {
// digitalWrite(led, HIGH);
// tone(buzzer, 1000, 300);
// //delay(500);
// lcd.begin(16,2);
// lcd.init();
// lcd.backlight();
// lcd.setCursor(5,0);
// lcd.print("MOTION");
// lcd.setCursor(4,5);
// lcd.print("DETECTED!");
// //if (state == LOW) {
// // Serial.println("Motion Detected!");
// //state == HIGH;
// }
// else {
// digitalWrite(led, LOW);
// noTone(buzzer);
// //delay(500);
// lcd.begin(16,2);
// lcd.init();
// lcd.backlight();
// lcd.setCursor(7,0);
// lcd.print("NO");
// lcd.setCursor(5,5);
// lcd.print("MOTION");
// //if (state == HIGH) {
// //Serial.println("NO Motion");
// //state == LOW;
// }
// }