There is nothing to hide or no rocket science running in my code ! As i need time to reduce the code(only needed part) i was not able to sit for long time as i had my knee surgery.
Anyways i just made the code short. Code does not compile as there are no header files.
#define BLYNK_TEMPLATE_ID "xxxxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxxxx"
#include "ESP8266TimerInterrupt.h"
ESP8266Timer ITimer;
BlynkTimer mytimer;
#define TIMER_INTERVAL_MS 100
#define DEBOUNCE_TIME 25
#define VPIN_BUTTON_1 V6
#define VPIN_BUTTON_2 V7
#define VPIN_BUTTON_3 V8
#define VPIN_BUTTON_4 V9
#define RELAY_PIN_1 02
#define RELAY_PIN_2 15
#define RELAY_PIN_3 13
#define RELAY_PIN_4 16
#define PUSH_BUTTON_1 05
#define PUSH_BUTTON_2 04
#define PUSH_BUTTON_3 14
#define PUSH_BUTTON_4 12
volatile bool oldState1;
volatile bool oldState2;
volatile bool oldState3;
volatile bool oldState4;
volatile bool State1 = 0;
volatile bool State2 = 0;
volatile bool State3 = 0;
volatile bool State4 = 0;
volatile bool relay1State = LOW;
volatile bool relay2State = LOW;
volatile bool relay3State = LOW;
volatile bool relay4State = LOW;
void ICACHE_RAM_ATTR checkPhysicalButtonI() { // THIS IS WORKING PERFECT EVEN WHEN THE DEVICE IS TRYING TO
//CONNECT TO INTERNET OR EVEN WHEN NO WIFI IS AVAILABLE "NO LAG"
State1 = digitalRead(PUSH_BUTTON_1);
if (State1 != oldState1) {
oldState1 = State1;
relay1State = !relay1State;
digitalWrite(RELAY_PIN_1, relay1State);
}
State2 = digitalRead(PUSH_BUTTON_2);
if (State2 != oldState2) {
oldState2 = State2;
relay2State = !relay2State;
digitalWrite(RELAY_PIN_2, relay2State);
}
State3 = digitalRead(PUSH_BUTTON_3);
if (State3 != oldState3) {
oldState3 = State3;
relay3State = !relay3State;
digitalWrite(RELAY_PIN_3, relay3State);
}
State4 = digitalRead(PUSH_BUTTON_4);
if (State4 != oldState4) {
oldState4 = State4;
relay4State = !relay4State;
digitalWrite(RELAY_PIN_4, relay4State);
}
}
void setup() {
Serial.begin(115200);
delay(100);
pinMode(RELAY_PIN_1, OUTPUT);
pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
digitalWrite(RELAY_PIN_1, 0);
relay1State = 0;
pinMode(RELAY_PIN_2, OUTPUT);
pinMode(PUSH_BUTTON_2, INPUT_PULLUP);
digitalWrite(RELAY_PIN_2, 0);
relay2State = 0;
pinMode(RELAY_PIN_3, OUTPUT);
pinMode(PUSH_BUTTON_3, INPUT_PULLUP);
digitalWrite(RELAY_PIN_3, 0);
relay3State = 0;
pinMode(RELAY_PIN_4, OUTPUT);
pinMode(PUSH_BUTTON_4, INPUT_PULLUP);
digitalWrite(RELAY_PIN_4, 0);
relay4State = 0;
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, checkPhysicalButtonI))
Serial.println("Starting ITimer OK, millis() = " + String(millis()));
else
Serial.println("Can't set ITimer. Select another freq. or interval");
delay(1000);
mytimer.setInterval(500,checkPhysicalButton);
BlynkEdgent.begin();
}
BLYNK_WRITE(VPIN_BUTTON_1) {
relay1State = param.asInt();
digitalWrite(RELAY_PIN_1, relay1State);
}
BLYNK_WRITE(VPIN_BUTTON_2) {
relay2State = param.asInt();
digitalWrite(RELAY_PIN_2, relay2State);
}
BLYNK_WRITE(VPIN_BUTTON_3) {
relay3State = param.asInt();
digitalWrite(RELAY_PIN_3, relay3State);
}
BLYNK_WRITE(VPIN_BUTTON_4) {
relay4State = param.asInt();
digitalWrite(RELAY_PIN_4, relay4State);
}
void loop() {
BlynkEdgent.run();
mytimer.run(); // THIS WILL NEVER UPDATE THE SWITCH STATUS ON THE APP SIDE
//checkPhysicalButton(); THIS UPDATE THE SWITCH STATUS ON THE APP SIDE
}
void checkPhysicalButton() {
State1 = digitalRead(PUSH_BUTTON_1);
if (State1 != oldState1) {
oldState1 = State1;
relay1State = !relay1State;
digitalWrite(RELAY_PIN_1, relay1State);
Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
Blynk.syncVirtual(V6);
}
State2 = digitalRead(PUSH_BUTTON_2);
if (State2 != oldState2) {
oldState2 = State2;
relay2State = !relay2State;
digitalWrite(RELAY_PIN_2, relay2State);
Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
Blynk.syncVirtual(V7);
}
State3 = digitalRead(PUSH_BUTTON_3);
if (State3 != oldState3) {
oldState3 = State3;
relay3State = !relay3State;
digitalWrite(RELAY_PIN_3, relay3State);
Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
Blynk.syncVirtual(V8);
}
State4 = digitalRead(PUSH_BUTTON_4);
if (State4 != oldState4) {
oldState4 = State4;
relay4State = !relay4State;
digitalWrite(RELAY_PIN_4, relay4State);
Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
Blynk.syncVirtual(V9);
}
}
There are no header files needed to compile !! But you get the idea.
Placing the checkPhysicalButton()
inside the loop will work just fine but the ESP crashes once in a while. And keeping the void loop clean cannot be achieved !! But calling the same function with a timer will never the update the switch status on the app side.