False Positives. ESP 8266 nodemcu V3 Lolin

Hello everyone. I apologize in advance for my English, I write with the help of a translator))) Lighting system in the house, 2 lamps, 2 channel relay, 2 switches, relay power separately, controller power separately. Faced with the problem of false positives. The problem is the following, I connect the entire system, flash Nodemcu, everything works. But after some time, Nodemcu switches the relay itself, this can happen in 5 minutes, maybe in 1 hour. But the funny thing is, the whole system works fine during the day (2-3 false positives), and at night it goes crazy. Help me solve the problem. BLINK server, installed the latest library update.

#define BLYNK_PRINT Serial            
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define AUTH "04-ZbCvNAAfdfR1bN1RqLcixxmcQ1D7J"  // You should get Auth Token in the Blynk App.  
#define WIFI_SSID "MTSRouter-436FFB"                   //Enter Wifi Name MTSRouter-436FFB
#define WIFI_PASS "52848315"                   //Enter wifi Password 52848315
     
#define RELAY_PIN_3      D1  // пин к которому подключаем реле
#define RELAY_PIN_4      D5   
   
#define PUSH_BUTTON_3    D7  // пин к которому подключаем выключатель/кнопка
#define PUSH_BUTTON_4    D6   //

#define VPIN_BUTTON_3    V4 // виртуальный пин в БЛИНК к которому подключаем выключатель/кнопка
#define VPIN_BUTTON_4    V5  




BlynkTimer timer;

void checkPhysicalButton();

int relay1State = LOW; //тут добавляем по аналогии реле 1,2,3 и т.д.
int pushButton1State = HIGH; //тут добавляем по аналогии выключатель/кнопку 1,2,3 и т.д.

int relay2State = LOW;
int pushButton2State = HIGH;

int relay3State = LOW;
int pushButton3State = HIGH;

int relay4State = LOW;
int pushButton4State = HIGH;


BLYNK_CONNECTED() {

  // Request the latest state from the server
  
   Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);
  

  // Alternatively, you could override server state using:
 // Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
 // Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
 // Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
 // Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);

}

// When App button is pushed - switch the state


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 checkPhysicalButton()
{
    if (digitalRead(PUSH_BUTTON_3) == LOW) {
    // pushButton3State is used to avoid sequential toggles
    if (pushButton3State != LOW) {

      // Toggle Relay state
      relay3State = !relay3State;
      digitalWrite(RELAY_PIN_3, relay3State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
    }
    pushButton3State = LOW;
  } else {
    pushButton3State = HIGH;
  }

  if (digitalRead(PUSH_BUTTON_4) == LOW) {
    // pushButton4State is used to avoid sequential toggles
    if (pushButton4State != LOW) {

      // Toggle Relay state
      relay4State = !relay4State;
      digitalWrite(RELAY_PIN_4, relay4State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
    }
    pushButton4State = LOW;
  } else {
    pushButton4State = HIGH;
  }
}

void setup()
{

  Serial.begin(115200);
  Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS,"blynk-cloud.com");
  

 

  pinMode(RELAY_PIN_3, OUTPUT);
  pinMode(PUSH_BUTTON_3, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_3, relay3State);


  pinMode(RELAY_PIN_4, OUTPUT);
  pinMode(PUSH_BUTTON_4, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_4, relay4State);

  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton);// 

}

void loop()
{
  Blynk.run();
  timer.run();
}

```cpp

void loop()

Your voiud setup contains these lines…

As you also have these declarations…

whenever your device starts-up it will set the relay states to LOW.
At the first Blynk.run command the the BLYNK_CONNECTED() callback will trigger, which synchronises the relay states with your app button sates.

I suspect that your device is restarting from time to time, maybe because of a power supply issue?
You could easily check this by pushing the current uptime to your app and see if this falls back to a low level when you experience the issue.

Other things to look for are induced currents in the logic wires to the relays, loose connections, or environmental factors that could be causing issues with the device or the relays.

Pete.

Good afternoon. Can you tell me what to do if I refer to your answer? But to be honest, I just started to understand this area. I found a sketch that worked. The most interesting thing is that everything works on the breadboard without problems, but if you connect the relay to alternating current, problems begin. This firmware is installed on a mock-up sample without connecting to AC, it works without problems.As for the cues from the power supply, I tried to flash the sketch with the interrupt function, it really works with interference, if for example you turn on the vacuum cleaner, then the light turns on. With the sketch that c gave in the example, the situation is different, regardless of the state of the AC network, the relay is triggered. That is, from the moment the controller is connected, a different amount of time may pass before the failure. This problem is especially evident in the evening, I’m honestly skeptical about this. But in fact, for 6 days, during the day, the controller works acceptably, but in the evening, miracles begin)).

Once again I apologize for my English

Regarding the reboot of the ESP 8266, I also initially thought that it was about the power supply. But when forced to restart, the device keeps the state of the switch in its original position. I was monitoring the device at the time of the false alarm, the device does not reboot. It simply switches the GPIO to a different state. On the breadboard, I tried connecting the multimeter to the pins and restarting the device, but there was no change from the initial state. Problems start after installation in the system (lighting control). I also noticed an interesting fact, the wemos d1 board often switches the button (physical or virtual) to reboot, and the Lolin v3 board works without interruptions. I would like to deal with this situation.

Are your relays opto-isolated?
If so, then you probably need a suitable capacitor across the relay load terminals to filter-out the noise.

Pete.

I use a relay with an optocoupler RS817C

Pete.

PС817C optocoupler

What scheme should I connect it to? And which capacitor is better to use?I use this type of relay Модуль реле 5В 2-канала электромеханическое с опторазвязкой / Купить в RoboShop

Something like this across the relay load contacts…

Pete.

Thanks. I also noticed that when I disconnected the connector of the physical switch on the first controller, there were no failures. On the second controller, in the line ``timer. setInterval(100L, checkPhysicalButton);// (I understand this is a physical switch polling timer), I increased the time to 500L, and there were also fewer failures.

Hello. Today I tried to put the capacitor that you recommended, the situation has changed for the worse. Disconnecting the control wire from the physical switch, as well as the sketch changes that I described in the previous answer, did not affect the behavior of the controller, the false positives continued, I hurried with the analysis.