#define BLYNK_TEMPLATE_ID "TMPL6p7xr4r3q"
#define BLYNK_TEMPLATE_NAME "smart garage door"
#define BLYNK_AUTH_TOKEN "l0ZcWvUCdCRfmoQxhmySS1GF08CjNxRa"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
#include <Ultrasonic.h>
char auth[] = "l0ZcWvUCdCRfmoQxhmySS1GF08CjNxR";
char ssid[] = "OCHI~SAN";
char password[] = "12345678";
// Define pin connections
const int servoPin = 18;
const int ultrasonicTrigPin = 12;
const int ultrasonicEchoPin = 5;
const int ledOpenPin = 19;
const int ledClosePin = 21;
const int buttonOpenPin = 14;
const int buttonClosePin = 2;
// Initialize servo, ultrasonic sensor, and LEDs
Servo servo;
Ultrasonic ultrasonic(12, 5);
bool doorOpen = false;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status()!= WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize Blynk
Blynk.begin(auth);
// Initialize servo and LEDs
servo.attach(18);
pinMode(19, OUTPUT);
pinMode(21, OUTPUT);
pinMode(19, INPUT);
pinMode(21, INPUT);
}
void loop() {
// Read ultrasonic sensor data
int distance = ultrasonic.read();
// Check if car is approaching
if (distance < 20 &&!doorOpen) {
// Open garage door
servo.write(90);
digitalWrite(19, HIGH);
doorOpen = true;
} else if (distance > 20 && doorOpen) {
// Close garage door
servo.write(0);
digitalWrite(21, HIGH);
doorOpen = false;
}
// Check manual override buttons
if (digitalRead(14) == HIGH) {
// Open garage door
servo.write(90);
digitalWrite(19, HIGH);
doorOpen = true;
} else if (digitalRead(2) == HIGH) {
// Close garage door
servo.write(0);
digitalWrite(21, HIGH);
doorOpen = false;
}
// Update Blynk app
Blynk.run();
delay(50);
}