#define BLYNK_TEMPLATE_ID "TMPL4flJrop1W"
#define BLYNK_TEMPLATE_NAME "Alarm System"
#define BLYNK_AUTH_TOKEN "Enjfg3sNC_3EzPoiClbarfROVuDp-poK"
#include <WiFi.h>
#include <PubSubClient.h>
#include <BlynkSimpleEsp32.h>
#include <HTTPClient.h>
#include <UrlEncode.h>
const char *ssid = "Wokwi-GUEST"; // Change to your Wi-Fi network name
const char *password = ""; // Change to your Wi-Fi password
const char *auth = "9Ucm9712xaqwHEooSjZMD9W_niCQGi-d";
String stMac;
char mac[50];
char clientId[50];
WiFiClient espClient;
PubSubClient client(espClient);
const char *mqttServer = "mqtt.flespi.io";
const char *mqtt_user = "k15SZYBAzQFtsouVCFzwUmNSrOeGJu9Ul2A9R2zeG2xYhPHHi412gFLxhP3P70zi";
const char *mqtt_password = "";
int port = 1883;
const char *topicFromNodered = "Cadavos/Morales/Petalcorin/Pulvera/fromNodered";
const int PIRPin = 19;
const int LED = 5;
const int buzzPin = 18;
bool alarmOn = false;
unsigned long lastNotificationTime = 0;
String phoneNumber = "+359893439660"; // Your phone number
String apiKey = "8817832"; // Your API key
void setup() {
Serial.begin(115200);
pinMode(PIRPin, INPUT);
pinMode(LED, OUTPUT);
pinMode(buzzPin, OUTPUT);
setupWifi();
setupBlynk();
}
void setupWifi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void setupBlynk() {
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
while (Blynk.connect() == false) {}
Serial.println("Blynk connected");
}
void loop() {
Blynk.run();
handleMotionDetection();
}
void handleMotionDetection() {
bool motionDetected = digitalRead(PIRPin);
if (motionDetected) {
Serial.println("Motion detected!");
digitalWrite(LED, HIGH);
tone(buzzPin, 1000);
delay(500);
digitalWrite(LED, LOW);
noTone(buzzPin);
if (!alarmOn) {
alarmOn = true;
activateAlarm();
}
} else {
if (alarmOn) {
alarmOn = false;
}
}
}
void sendWhatsAppNotification() {
unsigned long currentTime = millis();
if (currentTime - lastNotificationTime >= 5000) { // Send notification every 5 seconds
lastNotificationTime = currentTime;
String message = "Alarm activated! Motion detected."; // Your message here
// Data to send with HTTP POST
String url = "https://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
HTTPClient http;
http.begin(url);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Send HTTP POST request
int httpResponseCode = http.POST(url);
if (httpResponseCode == 200) {
Serial.println("Message sent successfully");
} else {
Serial.println("Error sending the message");
Serial.print("HTTP response code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
}
void activateAlarm() {
tone(buzzPin, 1000); // Activate the buzzer
// Optionally, send a notification that the alarm has been manually activated
sendWhatsAppNotification();
// Set up a timer to trigger notifications every 5 seconds
lastNotificationTime = millis(); // Reset timer
}
BLYNK_WRITE(V1) {
int buttonState = param.asInt();
if (buttonState == HIGH) {
activateAlarm(); // Call a function to activate the alarm
digitalWrite(LED, HIGH); // Turn on the LED lamp
} else {
digitalWrite(LED, LOW); // Turn off the LED lamp
noTone(buzzPin); // Turn off the buzzer
}
}