#define BLYNK_TEMPLATE_ID "TMPL3APtkBnmU"
#define BLYNK_TEMPLATE_NAME "New Template"
#define BLYNK_AUTH_TOKEN "LC1hnDjpCA94kJe4jG7-L2ROx-BPpWY8"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; // Replace with your Wi-Fi SSID
char pass[] = ""; // Replace with your Wi-Fi password
// Pins setup
#define SERVO_PIN 15
#define RELAY_PIN_1 16
#define RELAY_PIN_2 17
#define RELAY_PIN_3 18
Servo doorServo;
bool doorClosed = false;
BlynkTimer timer;
void setup() {
Serial.begin(115200);
doorServo.attach(SERVO_PIN);
pinMode(RELAY_PIN_1, OUTPUT);
pinMode(RELAY_PIN_2, OUTPUT);
pinMode(RELAY_PIN_3, OUTPUT);
digitalWrite(RELAY_PIN_1, LOW);
digitalWrite(RELAY_PIN_2, LOW);
digitalWrite(RELAY_PIN_3, LOW);
// Connect to Wi-Fi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
Blynk.begin(auth, ssid, pass);
// Setup a function to be called every second
timer.setInterval(1000L, checkDoorStatus);
}
void loop() {
Blynk.run();
timer.run();
}
void checkDoorStatus() {
int servoPosition = doorServo.read();
if (servoPosition > 90) { // Assuming > 90 degrees means door closed
if (!doorClosed) {
doorClosed = true;
Blynk.logEvent("door_closed", "Door is closed.");
}
} else {
doorClosed = false;
}
}
BLYNK_WRITE(V1) {
int pinValue = param.asInt();
digitalWrite(RELAY_PIN_1, pinValue);
}
BLYNK_WRITE(V2) {
int pinValue = param.asInt();
digitalWrite(RELAY_PIN_2, pinValue);
}
BLYNK_WRITE(V3) {
int pinValue = param.asInt();
digitalWrite(RELAY_PIN_3, pinValue);
}
BLYNK_WRITE(V4) {
int pinValue = param.asInt();
if (pinValue == 1) {
doorServo.write(180); // Close door
} else {
doorServo.write(0); // Open door
}
}