#define BLYNK_TEMPLATE_ID "TMPL3EyaOJwFe"
#define BLYNK_TEMPLATE_NAME "Home curtain control"
#define BLYNK_AUTH_TOKEN "dRo7eGwyysWbhCMkQxcZFt3F-sAAxBY-"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <Stepper.h>
const int noofrev = 200;
const int button_pin = 14;
const int switchPin = 12;
Stepper mystepper(noofrev, 0, 2, 4, 5);
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
unsigned long previousMillis = 0;
const long interval = 1000;
bool switchState = HIGH;
void setup() {
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
mystepper.setSpeed(60);
Serial.println("Hello, ESP32!");
pinMode(button_pin, INPUT_PULLUP);
pinMode(switchPin, INPUT_PULLUP);
}
void loop() {
Blynk.run();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
bool currentSwitchState = digitalRead(switchPin);
if (currentSwitchState != switchState) {
switchState = currentSwitchState;
if (switchState == LOW) {
Serial.println("Opening curtain (Clockwise)");
mystepper.step(noofrev * 5);
} else {
Serial.println("Closing curtain (Counterclockwise)");
mystepper.step(-(noofrev * 5));
}
}
}
}
BLYNK_WRITE(V0) {
int pinValue = param.asInt();
if (pinValue == 1) {
Serial.println("Opening curtain (Clockwise) from Blynk");
mystepper.step(noofrev * 5);
Serial.println("Stepper motor step completed for opening");
}
}
BLYNK_WRITE(V1) {
int pinValue = param.asInt();
if (pinValue == 1) {
Serial.println("Closing curtain (Counterclockwise) from Blynk");
mystepper.step(-(noofrev * 5));
Serial.println("Stepper motor step completed for closing");
}
}