#define BLYNK_TEMPLATE_ID "TMPL6q8k08Nb5"
#define BLYNK_TEMPLATE_NAME "sending data"
#define BLYNK_AUTH_TOKEN "HfYLJFDjDYwPgtK-a5ig2XCJxLzVaFwX"
#include <BlynkSimpleEsp32.h>
const int pirPin = 2; // Connect PIR sensor to GPIO 2
const int ledPin = 13; // Connect LED to GPIO 13
const int pinSpeaker = 12; // Connect Buzzer to GPIO 10
boolean isActivated = false;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(pinSpeaker, OUTPUT);
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
// Initialize the state of the components
digitalWrite(ledPin, LOW);
noTone(pinSpeaker);
}
void loop() {
Blynk.run();
if (isActivated)
{
int pirState = digitalRead(pirPin);
if (pirState == HIGH)
{
Serial.println("Motion detected!");
Blynk.virtualWrite(V3, "Motion Detected!");
digitalWrite(ledPin, HIGH); // Turn on the LED
tone(pinSpeaker, 500); // Activate the buzzer
} else
{
Serial.println("Motion ended!");
Blynk.virtualWrite(V3, "Motion Ended!");
digitalWrite(ledPin, LOW); // Turn off the LED
noTone(pinSpeaker); // Deactivate the buzzer
}
}
delay(500); // Adjust delay as needed
}
BLYNK_WRITE(V1)
{
int buttonState = param.asInt();
if (buttonState == 1)
{
isActivated = !isActivated;
Blynk.virtualWrite(V0, isActivated ? 255 : 0); // Control LED on V0
Blynk.virtualWrite(V2, isActivated ? 255 : 0); // Control Buzzer on V2
Blynk.virtualWrite(V3, isActivated ? 255 : 0); // Control Motion on V3
}
}