#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Wi-Fi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Blynk authentication token
char auth[] = "ekdMe5rZOOP_JzO1j_wJc6ljVcqZ65E2";
// Pin for the button
const int buttonPin = 2;
// Pin for the buzzer
const int buzzerPin = 5;
// Pin for the LED
const int ledPin = 13;
// Blynk virtual pin for the alarm status
#define VIRTUAL_ALARM V0
// Variable to store the previous state of the button
int previousState = HIGH;
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Set the button pin as input with internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Set the buzzer pin as output
pinMode(buzzerPin, OUTPUT);
// Set the LED pin as output
pinMode(ledPin, OUTPUT);
// Initially turn off the buzzer and LED
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPin, LOW);
}
void loop() {
Blynk.run();
// Read the current state of the button
int currentState = digitalRead(buttonPin);
// Check if the button state has changed
if (currentState != previousState) {
if (currentState == HIGH) {
// Button was clicked
if (digitalRead(buzzerPin) == LOW) {
Serial.println("Perimeter breach detected!");
Blynk.virtualWrite(VIRTUAL_ALARM, HIGH); // Turn on the virtual alarm
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
Serial.println("Perimeter breach resolved.");
Blynk.virtualWrite(VIRTUAL_ALARM, LOW); // Turn off the virtual alarm
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
digitalWrite(ledPin, LOW); // Turn off the LED
}
}
previousState = currentState;
}
delay(100); // Delay for stability
}