#define BLYNK_TEMPLATE_ID "TMPL5vIAZwp1z"
#define BLYNK_TEMPLATE_NAME "Smart Security System"
#define BLYNK_AUTH_TOKEN "xUgaIijEvwPjyA0JY16JmpQN15gClluY"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
char ssid[] = "WIFI_NAME";
char pass[] = "WIFI_PASSWORD";
#define PIR_PIN 5
#define LDR_PIN 34
#define LED_PIN 2
BlynkTimer timer;
int readings[10];
int idx = 0;
int motionCount = 0;
void sendToBlynk() {
int motion = digitalRead(PIR_PIN);
int lightLevel = analogRead(LDR_PIN);
// moving average buffer
readings[idx] = lightLevel;
idx = (idx + 1) % 10;
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += readings[i];
}
int threshold = sum / 10;
// motion logic
if (motion == HIGH) {
motionCount++;
} else {
motionCount = 0;
}
bool highRisk = (motionCount >= 3 && lightLevel < threshold);
// LED control
digitalWrite(LED_PIN, highRisk ? HIGH : LOW);
// Send to Blynk
Blynk.virtualWrite(V0, motion);
Blynk.virtualWrite(V1, lightLevel);
// Optional debug
Serial.print("Motion: ");
Serial.print(motion);
Serial.print(" | Light: ");
Serial.print(lightLevel);
Serial.print(" | Threshold: ");
Serial.println(threshold);
// OPTIONAL: Push notification (if enabled in Blynk)
if (highRisk) {
Blynk.logEvent("intrusion_alert", "HIGH RISK: Motion detected in low light!");
}
}
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
// initialize buffer
for (int i = 0; i < 10; i++) {
readings[i] = analogRead(LDR_PIN);
}
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(300L, sendToBlynk);
}
void loop() {
Blynk.run();
timer.run();
}
see less
Reply
has context menu
Post in channel