#define BLYNK_TEMPLATE_ID "TMPL303ssUc_a"
#define BLYNK_TEMPLATE_NAME "IOT INTRUDER MESSAGE"
#define BLYNK_AUTH_TOKEN "pdKNk8HVTGwrhdxxlp5N3TUVgNzABTp6"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define COOLDOWN_INTERVAL 1000 // 1 second cooldown interval (in milliseconds)
unsigned long lastNotificationTime = 0; // Variable to track the time of the last notification
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define PIR_PIN 19 // PIR sensor connected to pin 19
void setup()
{
Serial.begin(9600);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); // Use BLYNK_AUTH_TOKEN here
pinMode(PIR_PIN, INPUT);
}
void loop()
{
if (digitalRead(PIR_PIN) == HIGH) { // If motion detected
if (millis() - lastNotificationTime > COOLDOWN_INTERVAL) { // Check if cooldown interval has passed
Serial.println("Intruder detected!");
for (int i = 0; i < 2; i++) { // Send 20 newline characters to clear the terminal buffer
Blynk.virtualWrite(V1, "\n");
delay(100); // Small delay between each newline character
}
Blynk.virtualWrite(V1, "Intruder detected in secret room!"); // Send new notification
Blynk.virtualWrite(V2, 1); // Turn on a virtual pin connected to a display widget
lastNotificationTime = millis(); // Update last notification time
}
delay(1000); // Delay to avoid multiple triggers in quick succession
} else {
Blynk.virtualWrite(V2, 0); // Turn off the virtual pin when no intruder is detected
}
Blynk.run();
}