#define BLYNK_TEMPLATE_ID "TMPL3IBLGDdC1"
#define BLYNK_TEMPLATE_NAME "Wifi Home Automation"
#define BLYNK_AUTH_TOKEN "n835Zo6II3nmh10KSWqg2fhyTnn8KT8J"
#define BLYNK_PRINT Serial
#include <WiFi.h> // add Wi-fi Library
#include <WiFiClient.h> // add Wi-fi Client Library
#include<BlynkSimpleEsp32.h> // add Blynk Esp32 Library
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; // set Wifi name
char pass[] = ""; // set passwaord
BlynkTimer timer;
//*******************************************************************************
// define Physical button pin connection on Arduino pins
#define button1_pin 25
#define button2_pin 33
#define button3_pin 32
#define button4_pin 35
// define Relay pin connection on Arduino pins
#define relay1_pin 12
#define relay2_pin 14
#define relay3_pin 27
#define relay4_pin 26
// set Relay state to 0 initially
int relay1_state = 0;
int relay2_state = 0;
int relay3_state = 0;
int relay4_state = 0;
// define Virtual Button pin for Blynk App (so Virtual Buttons can be changed according to Room)
#define button1_vpin V1
#define button2_vpin V2
#define button3_vpin V3
#define button4_vpin V0
// Blynk.Cloud request the letest state from the server
BLYNK_CONNECTED()
{
Blynk.syncVirtual(button1_vpin);
Blynk.syncVirtual(button2_vpin);
Blynk.syncVirtual(button3_vpin);
Blynk.syncVirtual(button4_vpin);
}
//when push button switched from Blynk App then Virtual pin state & Relay state changes
BLYNK_WRITE(button1_vpin)
{
relay1_state = param.asInt();
digitalWrite(relay1_pin , relay1_state);
}
BLYNK_WRITE(button2_vpin)
{
relay2_state = param.asInt();
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); // set Buad Rate to 1,15,200
//define buttons as Input
pinMode(button1_pin,INPUT_PULLUP);
pinMode(button2_pin,INPUT_PULLUP);
pinMode(button3_pin,INPUT_PULLUP);
pinMode(button4_pin,INPUT_PULLUP);
// define Relay as Output
pinMode(relay1_pin,OUTPUT);
pinMode(relay2_pin,OUTPUT);
pinMode(relay3_pin,OUTPUT);
pinMode(relay4_pin,OUTPUT);
//set Relay pin High initially
digitalWrite(relay1_pin,HIGH);
digitalWrite(relay2_pin,HIGH);
digitalWrite(relay3_pin,HIGH);
digitalWrite(relay4_pin,HIGH);
Blynk.begin(auth,ssid,pass); // fetching Token, Wifi name & Password
}
//***************************************************************************
void loop()
{
Blynk.run();
timer.run();
listen_push_buttons(); // get data from push buttons
}
//**************************************************************************
//defining listen_push_function here to get data from buttons
void listen_push_buttons()
{
if(digitalRead(button1_pin)==LOW)
{
delay(200);
control_relay(1);
Blynk.virtualWrite(button1_pin , relay1_state); // update button state
}
else if(digitalRead(button2_pin)==LOW) {
delay(200);
control_relay(2);
Blynk.virtualWrite(button2_pin , relay2_state);
}
else if(digitalRead(button3_pin)==LOW) {
delay(200);
control_relay(3);
Blynk.virtualWrite(button3_pin , relay3_state);
}
else if (digitalRead(button4_pin)==LOW) {
delay(200);
control_relay(4);
Blynk.virtualWrite(button4_pin , relay4_state);
}
}
//*****************************************************************
//define contro_relay function
void control_relay(int relay)
{
if(relay==1)
{
relay1_state =! relay1_state;
digitalWrite(relay1_pin , relay1_state);
Serial.print("Relay1 State");
Serial.print(relay1_state);
delay(50);
}
else if(relay==2) {
relay2_state =! relay2_state;
digitalWrite(relay2_pin , relay2_state);
Serial.print("Relay2 State");
Serial.print(relay2_state);
delay(50);
}
else if(relay==3) {
relay1_state =! relay3_state;
digitalWrite(relay3_pin , relay3_state);
Serial.print("Relay3 State");
Serial.print(relay3_state);
delay(50);
}
else if(relay==4) {
relay4_state =! relay4_state;
digitalWrite(relay4_pin , relay4_state);
Serial.print("Relay4 State");
Serial.print(relay4_state);
delay(50);
}
}