#define BLYNK_TEMPLATE_ID "TMPL_BGP00-H"
#define BLYNK_TEMPLATE_NAME "SMART CURTAIN"
#define BLYNK_AUTH_TOKEN "3Qt7t4k_rDZb-_BNlwHYu3GaIibFq1Ex"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include<ESP32Servo.h>
int ldr = 26;
//int relay = D1; //connect with lamp
int servoPin = 5;
//int motorDir = D4;
int led = 27;
int ldrValue;
int selectMode;
Servo servo;
BlynkTimer timer;
WidgetLCD lcd(V5);
char auth [] = BLYNK_AUTH_TOKEN;
char ssid [] = "Wokwi-GUEST";
char pass [] = "";
BLYNK_CONNECTED ()
{
Blynk.syncVirtual(V0);
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
Blynk.syncVirtual(V3);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Blynk.begin(auth,ssid,pass);
pinMode(led, OUTPUT);
pinMode(ldr, INPUT);
servo.attach(servoPin, 500, 2400);
timer.setInterval(1000L, readLDR);
lcd.clear();
lcd.print(0,0, " ");
lcd.print(0,1, " ");
}
BLYNK_WRITE (V0) // mode selection
{
selectMode = param.asInt();
if (selectMode == 0) // auto mode
{
Serial.println("Auto MODE IS ACTIVATED!");
lcd.print(3,0, " ");
lcd.print(4,0, "AUTO MODE");
lcd.print(0,1, " ");
lcd.print(4,1, "ACTIVATED");
}
else if (selectMode == 1) // manual mode
{
Serial.println("MANUAL MODE IS ACTIVATED!");
lcd.print(3,0, " ");
lcd.print(3,0, "MANUAL MODE");
lcd.print(0,1, " ");
lcd.print(4,1, "ACTIVATED");
digitalWrite(led, LOW);
}
}
BLYNK_WRITE (V2) // lamp
{
int buttonState = param.asInt ();
if (buttonState == 1)
{
Serial.println("Lamp is ON!");
digitalWrite(led, HIGH);
lcd.print(2,1, " ");
lcd.print(5,1, "LAMP ON");
}
else if (buttonState == 0)
{
Serial.println("Lamp is OFF!");
digitalWrite(led, LOW);
lcd.print(5,1, "LAMP OFF");
}
}
BLYNK_WRITE (V3) //curtain open
{
int buttonState = param.asInt ();
if (buttonState == 1)
{
Serial.println("Opening the curtain!");
lcd.print(2,1, " ");
lcd.print(2,1, "CURTAIN OPEN");
curtainOpen();
}
else if (buttonState == 0)
{
Serial.println("Stop opening the curtain!");
lcd.print(2,1, " ");
lcd.print(2,1, "CURTAIN STOP");
curtainStop();
}
}
BLYNK_WRITE (V4) //curtain close
{
int buttonState = param.asInt ();
if (buttonState == 1)
{
Serial.println("Closing the curtain!");
lcd.print(2,1, " ");
lcd.print(2,1, "CURTAIN CLOSE");
curtainClose();
}
else if (buttonState == 0)
{
Serial.println("Stop Closing the curtain!");
lcd.print(2,1, " ");
lcd.print(2,1, "CURTAIN STOP");
curtainStop();
}
}
void readLDR ()
{
ldrValue = analogRead(ldr);
// Serial.println(ldrValue);
if (selectMode != 1)
{
if (ldrValue <400) // increase the value if the sensor is too sensitive
{
digitalWrite(led, LOW);
servo.write(180);
}
else
{
digitalWrite(led, HIGH);
servo.write(0);
}
}
}
void curtainOpen ()
{
servo.write(180);
}
void curtainClose ()
{
servo.write(90);
}
void curtainStop ()
{
servo.write(0);
}
void loop() {
Blynk.run();
timer.run();
}