#define BLYNK_TEMPLATE_ID "TMPL2eZYXYnAY"
#define BLYNK_TEMPLATE_NAME "iot power"
#define BLYNK_AUTH_TOKEN "KQDME0O0YjfR-4WlZq92S3iUFD9xzp9K"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP32WiFi.h>
#include <BlynkSimpleEsp32.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "WE_88ED6E8";
char pass[] = "m3x09811";
// GPIO pins to control
#define PIN_TO_CONTROL_1 5 // Example GPIO 5, which corresponds to D1 on NodeMCU
#define PIN_TO_CONTROL_2 12 // Example GPIO 12, which corresponds to D6 on NodeMCU
#define PIN_TO_CONTROL_3 4 // Example GPIO 4, which corresponds to D2 on NodeMCU
WidgetLED led1(V1);
WidgetLED led2(V2);
WidgetLED led3(V3);
BlynkTimer timer;
bool isOn1 = false; // Variable to store LED state for V1
bool isOn2 = false; // Variable to store LED state for V2
bool isOn3 = false; // Variable to store LED state for V3
int check = 0 ;
// V1 LED Widget
void blinkLedWidget1() {
if (isOn1) {
led1.on(); //turn on the switch on the app
Serial.println("LED Status: ON");
Serial.println(" ");
} else {
led1.off();
Serial.println("LED Status: OFF");
Serial.println(" ");
}
}
// V2 LED Widget
void blinkLedWidget2() {
if (isOn2) {
led2.on();
Serial.println("Rotation Direction: CW");
} else {
led2.off();
Serial.println("Rotation Direction: CCW");
}
}
// V3 LED Widget
void blinkLedWidget3() {
if (isOn3) {
led3.on();
Serial.println("MOTOR Status: ON");
Serial.println("*********");
Serial.println("*********");
} else {
led3.off();
Serial.println("MOTOR Status: OFF");
Serial.println("*********");
Serial.println("*********");
}
}
void setup() {
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); //BLYNK_AUTH_TOKEN: Token that is reserved for us in the cloud to communicate with
// Setting interval to send data to Blynk Cloud to 2000ms.
// It means that data will be sent every 2 second
timer.setInterval(2000L, blinkLedWidget1);
timer.setInterval(2000L, blinkLedWidget2);
timer.setInterval(2000L, blinkLedWidget3);
// Slider widget (V0) value change handler
Blynk.virtualWrite(V0, 0); // Initial value for the slider
Blynk.syncVirtual(V0); // Request slider value from the server
pinMode(PIN_TO_CONTROL_1, OUTPUT);
pinMode(PIN_TO_CONTROL_2, OUTPUT);
pinMode(PIN_TO_CONTROL_3, OUTPUT);
}
void loop() {
Blynk.run();
timer.run();
}
// This function will be called every time the LED widget in Blynk app
// writes values to the virtual pin V1.
BLYNK_WRITE(V1) {
int ledValue = param.asInt(); // Get value from the LED widget
// If LED is turned ON, set isOn1 to true
if (ledValue == 255) {
digitalWrite(PIN_TO_CONTROL_1, HIGH); // Set D1 HIGH first
delay(100); // Add a delay for stability
isOn1 = true;
}
// If LED is turned OFF, set isOn1 to false
else {
digitalWrite(PIN_TO_CONTROL_1, LOW);
delay(100); // Add a delay for stability
isOn1 = false;
}
}
// This function will be called every time the LED widget in Blynk app
// writes values to the virtual pin V2.
BLYNK_WRITE(V2) {
int ledValue = param.asInt(); // Get value from the LED widget
// If LED is turned ON, set isOn2 to true
if (ledValue == 255) {
delay(100); // Add a delay for stability
isOn2 = true;
check = 1 ;
}
// If LED is turned OFF, set isOn2 to false
else {
delay(100); // Add a delay for stability
isOn2 = false;
check = 0 ;
}
}
// This function will be called every time the LED widget in Blynk app
// writes values to the virtual pin V3.
BLYNK_WRITE(V3) {
int ledValue = param.asInt(); // Get value from the LED widget
// If LED is turned ON, set isOn2 to true
if (ledValue == 255) {
delay(100); // Add a delay for stability
isOn3 = true;
if(check == 1) {
digitalWrite(PIN_TO_CONTROL_2, HIGH);
digitalWrite(PIN_TO_CONTROL_3, LOW);
}else{
digitalWrite(PIN_TO_CONTROL_3, HIGH);
digitalWrite(PIN_TO_CONTROL_2, LOW);
}
}
// If LED is turned OFF, set isOn3 to false
else {
delay(100); // Add a delay for stability
isOn3 = false;
digitalWrite(PIN_TO_CONTROL_3, LOW);
digitalWrite(PIN_TO_CONTROL_2, LOW);
}
}
// Handler for slider widget value changes
BLYNK_WRITE(V0) {
int sliderValue = param.asInt(); // Get value from the slider widget
// If device is ON, turn it off
digitalWrite(PIN_TO_CONTROL_1, LOW);
led1.off();
//Serial.println("GPIO pin 5 turned OFF");
isOn1 = false;
// Turn on the GPIO pin after the specified time
sliderValue= sliderValue * 60; //turn the slider timer to minutes
if (sliderValue > 0) {
digitalWrite(PIN_TO_CONTROL_1, HIGH);
led1.on();
Serial.println("Timer is ON");
isOn1 = true;
while ( sliderValue > 0) {
delay(1000); // Wait for 1 second
sliderValue--;
Serial.println(sliderValue); //count down in the serial mointor
}
digitalWrite(PIN_TO_CONTROL_1, LOW);
led1.off();
Blynk.virtualWrite(V0, 0); // return the slider to 0
Serial.println("TImer is OFF");
delay(2000); // Wait for 2 seconds to read the status of timer in serial monitor
Serial.println("*********");
Serial.println("*********");
isOn1 = false;
}
}