#define BLYNK_TEMPLATE_ID "TMPL43EEInpH8"
#define BLYNK_TEMPLATE_NAME "anti theft security system"
#define BLYNK_AUTH_TOKEN "r1zw8y2jobt6ozLXhDmzztZGGQfYrDzg"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
// Blynk credentials
char auth[] = "r1zw8y2jobt6ozLXhDmzztZGGQfYrDzg";
// Define pin numbers
const int pirSensorPin = 2; // GPIO pin connected to the PIR sensor output
const int buzzerPin = 16; // GPIO pin connected to the buzzer
const int ledPin = 17; // GPIO pin connected to the LED
// Function to check and handle PIR sensor output
void checkPIRSensor() {
int motionDetected = digitalRead(pirSensorPin); // Read motion sensor
if (motionDetected == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on LED
digitalWrite(buzzerPin, HIGH); // Turn on buzzer
Blynk.virtualWrite(V1, 255); // Update Blynk app: LED is ON
delay(1000); // Keep them on for 1 second
digitalWrite(ledPin, LOW); // Turn off LED
digitalWrite(buzzerPin, LOW); // Turn off buzzer
Blynk.virtualWrite(V1, 0); // Update Blynk app: LED is OFF
}
}
void setup() {
pinMode(pirSensorPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// Connect to WiFi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Connect to Blynk
Blynk.begin(auth, ssid, pass);
// Setup Blynk timer to check sensor status every 100 milliseconds
timer.setInterval(100L, checkPIRSensor);
}
void loop() {
Blynk.run(); // Run Blynk process
timer.run(); // Run timer events
}