#define BLYNK_TEMPLATE_ID "TMPL6QPVkwvHh"
#define BLYNK_TEMPLATE_NAME "Home Automation"
#define BLYNK_AUTH_TOKEN "nTjTb-jz6Whs9XxHyp5K4pfOBmDFCC8v"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
#define button1_pin 33
#define button2_pin 32
#define led1 15
#define led2 2
int led1_state = 0;
int led2_state = 0;
//Change the virtual pins according the rooms
#define button1_vpin V0
#define button2_vpin V1
//------------------------------------------------------------------------------
// This function is called every time the device is connected to the Blynk.Cloud
// Request the latest state from the server
BLYNK_CONNECTED() {
Blynk.syncVirtual(button1_vpin);
Blynk.syncVirtual(button2_vpin);
}
//--------------------------------------------------------------------------
// This function is called every time the Virtual Pin state change
//i.e when web push switch from Blynk App or Web Dashboard
BLYNK_WRITE(button1_vpin) {
led1_state = param.asInt();
digitalWrite(led1, led1_state);
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button2_vpin) {
led2_state = param.asInt();
digitalWrite(led2, led2_state);
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
void setup()
{
// Debug console
Serial.begin(115200);
//--------------------------------------------------------------------
pinMode(button1_pin, INPUT_PULLUP);
pinMode(button2_pin, INPUT_PULLUP);
//--------------------------------------------------------------------
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
//--------------------------------------------------------------------
//During Starting all Relays should TURN OFF
//digitalWrite(relay1_pin, HIGH);
//digitalWrite(relay2_pin, HIGH);
//digitalWrite(relay3_pin, HIGH);
//digitalWrite(relay4_pin, HIGH);
//--------------------------------------------------------------------
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
//--------------------------------------------------------------------
//Blynk.virtualWrite(button1_vpin, relay1_state);
//Blynk.virtualWrite(button2_vpin, relay2_state);
//Blynk.virtualWrite(button3_vpin, relay3_state);
//Blynk.virtualWrite(button4_vpin, relay4_state);
//--------------------------------------------------------------------
}
void loop()
{
Blynk.run();
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
listen_push_buttons();
}
void listen_push_buttons(){
//--------------------------------------------------------------------------
if(digitalRead(button1_pin) == LOW)
{
delay(200);
control_led(1);
Blynk.virtualWrite(button1_vpin, led1_state); //update button state
}
//--------------------------------------------------------------------------
else if (digitalRead(button2_pin) == LOW)
{
delay(200);
control_led(2);
Blynk.virtualWrite(button2_vpin, led2_state); //update button state
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
}
void control_led(int led)
{
//------------------------------------------------
if(led == 1)
{
led1_state = !led1_state;
digitalWrite(led1, led1_state);
Serial.print("led1 State = ");
Serial.println(led1_state);
delay(50);
}
//------------------------------------------------
else if(led == 2)
{
led2_state = !led2_state;
digitalWrite(led2, led2_state);
delay(50);
}
//------------------------------------------------
//------------------------------------------------
}