//#define BLYNK_TEMPLATE_ID
//#define BLYNK_DEVICE_NAME "HOME Automation" // name we are given to project
//#define BLYNK_AUTH_TOKEN // it is comes to you for registered email id..and we need to link with the project
#define BLYNK_TEMPLATE_ID "TMPL3JPrMR486"
#define BLYNK_TEMPLATE_NAME "Home Automation"
#define BLYNK_AUTH_TOKEN "VqkbWzeWaim-t8lzj3UTZQuiPrDHtf_j"
#define BLYNK_PRINT Serial // print in serial manner
#include<WiFi.h>// here we added the library
#include<WiFiClient.h>// when we join into the company we need to add the clent also that whom given this project to us.
#include<BlynkSimpleEsp32.h>// adding esp32 library..
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";// here we are creating wifi name and password..to the wifi and esp32 doesn't have directly wifi connection, we need to provide..
char pass[] = " ";
BlynkTimer timer;
#define button1_pin 26 // here we defining the push buttons
#define button2_pin 25
#define button3_pin 33
#define button4_pin 32
#define relay1_pin 13 // here we are defining the relay modules....
#define relay2_pin 12
#define relay3_pin 14
#define relay4_pin 27
int relay1_state = 0;// here we are making the state of every realy as zero because we need to start the relay from the first/start/initial stage..
int relay2_state = 0;
int relay3_state = 0;
int relay4_state = 0;
#define button1_vpin V1 // when ever we are creating a project with blynk we need to create the virtual pins...and it make sense when we started the application..
#define button2_vpin V2
#define button3_vpin V3
#define button4_vpin V4
BLYNK_CONNECTED(){ // here we connecting ..
Blynk.syncVirtual(button1_vpin);// here we are sync with the butttons
Blynk.syncVirtual(button2_vpin);
Blynk.syncVirtual(button3_vpin);
Blynk.syncVirtual(button4_vpin);
}
BLYNK_WRITE(button1_vpin){ // here we making sync betw the virtual pins with relay..
relay1_state = param.asInt();// here we got to know the exact state of relay either is on off or on.
digitalWrite(relay1_pin, relay1_state);
}
BLYNK_WRITE(button2_vpin){
relay2_state = param.asInt();// why we are write as int means we already defined with int in the lines...
digitalWrite(relay2_pin, relay2_state);
}
BLYNK_WRITE(button3_vpin){
relay3_state = param.asInt();
digitalWrite(relay3_pin, relay3_state);
}
BLYNK_WRITE(button4_vpin){
relay4_state = param.asInt();
digitalWrite(relay4_pin, relay4_state);
}
void setup(){
Serial.begin(115200);
pinMode(button1_pin, INPUT_PULLUP);
pinMode(button2_pin, INPUT_PULLUP);
pinMode(button3_pin, INPUT_PULLUP);
pinMode(button4_pin, INPUT_PULLUP);
pinMode(relay1_pin, OUTPUT);
pinMode(relay2_pin, OUTPUT);
pinMode(relay3_pin, OUTPUT);
pinMode(relay4_pin, OUTPUT);
digitalWrite(relay1_pin, HIGH);
digitalWrite(relay2_pin, HIGH);
digitalWrite(relay3_pin, HIGH);
digitalWrite(relay4_pin, HIGH);
Blynk.begin(auth, ssid, pass); //here we starting the blynk ..
}
void loop(){
Blynk.run();// while writing/taking the functions we can not use the semicolon we need to use colon...
timer.run();
listen_push_button();
}
void listen_push_button(){
if (digitalRead(button1_pin) == LOW){
delay(200);
control_relay(1);
Blynk.virtualWrite(button1_vpin, relay1_state);// it gives the confirmation that it is in off or on state..
}
else if(digitalRead(button2_pin) == LOW){
delay(200);
control_relay(2);
Blynk.virtualWrite(button2_vpin, relay2_state);
}else if(digitalRead(button3_pin) == LOW){
delay(200);
control_relay(3);
Blynk.virtualWrite(button3_vpin, relay3_state);
}else if(digitalRead(button4_pin) == LOW){
delay(200);
control_relay(4);
Blynk.virtualWrite(button4_vpin, relay4_state);
}
}
void control_relay(int relay){
if(relay == 1){ // if the condition true then it's on otherwise it wonn't be on..
relay1_state = !relay1_state;
digitalWrite(relay1_pin, relay1_state);
Serial.print("Relay1 state = ");
Serial.println(relay1_state);
delay(50);
}
else if (relay == 2){
relay2_state = !relay2_state;
digitalWrite(relay2_pin, relay2_state);
Serial.print("Relay2 state = ");
Serial.println(relay2_state);
delay(50);
}
else if (relay == 3){
relay3_state = !relay3_state;
digitalWrite(relay3_pin, relay3_state);
Serial.print("Relay3 state = ");
Serial.println(relay3_state);
delay(50);
}
else if (relay == 4){
relay4_state = !relay4_state;
digitalWrite(relay4_pin, relay4_state);
Serial.print("Relay4 state = ");
Serial.println(relay4_state);
delay(50);
}
}