#define BLYNK_TEMPLATE_ID "TMPL3sW0gzb3r"
#define BLYNK_TEMPLATE_NAME "rely"
#define BLYNK_AUTH_TOKEN "9V2q650sI6EOtiY-efgYl3bOold-8DGc"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; // Replace with your Wi-Fi network name
char pass[] = ""; // Replace with your Wi-Fi network password
BlynkTimer timer;
const int RELAY_1_PIN = 27; // Replace with the actual pin number for relay 1
const int RELAY_2_PIN = 14; // Replace with the actual pin number for relay 2
class Relay {
public:
Relay(int pin) : pin_(pin), isOn_(false) {
pinMode(pin_, OUTPUT);
turnOff(); // Ensure the relay is initially off
}
void turnOn() {
if (!isOn_) {
digitalWrite(pin_, HIGH);
isOn_ = true;
}
}
void turnOff() {
if (isOn_) {
digitalWrite(pin_, LOW);
isOn_ = false;
}
}
bool isOn() {
return isOn_;
}
private:
int pin_;
bool isOn_;
};
Relay relay1(RELAY_1_PIN);
Relay relay2(RELAY_2_PIN);
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
while (Blynk.connect() == false) {
// Wait until connected
}
}
void loop() {
Blynk.run();
}
BLYNK_WRITE(V0) { // Virtual Pin V0 to control relay 1
int value = param.asInt();
if (value == 1) {
relay1.turnOn();
} else {
relay1.turnOff();
}
}
BLYNK_WRITE(V1) { // Virtual Pin V1 to control relay 2
int value = param.asInt();
if (value == 1) {
relay2.turnOn();
} else {
relay2.turnOff();
}
}
BLYNK_CONNECTED() {
// When Blynk connects, set the initial state of the virtual pins to 0 (OFF)
Blynk.virtualWrite(V0, 0);
Blynk.virtualWrite(V1, 0);
}