/* **************************************************************************** * Preferences--> Aditional boards Manager URLs :
* For ESP32:
* https://dl.espressif.com/dl/package_esp32_index.json
*
* **************************************************************************** * Download Blynk Library (Version 1.0.1):
* https://github.com/blynkkk/blynk-library
******************************************************************************/
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "TMPL6dbhB9Y2B"
#define BLYNK_DEVICE_NAME "Kontrol Relay IOT"
#define BLYNK_AUTH_TOKEN "OBlAuUJnqhCKvMZX5TCsadYvYK9hbjwo"
// 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[] = "Redmi 9C";
char pass[] = "12345678";
BlynkTimer timer;
#define button1_pin 26
#define relay1_pin 13
int relay1_state = 0;
//Change the virtual pins according the rooms
#define button1_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);
}
//--------------------------------------------------------------------------
// 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);
}
void setup()
{
// Debug console
Serial.begin(115200);
//--------------------------------------------------------------------
pinMode(button1_pin, INPUT_PULLUP);
//--------------------------------------------------------------------
pinMode(relay1_pin, OUTPUT);
//--------------------------------------------------------------------
//During Starting all Relays should TURN OFF
digitalWrite(relay1_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);
//--------------------------------------------------------------------
}
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();
}
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
void listen_push_buttons(){
//--------------------------------------------------------------------------
if(digitalRead(button1_pin) == LOW){
delay(200);
control_relay(1);
Blynk.virtualWrite(button1_vpin, relay1_state); //update button state
}
//--------------------------------------------------------------------------
}
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
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);
}
}