HI,
i read that , i have a new code from my old friend but its not working properly ( switch automatically goes to on ) and my old friend is no more , so please help me .
#define BLYNK_DEVICE_NAME "MINING ON HOME"
#define BLYNK_AUTH_TOKEN "YS1rht2PTNeQ-bk88WTo07wkcOVYehuI"
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxx";
char pass[] = "xxxx";
bool fetch_blynk_state = true; //true or false
//#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// define the GPIO connected with Relays and switches
#define RelayPin1 23 //D23
#define RelayPin2 22 //D22
#define RelayPin3 21 //D21
#define RelayPin4 19 //D19
#define RelayPin5 18 //D18
#define RelayPin6 5 //D5
#define RelayPin7 25 //D25
#define RelayPin8 26 //D26
#define SwitchPin1 13 //D13
#define SwitchPin2 12 //D12
#define SwitchPin3 14 //D14
#define SwitchPin4 27 //D27
#define SwitchPin5 33 //D33
#define SwitchPin6 32 //D32
#define SwitchPin7 15 //D15
#define SwitchPin8 4 //D4
#define wifiLed 2 //D2
//Change the virtual pins according the rooms
#define VPIN_BUTTON_1 V1
#define VPIN_BUTTON_2 V2
#define VPIN_BUTTON_3 V3
#define VPIN_BUTTON_4 V4
#define VPIN_BUTTON_5 V5
#define VPIN_BUTTON_6 V6
#define VPIN_BUTTON_7 V7
#define VPIN_BUTTON_8 V8
#define VPIN_BUTTON_C V9
// Relay State
bool toggleState_1 = LOW; //Define integer to remember the toggle state for relay 1
bool toggleState_2 = LOW; //Define integer to remember the toggle state for relay 2
bool toggleState_3 = LOW; //Define integer to remember the toggle state for relay 3
bool toggleState_4 = LOW; //Define integer to remember the toggle state for relay 4
bool toggleState_5 = LOW; //Define integer to remember the toggle state for relay 5
bool toggleState_6 = LOW; //Define integer to remember the toggle state for relay 6
bool toggleState_7 = LOW; //Define integer to remember the toggle state for relay 7
bool toggleState_8 = LOW; //Define integer to remember the toggle state for relay 8
// Switch State
bool SwitchState_1 = LOW;
bool SwitchState_2 = LOW;
bool SwitchState_3 = LOW;
bool SwitchState_4 = LOW;
bool SwitchState_5 = LOW;
bool SwitchState_6 = LOW;
bool SwitchState_7 = LOW;
bool SwitchState_8 = LOW;
int wifiFlag = 0;
char auth[] = BLYNK_AUTH_TOKEN;
BlynkTimer timer;
// When App button is pushed - switch the state
BLYNK_WRITE(VPIN_BUTTON_1) {
toggleState_1 = param.asInt();
digitalWrite(RelayPin1, !toggleState_1);
}
BLYNK_WRITE(VPIN_BUTTON_2) {
toggleState_2 = param.asInt();
digitalWrite(RelayPin2, !toggleState_2);
}
BLYNK_WRITE(VPIN_BUTTON_3) {
toggleState_3 = param.asInt();
digitalWrite(RelayPin3, !toggleState_3);
}
BLYNK_WRITE(VPIN_BUTTON_4) {
toggleState_4 = param.asInt();
digitalWrite(RelayPin4, !toggleState_4);
}
BLYNK_WRITE(VPIN_BUTTON_5) {
toggleState_5 = param.asInt();
digitalWrite(RelayPin5, !toggleState_5);
}
BLYNK_WRITE(VPIN_BUTTON_6) {
toggleState_6 = param.asInt();
digitalWrite(RelayPin6, !toggleState_6);
}
BLYNK_WRITE(VPIN_BUTTON_7) {
toggleState_7 = param.asInt();
digitalWrite(RelayPin7, !toggleState_7);
}
BLYNK_WRITE(VPIN_BUTTON_8) {
toggleState_8 = param.asInt();
digitalWrite(RelayPin8, !toggleState_8);
}
void checkBlynkStatus() { // called every 2 seconds by SimpleTimer
bool isconnected = Blynk.connected();
if (isconnected == false) {
wifiFlag = 1;
Serial.println("Blynk Not Connected");
digitalWrite(wifiLed, LOW);
}
if (isconnected == true) {
wifiFlag = 0;
if (!fetch_blynk_state){
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
Blynk.virtualWrite(VPIN_BUTTON_5, toggleState_5);
Blynk.virtualWrite(VPIN_BUTTON_6, toggleState_6);
Blynk.virtualWrite(VPIN_BUTTON_7, toggleState_7);
Blynk.virtualWrite(VPIN_BUTTON_8, toggleState_8);
}
digitalWrite(wifiLed, HIGH);
Serial.println("Blynk Connected");
}
}
BLYNK_CONNECTED() {
// Request the latest state from the server
if (fetch_blynk_state){
Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
Blynk.syncVirtual(VPIN_BUTTON_3);
Blynk.syncVirtual(VPIN_BUTTON_4);
Blynk.syncVirtual(VPIN_BUTTON_5);
Blynk.syncVirtual(VPIN_BUTTON_6);
Blynk.syncVirtual(VPIN_BUTTON_7);
Blynk.syncVirtual(VPIN_BUTTON_8);
}
}
void manual_control()
{
if (digitalRead(SwitchPin1) == LOW && SwitchState_1 == LOW) {
digitalWrite(RelayPin1, LOW);
toggleState_1 = 1;
SwitchState_1 = HIGH;
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
Serial.println("Switch-1 on");
}
if (digitalRead(SwitchPin1) == HIGH && SwitchState_1 == HIGH) {
digitalWrite(RelayPin1, HIGH);
toggleState_1 = 0;
SwitchState_1 = LOW;
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
Serial.println("Switch-1 off");
}
if (digitalRead(SwitchPin2) == LOW && SwitchState_2 == LOW) {
digitalWrite(RelayPin2, LOW);
toggleState_2 = 1;
SwitchState_2 = HIGH;
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
Serial.println("Switch-2 on");
}
if (digitalRead(SwitchPin2) == HIGH && SwitchState_2 == HIGH) {
digitalWrite(RelayPin2, HIGH);
toggleState_2 = 0;
SwitchState_2 = LOW;
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
Serial.println("Switch-2 off");
}
if (digitalRead(SwitchPin3) == LOW && SwitchState_3 == LOW) {
digitalWrite(RelayPin3, LOW);
toggleState_3 = 1;
SwitchState_3 = HIGH;
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
Serial.println("Switch-3 on");
}
if (digitalRead(SwitchPin3) == HIGH && SwitchState_3 == HIGH) {
digitalWrite(RelayPin3, HIGH);
toggleState_3 = 0;
SwitchState_3 = LOW;
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
Serial.println("Switch-3 off");
}
if (digitalRead(SwitchPin4) == LOW && SwitchState_4 == LOW) {
digitalWrite(RelayPin4, LOW);
toggleState_4 = 1;
SwitchState_4 = HIGH;
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
Serial.println("Switch-4 on");
}
if (digitalRead(SwitchPin4) == HIGH && SwitchState_4 == HIGH) {
digitalWrite(RelayPin4, HIGH);
toggleState_4 = 0;
SwitchState_4 = LOW;
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
Serial.println("Switch-4 off");
}
if (digitalRead(SwitchPin5) == LOW && SwitchState_5 == LOW) {
digitalWrite(RelayPin5, LOW);
toggleState_5 = 1;
SwitchState_5 = HIGH;
Blynk.virtualWrite(VPIN_BUTTON_5, toggleState_5);
Serial.println("Switch-5 on");
}
if (digitalRead(SwitchPin5) == HIGH && SwitchState_5 == HIGH) {
digitalWrite(RelayPin5, HIGH);
toggleState_5 = 0;
SwitchState_5 = LOW;
Blynk.virtualWrite(VPIN_BUTTON_5, toggleState_5);
Serial.println("Switch-5 off");
}
if (digitalRead(SwitchPin6) == LOW && SwitchState_6 == LOW) {
digitalWrite(RelayPin6, LOW);
toggleState_6 = 1;
SwitchState_6 = HIGH;
Blynk.virtualWrite(VPIN_BUTTON_6, toggleState_6);
Serial.println("Switch-6 on");
}
if (digitalRead(SwitchPin6) == HIGH && SwitchState_6 == HIGH) {
digitalWrite(RelayPin6, HIGH);
toggleState_6 = 0;
SwitchState_6 = LOW;
Blynk.virtualWrite(VPIN_BUTTON_6, toggleState_6);
Serial.println("Switch-6 off");
}
if (digitalRead(SwitchPin7) == LOW && SwitchState_7 == LOW) {
digitalWrite(RelayPin7, LOW);
toggleState_7 = 1;
SwitchState_7 = HIGH;
Blynk.virtualWrite(VPIN_BUTTON_7, toggleState_7);
Serial.println("Switch-7 on");
}
if (digitalRead(SwitchPin7) == HIGH && SwitchState_7 == HIGH) {
digitalWrite(RelayPin7, HIGH);
toggleState_7 = 0;
SwitchState_7 = LOW;
Blynk.virtualWrite(VPIN_BUTTON_7, toggleState_7);
Serial.println("Switch-7 off");
}
if (digitalRead(SwitchPin8) == LOW && SwitchState_8 == LOW) {
digitalWrite(RelayPin8, LOW);
toggleState_8 = 1;
SwitchState_8 = HIGH;
Blynk.virtualWrite(VPIN_BUTTON_8, toggleState_8);
Serial.println("Switch-8 on");
}
if (digitalRead(SwitchPin8) == HIGH && SwitchState_8 == HIGH) {
digitalWrite(RelayPin8, HIGH);
toggleState_8 = 0;
SwitchState_8 = LOW;
Blynk.virtualWrite(VPIN_BUTTON_8, toggleState_8);
Serial.println("Switch-8 off");
}
}
BLYNK_WRITE(VPIN_BUTTON_C) {
all_SwitchOff();
}
void all_SwitchOff(){
toggleState_1 = 0; digitalWrite(RelayPin1, HIGH); Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1); delay(100);
toggleState_2 = 0; digitalWrite(RelayPin2, HIGH); Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2); delay(100);
toggleState_3 = 0; digitalWrite(RelayPin3, HIGH); Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3); delay(100);
toggleState_5 = 0; digitalWrite(RelayPin5, HIGH); Blynk.virtualWrite(VPIN_BUTTON_5, toggleState_5); delay(100);
toggleState_6 = 0; digitalWrite(RelayPin6, HIGH); Blynk.virtualWrite(VPIN_BUTTON_6, toggleState_6); delay(100);
toggleState_7 = 0; digitalWrite(RelayPin7, HIGH); Blynk.virtualWrite(VPIN_BUTTON_7, toggleState_7); delay(100);
toggleState_8 = 0; digitalWrite(RelayPin8, HIGH); Blynk.virtualWrite(VPIN_BUTTON_8, toggleState_8); delay(100);
}
void setup()
{
Serial.begin(9600);
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
pinMode(RelayPin5, OUTPUT);
pinMode(RelayPin6, OUTPUT);
pinMode(RelayPin7, OUTPUT);
pinMode(RelayPin8, OUTPUT);
pinMode(wifiLed, OUTPUT);
pinMode(SwitchPin1, INPUT_PULLUP);
pinMode(SwitchPin2, INPUT_PULLUP);
pinMode(SwitchPin3, INPUT_PULLUP);
pinMode(SwitchPin4, INPUT_PULLUP);
pinMode(SwitchPin5, INPUT_PULLUP);
pinMode(SwitchPin6, INPUT_PULLUP);
pinMode(SwitchPin7, INPUT_PULLUP);
pinMode(SwitchPin8, INPUT_PULLUP);
//During Starting all Relays should TURN OFF
digitalWrite(RelayPin1, !toggleState_1);
digitalWrite(RelayPin2, !toggleState_2);
digitalWrite(RelayPin3, !toggleState_3);
digitalWrite(RelayPin4, !toggleState_4);
digitalWrite(RelayPin5, !toggleState_5);
digitalWrite(RelayPin6, !toggleState_6);
digitalWrite(RelayPin7, !toggleState_7);
digitalWrite(RelayPin8, !toggleState_8);
digitalWrite(wifiLed, LOW);
//Blynk.begin(auth, ssid, pass);
WiFi.begin(ssid, pass);
timer.setInterval(2000L, checkBlynkStatus); // check if Blynk server is connected every 2 seconds
Blynk.config(auth);
delay(1000);
if (!fetch_blynk_state){
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
Blynk.virtualWrite(VPIN_BUTTON_5, toggleState_5);
Blynk.virtualWrite(VPIN_BUTTON_6, toggleState_6);
Blynk.virtualWrite(VPIN_BUTTON_7, toggleState_7);
Blynk.virtualWrite(VPIN_BUTTON_8, toggleState_8);
}
}
void loop()
{
manual_control();
Blynk.run();
timer.run();
}```