#define BLYNK_TEMPLATE_ID "TMPL38gZieQFv"
#define BLYNK_TEMPLATE_NAME "door is open"
#define BLYNK_AUTH_TOKEN "-htSoTcwzOw3IE8GpsdgGKLlOi3wSie9"
#define BLYNK_PRINT Serial
#include <WiFi.h> // Use WiFi library for ESP32
#include <BlynkSimpleEsp32.h> // Blynk library for ESP32
char auth[] = BLYNK_AUTH_TOKEN ; // Replace with your Blynk token
char ssid[] = "Wokwi-GUEST"; // Replace with your WiFi SSID
char pass[] = ""; // Replace with your WiFi password
#define trigPin 18 // Trigger pin (use GPIO number for ESP32)
#define echoPin 19 // Echo pin (use GPIO number for ESP32)
#define buzzerPin 5 // Buzzer pin (use GPIO number for ESP32)
long duration;
int distance;
BlynkTimer timer;
// Function to measure distance using the ultrasonic sensor
void measureDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
if (distance < 10) { // Adjust the threshold for your door distance
// Door is open
digitalWrite(buzzerPin, HIGH); // Activate buzzer
Blynk.logEvent("Door is open!");
Blynk.logEvent(V1, "Door is open!"); // Send message to Blynk
} else {
// Door is closed
digitalWrite(buzzerPin, LOW); // Deactivate buzzer
Blynk.logEvent(V1, "Door is closed.");
}
}
void setup() {
Serial.begin(115200); // ESP32 uses 115200 baud rate for serial communication
Blynk.begin(auth, ssid, pass);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
// Set a timer to measure the distance every 1 second
timer.setInterval(1000L, measureDistance);
}
void loop() {
Blynk.run();
timer.run(); // Run the distance measurement at set intervals
}