Light automation blynk buttons stops responding after few clicks

I have made home automation controller with MCP23017 IO expander, ULN2803 transistor array, NodeMCU board, TTL AC 220V Optocoupler Isolation Modules as light On/Off sensors, solid state relays and pushbuttons as light switches

Made custom board to connect it all.
For few times everything works great. I can switch lights on/off from Blynk app. Also the button state changes from on to off and back. Synced with real light status.
I can also turn on and off the lights using physical pushbuttons and the light status changes in Blynk app also. I can do this as many times I want but only from one side. I can also turn the lights on and off from my Samsung Galaxy watch app GearBlynk.

If I turn the light on with physical switch and then multiple times from the app, after few clicks in the app and again physical buttons, the buttons in app stops responding (they stays on or off in opposite state of lamp/sensor board input and does not change the state of lamp anymore). Only responding to the state according light status changed by physical switches.

ESP is connected to relays via MCP23017 and ULN2803. ESP communicates to MCP23017 via I2C. Relays simulate real pushbuttons for solid state relays ( 220V connected to relays, pushbuttons, lamps and sensor board. Relay board is made similar to this (, just ESP board placed on the PCB and connector for sensor board ( attached. The only way to get it working again is to reset the ESP board. Then it again works for some clickc

Code added:
I can provide schematics also, but i dont think it helps. The problem seems to be software not hardware related.

#include <TimeLib.h>            // Used by WidgetRTC.h
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include "SPI.h" //Why? Because library supports SPI and I2C connection
#include "Adafruit_MCP23017.h"

SimpleTimer timer;
// Lighting control pins and AC feedback
int feedback1;            // AC feedback variable
int feedback2;            // AC feedback variable

#define DEB    50

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxx";

Adafruit_MCP23017 mcp;

void setup()
  Blynk.begin(auth, ssid, pass);

  //Lets sync up all the store states for the V pins and load the appropriate variables listed below

 for (uint8 pin = 0; pin  < 8; pin ++) {

    mcp.pinMode(pin, OUTPUT);
    mcp.digitalWrite(pin, LOW);

  for (uint8 pin = 8; pin  < 15; pin ++) {

    mcp.pinMode(pin, INPUT);

  timer.setInterval(300, AC_detect);      // Check AC_detect for AC presence every 50 milliseconds

void loop()

void AC_detect()
  feedback1 = mcp.digitalRead(8); // Read the digital pin for an AC input

  // update SWITCH widget state only if AC input has changed since last check
  // this prevents widget switch bounce on the app device screen
  if (feedback1 != feedback2){
    Blynk.virtualWrite(V1, !feedback1);    
    feedback2 = feedback1;

BLYNK_WRITE(V1)  // Runs every-time switch widget is toggled - For Light 1
  int pinData = param.asInt(); 
  mcp.digitalWrite(1, pinData); // Toggle relay1
  Blynk.virtualWrite(V1, mcp.digitalRead(8));    // Store Light 1 relay state in V1