// #define BLYNK_TEMPLATE_ID "TMPL63a3uzc5S"
// #define BLYNK_TEMPLATE_NAME "Home Automation"
// #define BLYNK_AUTH_TOKEN "tOU7oidMTjenfZ3KHd67rTBffV4hNb17"
// // 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 26
// // #define button2_pin 25
// // #define button3_pin 33
// #define button4_pin 32
// // #define relay1_pin 13
// // #define relay2_pin 12
// // #define relay3_pin 14
// #define relay4_pin 27
// // pir sensor pin and buzzer pin
// #define pir_sensor_pin 4
// // int relay1_state = 0;
// // int relay2_state = 0;
// // int relay3_state = 0;
// int relay4_state = 0;
// int motion_state = 0;
// // current state of pir sensors
// int pirCurrentState = LOW;
// int pirPreviousState = LOW;
// //Change the virtual pins according the rooms
// // #define button1_vpin V1
// // #define button2_vpin V2
// // #define button3_vpin V3
// #define sensor_vpin V4
// #define status_vpin V6
// //------------------------------------------------------------------------------
// // 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);
// // Blynk.syncVirtual(button3_vpin);
// Blynk.syncVirtual(sensor_vpin);
// Blynk.syncVirtual(status_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) {
// // 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(sensor_vpin) {
// motion_state = param.asInt();
// digitalWrite(pir_sensor_pin, motion_state);
// }
// //--------------------------------------------------------------------------
// void setup()
// {
// // Debug console
// 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);
// //--------------------------------------------------------------------
// pinMode(pir_sensor_pin, INPUT);
// //--------------------------------------------------------------------
// //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);
// //--------------------------------------------------------------------
// }
// 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!
// pirPreviousState = pirCurrentState;
// pirCurrentState = digitalRead(pir_sensor_pin);
// if (pirPreviousState == LOW && pirCurrentState == HIGH) {
// Serial.println("Motion detected!");
// // digitalWrite(relay4_pin, HIGH);
// digitalWrite(relay4_pin, HIGH);
// Blynk.virtualWrite(status_vpin, "Motion Detected");
// Blynk.logEvent("intruderalarm", "Intruder Detected !! " );
// }
// else
// if(pirPreviousState == HIGH && pirCurrentState == LOW) {
// Serial.println("Motion stopped!");
// // val = 0;
// // digitalWrite(relay4_pin, LOW);
// Blynk.virtualWrite(status_vpin, "Motion Stopped");
// }
// // timer.setInterval(5000L, intruderalarm);
// listen_push_buttons();
// }
// //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// void listen_push_buttons(){
// //--------------------------------------------------------------------------
// // if(digitalRead(button1_pin) == LOW){
// // delay(200);
// // control_relay(1);
// // Blynk.virtualWrite(button1_vpin, relay1_state); //update button state
// // }
// // //--------------------------------------------------------------------------
// // else if (digitalRead(button2_pin) == LOW){
// // delay(200);
// // control_relay(2);
// // Blynk.virtualWrite(button2_vpin, relay2_state); //update button state
// // }
// // //--------------------------------------------------------------------------
// // else if (digitalRead(button3_pin) == LOW){
// // delay(200);
// // control_relay(3);
// // Blynk.virtualWrite(button3_vpin, relay3_state); //update button state
// // }
// //--------------------------------------------------------------------------
// // else
// if (digitalRead(button4_pin) == LOW){
// delay(200);
// digitalWrite(relay4_pin, LOW);
// // Blynk.virtualWrite(button4_vpin, relay4_state); //update button state
// }
// //--------------------------------------------------------------------------
// }
// //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// // void control_relay(int relay){
// // //------------------------------------------------
// // // if(relay == 1){
// // // 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);
// // // delay(50);
// // // }
// // // //------------------------------------------------
// // // else if(relay == 3){
// // // relay3_state = !relay3_state;
// // // digitalWrite(relay3_pin, relay3_state);
// // // delay(50);
// // // }
// // //------------------------------------------------
// // // else
// // if(relay == 4){
// // relay4_state = LOW;
// // digitalWrite(relay4_pin, relay4_state);
// // delay(50);
// // }
// // //------------------------------------------------
// // }
// //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM