My code
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
#define W5100_CS 10
#define SDCARD_CS 4
const int relayPin1 = 22;
const int relayPin2 = 24;
const int relayPin3 = 26;
const int relayPin4 = 28;
const int relayPin5 = 30;
const int relayPin6 = 32;
const int relayPin7 = 34;
const int relayPin8 = 36;
const int btnPin1 = 23;
const int btnPin2 = 25;
const int btnPin3 = 27;
const int btnPin4 = 29;
const int btnPin5 = 31;
const int btnPin6 = 33;
const int btnPin7 = 35;
const int btnPin8 = 37;
SimpleTimer timer;
SimpleTimer timer_cnn;
void checkPhysicalButton1();
void checkPhysicalButton2();
void checkPhysicalButton3();
void checkPhysicalButton4();
void checkPhysicalButton5();
void checkPhysicalButton6();
void checkPhysicalButton7();
void checkPhysicalButton8();
int relayState1 = LOW;
int relayState2 = LOW;
int relayState3 = LOW;
int relayState4 = LOW;
int relayState5 = LOW;
int relayState6 = LOW;
int relayState7 = LOW;
int relayState8 = LOW;
int btnState1 = HIGH;
int btnState2 = HIGH;
int btnState3 = HIGH;
int btnState4 = HIGH;
int btnState5 = HIGH;
int btnState6 = HIGH;
int btnState7 = HIGH;
int btnState8 = HIGH;
BLYNK_CONNECTED() {
Blynk.syncVirtual(V0);
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
Blynk.syncVirtual(V3);
Blynk.syncVirtual(V4);
Blynk.syncVirtual(V5);
Blynk.syncVirtual(V6);
Blynk.syncVirtual(V7);
}
BLYNK_WRITE(V0) {
relayState1 = param.asInt();
digitalWrite(relayPin1, relayState1);
}
BLYNK_WRITE(V1) {
relayState2 = param.asInt();
digitalWrite(relayPin2, relayState2);
}
BLYNK_WRITE(V2) {
relayState3 = param.asInt();
digitalWrite(relayPin3, relayState3);
}
BLYNK_WRITE(V3) {
relayState4 = param.asInt();
digitalWrite(relayPin4, relayState4);
}
BLYNK_WRITE(V4) {
relayState5 = param.asInt();
digitalWrite(relayPin5, relayState5);
}
BLYNK_WRITE(V5) {
relayState6 = param.asInt();
digitalWrite(relayPin6, relayState6);
}
BLYNK_WRITE(V6) {
relayState7 = param.asInt();
digitalWrite(relayPin7, relayState7);
}
BLYNK_WRITE(V7) {
relayState8 = param.asInt();
digitalWrite(relayPin8, relayState8);
}
void checkPhysicalButton1()
{
if (digitalRead(btnPin1) == LOW) {
if (btnState1 != LOW) {
relayState1 = !relayState1;
digitalWrite(relayPin1, relayState1);
Blynk.virtualWrite(V0, relayState1);
}
btnState1 = LOW;
} else {
btnState1 = HIGH;
}
}
void checkPhysicalButton2()
{
if (digitalRead(btnPin2) == LOW) {
if (btnState2 != LOW) {
relayState2 = !relayState2;
digitalWrite(relayPin2, relayState2);
Blynk.virtualWrite(V1, relayState2);
}
btnState2 = LOW;
} else {
btnState2 = HIGH;
}
}
void checkPhysicalButton3()
{
if (digitalRead(btnPin3) == LOW) {
if (btnState3 != LOW) {
relayState3 = !relayState3;
digitalWrite(relayPin3, relayState3);
Blynk.virtualWrite(V2, relayState3);
}
btnState3 = LOW;
} else {
btnState3 = HIGH;
}
}
void checkPhysicalButton4()
{
if (digitalRead(btnPin4) == LOW) {
if (btnState4 != LOW) {
relayState4 = !relayState4;
digitalWrite(relayPin4, relayState4);
Blynk.virtualWrite(V3, relayState4);
}
btnState4 = LOW;
} else {
btnState4 = HIGH;
}
}
void checkPhysicalButton5()
{
if (digitalRead(btnPin5) == LOW) {
if (btnState5 != LOW) {
relayState5 = !relayState5;
digitalWrite(relayPin5, relayState5);
Blynk.virtualWrite(V4, relayState5);
}
btnState5 = LOW;
} else {
btnState5 = HIGH;
}
}
void checkPhysicalButton6()
{
if (digitalRead(btnPin6) == LOW) {
if (btnState6 != LOW) {
relayState6 = !relayState6;
digitalWrite(relayPin6, relayState6);
Blynk.virtualWrite(V5, relayState6);
}
btnState6 = LOW;
} else {
btnState6 = HIGH;
}
}
void checkPhysicalButton7()
{
if (digitalRead(btnPin7) == LOW) {
if (btnState7 != LOW) {
relayState7 = !relayState7;
digitalWrite(relayPin7, relayState7);
Blynk.virtualWrite(V6, relayState7);
}
btnState7 = LOW;
} else {
btnState7 = HIGH;
}
}
void checkPhysicalButton8()
{
if (digitalRead(btnPin8) == LOW) {
if (btnState8 != LOW) {
relayState8 = !relayState8;
digitalWrite(relayPin8, relayState8);
Blynk.virtualWrite(V7, relayState8);
}
btnState8 = LOW;
} else {
btnState8 = HIGH;
}
}
void reconnect(){
if(!Blynk.connected()){
Ethernet.begin(mac);
Blynk.connect(1000);
}
}
void setup()
{
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH);
Serial.begin(9600);
//Blynk.begin(auth);
Blynk.config(auth);
Blynk.connect();
timer_cnn.setInterval(30000L, reconnect);
int mytimeout = millis() / 1000;
while (Blynk.connect() == false) {
if((millis() / 1000) > mytimeout + 8)
{ // try for less than 9 seconds
break;
}
}
pinMode(relayPin1, OUTPUT);
pinMode(btnPin1, INPUT_PULLUP);
digitalWrite(relayPin1, relayState1);
timer.setInterval(100L, checkPhysicalButton1);
pinMode(relayPin2, OUTPUT);
pinMode(btnPin2, INPUT_PULLUP);
digitalWrite(relayPin2, relayState2);
timer.setInterval(100L, checkPhysicalButton2);
pinMode(relayPin3, OUTPUT);
pinMode(btnPin3, INPUT_PULLUP);
digitalWrite(relayPin3, relayState3);
timer.setInterval(100L, checkPhysicalButton3);
pinMode(relayPin4, OUTPUT);
pinMode(btnPin4, INPUT_PULLUP);
digitalWrite(relayPin4, relayState4);
timer.setInterval(100L, checkPhysicalButton4);
pinMode(relayPin5, OUTPUT);
pinMode(btnPin5, INPUT_PULLUP);
digitalWrite(relayPin5, relayState5);
timer.setInterval(100L, checkPhysicalButton5);
pinMode(relayPin6, OUTPUT);
pinMode(btnPin6, INPUT_PULLUP);
digitalWrite(relayPin6, relayState6);
timer.setInterval(100L, checkPhysicalButton6);
pinMode(relayPin7, OUTPUT);
pinMode(btnPin7, INPUT_PULLUP);
digitalWrite(relayPin7, relayState7);
timer.setInterval(100L, checkPhysicalButton7);
pinMode(relayPin8, OUTPUT);
pinMode(btnPin8, INPUT_PULLUP);
digitalWrite(relayPin8, relayState8);
timer.setInterval(100L, checkPhysicalButton8);
}
void loop()
{
timer.run();
if(Blynk.connected()) { Blynk.run(); }
}