Relay : green led and red led

My psychic powers don’t extend to understanding exactly what this means :frowning_face:

Pete.

I know you don’t like videos but it’s easier for you to understand my problem.
Don’t forget to put on some sound

Try updating the button with the exact state of relais.

 if (digitalRead(btn1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      relais1Sate = !relais1Sate;
      digitalWrite(relais1, relais1Sate);
     
      digitalWrite(ledV1, relais1Sate);
      digitalWrite(ledR1, !relais1Sate);


      // Update Button Widget
      Blynk.virtualWrite(V1, relais1Sate);              /////////////////try changing to exact state
     

    }
    btn1State = LOW;
  } else {
    btn1State = HIGH;

Then on the app if it is not correct go into the settings of the button and invert the 1 and 0.

I think your !(not) relais statements are getting you confused.

You were previously inverting the ledxState value (which was identical to relaisxSate) the with a NOT (!)
To make this work correctly, your new code needs to do this:

      relaisxSate = !relaisxSate;
      digitalWrite(relaisx, relaisxSate);
      ledVxState = !ledVxState;
      digitalWrite(ledVx, !ledVxState);
      digitalWrite(ledx4, ledx4State);

Pete.

thank you for your help, the synchronization is not yet optimal. I also have conection and deconection problem.
look on my phone.

here is my last sketch

#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#include <SPI.h>
#include <Wire.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

BlynkTimer timer;

char auth[] = "xxxxxxxxxxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxx";

// Set your LED and physical button pins here
const int relais1 = 14;
const int btn1 = 27;
const int ledV1 = 5;
const int ledR1 = 2;


const int relais2 = 32;
const int btn2 = 26;
const int ledV2 = 18;
const int ledR2 = 4;


const int relais3 = 15;
const int btn3 = 25;
const int ledV3 = 19;
const int ledR3 = 16;

const int relais4 = 12;
const int btn4 = 33;
const int ledV4 = 17;
const int ledR4 = 13;

// one wire temperature
#define ONE_WIRE_BUS 23
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int deviceCount = 3;
float tempC;






void checkPhysicalButton1();

void temperature();


int relais1Sate = LOW;
int btn1State = HIGH;
int ledV1State = LOW;
int ledR1State = LOW;

int relais2Sate = LOW;
int btn2State = HIGH;
int ledV2State = LOW;
int ledR2State = LOW;

int relais3Sate = LOW;
int btn3State = HIGH;
int ledV3State = LOW;
int ledR3State = LOW;

int relais4Sate = LOW;
int btn4State = HIGH;
int ledV4State = LOW;
int ledR4State = LOW;

int lcd_position[] = {16, 30, 44}; // position temperature sur l'écran


BLYNK_WRITE(V1) {
  relais1Sate = param.asInt();
  digitalWrite(relais1, relais1Sate);
  digitalWrite(ledV1, relais1Sate);
  digitalWrite(ledR1, !relais1Sate);

}

BLYNK_WRITE(V2) {
  relais2Sate = param.asInt();
  digitalWrite(relais2, relais2Sate);
  digitalWrite(ledV2, relais2Sate);
  digitalWrite(ledR2, !relais2Sate);

}

BLYNK_WRITE(V3) {
  relais3Sate = param.asInt();
  digitalWrite(relais3, relais3Sate);
  digitalWrite(ledV3, relais3Sate);
  digitalWrite(ledR3, !relais3Sate);

}

BLYNK_WRITE(V4) {
  relais4Sate = param.asInt();
  digitalWrite(relais4, relais4Sate);
  digitalWrite(ledV4, relais4Sate);
  digitalWrite(ledR4, !relais4Sate);

}

void temperature()
{
  display.clearDisplay();
  display.display();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);


  display.setCursor(0, 0);
  display.println("---------------------");
  display.setCursor(0, 6);
  display.print("|    Temperature    |");
  display.setCursor(0, 12);
  display.println("---------------------");

  display.setCursor(0, 16); // position 0
  display.println(" Interieur  |");
  display.print("---------------------");
  display.display();

  display.setCursor(0, 30); // position 1
  display.println(" Exterrieur |");
  display.print("---------------------");
  display.display();

  display.setCursor(0, 44); // position 2
  display.println(" Eau        |");
  display.print("---------------------");
  display.display();




  // Send command to all the sensors for temperature conversion
  sensors.requestTemperatures();

  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++)
  {

    tempC = sensors.getTempCByIndex(i);
    Blynk.virtualWrite(i + 21, tempC);
    display.setCursor(80, lcd_position[i]);
    display.print(tempC);
    display.display();

  }
}



void checkPhysicalButton1()
{
  if (digitalRead(btn1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      relais1Sate = !relais1Sate;
      digitalWrite(relais1, relais1Sate);
      ledV1State = !ledV1State;
      digitalWrite(ledV1, !ledV1State);
      digitalWrite(ledR1, ledV1State);


      // Update Button Widget
      Blynk.virtualWrite(V1, !relais1Sate);
     

    }
    btn1State = LOW;
  } else {
    btn1State = HIGH;



  }

  if (digitalRead(btn2) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn2State != LOW) {

      // Toggle LED state
      relais2Sate = !relais2Sate;
      digitalWrite(relais2, relais2Sate);
      ledV2State = !ledV2State;
      digitalWrite(ledV2, !ledV2State);
      digitalWrite(ledR2, ledV2State);

      // Update Button Widget
      Blynk.virtualWrite(V2, !relais2Sate);
     

    }
    btn2State = LOW;
  } else {
    btn2State = HIGH;

  }

  if (digitalRead(btn3) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn3State != LOW) {

      // Toggle LED state
      relais3Sate = !relais3Sate;
      digitalWrite(relais3, relais3Sate);
      ledV3State = !ledV3State;
      digitalWrite(ledV3, !ledV3State);
      digitalWrite(ledR3, ledV3State);

      // Update Button Widget
      Blynk.virtualWrite(V3, !relais3Sate);


    }
    btn3State = LOW;
  } else {
    btn3State = HIGH;
  }

  if (digitalRead(btn4) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn4State != LOW) {

      // Toggle LED state
      relais4Sate = !relais4Sate;
      digitalWrite(relais4, relais4Sate);
      ledV4State = !ledV4State;
      digitalWrite(ledV4, !ledV4State);
      digitalWrite(ledR4, ledV4State);

      // Update Button Widget
      Blynk.virtualWrite(V4, !relais4Sate);

      

    }
    btn4State = LOW;
  } else {
    btn4State = HIGH;
  }
}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
  Blynk.syncVirtual(V4);

}



void setup()
{
  sensors.begin();  // debut de la librairie capteur de température
  // Debug console
  Serial.begin(9600);
  WiFi.begin(ssid, pass);
  //timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds
  Blynk.config(auth);

  pinMode(relais1, OUTPUT);
  pinMode(btn1, INPUT_PULLUP);
  digitalWrite(relais1, relais1Sate);
  pinMode(ledV1, OUTPUT);
  pinMode(ledR1, OUTPUT);

  pinMode(relais2, OUTPUT);
  pinMode(btn2, INPUT_PULLUP);
  digitalWrite(relais2, relais2Sate);
  pinMode(ledV2, OUTPUT);
  pinMode(ledR2, OUTPUT);

  pinMode(relais3, OUTPUT);
  pinMode(btn3, INPUT_PULLUP);
  digitalWrite(relais3, relais3Sate);
  pinMode(ledV3, OUTPUT);
  pinMode(ledR3, OUTPUT);

  pinMode(relais4, OUTPUT);
  pinMode(btn4, INPUT_PULLUP);
  digitalWrite(relais4, relais4Sate);
  pinMode(ledV4, OUTPUT);
  pinMode(ledR4, OUTPUT);


  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  // Clear the buffer
  display.clearDisplay();
  display.display();


  timer.setInterval(100L, checkPhysicalButton1);

  timer.setInterval(10000L, temperature);

}



void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

does anyone have any idea how to improve my code?

I’d suggest that you start by describing g the problems you’re having, rather than posting videos of them
Adding serial print statements to your code at key points will also allow you to track the state of your variables and the actions that are meant to be triggered at each step.

Pete.

I just found this code. Do you think that I would have less problem by modifying my code by creating “CASE 1” “CASE 2” “CASE 3” “CASE 4” for my relays

#include <BlynkSimpleEsp8266.h> 

// define the GPIO connected with Relays and switches
#define RelayPin1 5  //D1
#define RelayPin2 4  //D2
#define RelayPin3 14 //D5
#define RelayPin4 12 //D6

#define SwitchPin1 10  //SD3
#define SwitchPin2 0   //D3 
#define SwitchPin3 13  //D7
#define SwitchPin4 3   //RX

#define wifiLed   16   //D0

//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

int toggleState_1 = 1; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 1; //Define integer to remember the toggle state for relay 2
int toggleState_3 = 1; //Define integer to remember the toggle state for relay 3
int toggleState_4 = 1; //Define integer to remember the toggle state for relay 4

int wifiFlag = 0;

#define AUTH "AUTH TOKEN"                 // You should get Auth Token in the Blynk App.  
#define WIFI_SSID "Wifi Name"             //Enter Wifi Name
#define WIFI_PASS "Wifi Password"         //Enter wifi Password

BlynkTimer timer;

void relayOnOff(int relay){
    switch(relay){
      case 1: 
             if(toggleState_1 == 1){
              digitalWrite(RelayPin1, LOW); // turn on relay 1
              toggleState_1 = 0;
              Serial.println("Device1 ON");
              }
             else{
              digitalWrite(RelayPin1, HIGH); // turn off relay 1
              toggleState_1 = 1;
              Serial.println("Device1 OFF");
              }
             delay(100);
      break;
      case 2: 
             if(toggleState_2 == 1){
              digitalWrite(RelayPin2, LOW); // turn on relay 2
              toggleState_2 = 0;
              Serial.println("Device2 ON");
              }
             else{
              digitalWrite(RelayPin2, HIGH); // turn off relay 2
              toggleState_2 = 1;
              Serial.println("Device2 OFF");
              }
             delay(100);
      break;
      case 3: 
             if(toggleState_3 == 1){
              digitalWrite(RelayPin3, LOW); // turn on relay 3
              toggleState_3 = 0;
              Serial.println("Device3 ON");
              }
             else{
              digitalWrite(RelayPin3, HIGH); // turn off relay 3
              toggleState_3 = 1;
              Serial.println("Device3 OFF");
              }
             delay(100);
      break;
      case 4: 
             if(toggleState_4 == 1){
              digitalWrite(RelayPin4, LOW); // turn on relay 4
              toggleState_4 = 0;
              Serial.println("Device4 ON");
              }
             else{
              digitalWrite(RelayPin4, HIGH); // turn off relay 4
              toggleState_4 = 1;
              Serial.println("Device4 OFF");
              }
             delay(100);
      break;
      default : break;      
      }  
}

void with_internet(){
    //Manual Switch Control
    if (digitalRead(SwitchPin1) == LOW){
      delay(200);
      relayOnOff(1); 
      Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);   // Update Button Widget  
    }
    else if (digitalRead(SwitchPin2) == LOW){
      delay(200);
      relayOnOff(2);      
      Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);   // Update Button Widget
    }
    else if (digitalRead(SwitchPin3) == LOW){
      delay(200);
      relayOnOff(3);
      Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);   // Update Button Widget
    }
    else if (digitalRead(SwitchPin4) == LOW){
      delay(200);
      relayOnOff(4);
      Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);   // Update Button Widget
    }
}
void without_internet(){
    //Manual Switch Control
    if (digitalRead(SwitchPin1) == LOW){
      delay(200);
      relayOnOff(1);      
    }
    else if (digitalRead(SwitchPin2) == LOW){
      delay(200);
      relayOnOff(2);
    }
    else if (digitalRead(SwitchPin3) == LOW){
      delay(200);
      relayOnOff(3);
    }
    else if (digitalRead(SwitchPin4) == LOW){
      delay(200);
      relayOnOff(4);
    }
}

// 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);
}

void checkBlynkStatus() { // called every 3 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false) {
    wifiFlag = 1;
  }
  if (isconnected == true) {
    wifiFlag = 0;
    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);
  }
}
void setup()
{
  Serial.begin(9600);

  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);

  pinMode(wifiLed, OUTPUT);

  pinMode(SwitchPin1, INPUT_PULLUP);
  pinMode(SwitchPin2, INPUT_PULLUP);
  pinMode(SwitchPin3, INPUT_PULLUP);
  pinMode(SwitchPin4, 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(wifiLed, HIGH);

  WiFi.begin(WIFI_SSID, WIFI_PASS);
  timer.setInterval(3000L, checkBlynkStatus); // check if Blynk server is connected every 3 seconds
  Blynk.config(AUTH);
  delay(1000);

  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);
}

void loop()
{  
  if (WiFi.status() != WL_CONNECTED)
  {
    //Serial.println("WiFi Not Connected");
    digitalWrite(wifiLed, HIGH);
  }
  else
  {
    //+Serial.println("WiFi Connected");
    digitalWrite(wifiLed, LOW); //Turn on WiFi LED
    Blynk.run();
  }

  timer.run(); // Initiates SimpleTimer
  if (wifiFlag == 0)
    with_internet();
  else
    without_internet();
}

We have a saying in English about “throwing the baby out with the bathwater”.
I’m not sure if there is a french equivalent.

As I said before…

Pete.

Hi Pete,

I will try and explain it in words as i am not really fluent in English. So what is happening is that when I switch on the ESP32, the synchronisation takes a couple of second for it to match with the app on my phone. Then on the app, there’s a pop-up saying “one device connected” and following this, a couple secs later, new pop-up appears with message “disconnected”.
Once everything is switch on, the syncronisation between the app and the ESP32 matches regarding “on=green light” and “off=red light”. But when I press the buttons mannually on the ESP32, it looses the syncronisation after two or three clicks, taking note as well that it seems that this glitch is random which means sometimes it stays synchronised and sometime it doesnt and that applies for all the manual buttons on the ESP32.
Now when I start by manually switching on or off the buttons on the EPS32 but then switch to the app, there’s one click delay, find example below the summary of the video i have sent through in words:
On EPS32, all buttons are off with synchronisation on the app. Pop-up appears with “one device connected” then a few secs later another pop-up with “disconnected”. But synchronisation between the app and ESP32 is done and what’s appearing on the app is reflected on the ESP32 so since on the app it’s “on”, all the lights changes from RED to GREEN.
Once you click on one of the button manually on the ESP32 (note that now all the buttons are GREEN as they are ON), the light on the ESP32 turns RED but on the app it doesnt change and still indicates ON. Another click and the light on the ESP32 turn back to GREEN and on the app it shows “OFF”. Now going on the app, I switch the same button to “ON” and the light on the ESP32 turn RED then with a delay of 2 secs, it switches back to GREEN.
From my observation, it seems that the communication between the app and the ESP32 is not reliable and it looses it connection somewhere or somehow thus having those glitches appers when doing the testing.
Hope this helps you understand and thanks for your help.

How many devices do you have in your project, and do you have any other hardware that is using the same Auth token?

When you’re writing a description you need to refer to physical buttons versus widget buttons, and it would help if you focussed on just one pair of physical/widget buttons, if you are able to demonstrate the issue in that way.
But, as I said before, you need to add some serial print statements into your code and observe what is happening to the various variables you are using, and what pieces of code are being executed, as you press these physical/widget buttons.
Serial debugging is the most powerful tool you have available to you, and you aren’t making use of it.

Pete.

I only have one device with this Auth token

I try the serial print statements

here is my first serial print statement.
Just a quick question. is it normal that my esp 32 starts to read my sensors and me physical button while it is still not connected to BLYNK

13:52:16.100 ->     ___  __          __
13:52:16.100 ->    / _ )/ /_ _____  / /__
13:52:16.133 ->   / _  / / // / _ \/  '_/
13:52:16.166 ->  /____/_/\_, /_//_/_/\_\
13:52:16.200 ->         /___/ v0.6.1 on ESP32
13:52:16.233 -> 
13:52:16.233 -> [197] Connecting to blynk-cloud.com:80
13:52:16.267 -> Low physical yelow button
13:52:16.301 -> Low physical bleu button
13:52:16.334 -> LOW physical green button
13:52:16.369 -> Low physical white button
13:52:16.369 -> check physical button finish
13:52:16.405 -> check physical button finish
13:52:16.437 -> check physical button finish
13:52:16.539 -> check physical button finish
13:52:16.640 -> check physical button finish
13:52:16.742 -> check physical button finish
13:52:16.843 -> check physical button finish
13:52:16.910 -> check physical button finish
13:52:17.014 -> check physical button finish
13:52:17.114 -> check physical button finish
13:52:17.216 -> check physical button finish
13:52:17.318 -> check physical button finish
13:52:17.421 -> check physical button finish
13:52:17.525 -> check physical button finish
13:52:17.627 -> check physical button finish
13:52:17.728 -> check physical button finish
13:52:17.828 -> check physical button finish
13:52:17.928 -> check physical button finish
13:52:18.030 -> check physical button finish
13:52:18.130 -> check physical button finish
13:52:18.231 -> check physical button finish
13:52:18.332 -> check physical button finish
13:52:18.434 -> check physical button finish
13:52:18.535 -> check physical button finish
13:52:18.636 -> check physical button finish
13:52:18.737 -> check physical button finish
13:52:18.838 -> check physical button finish
13:52:18.939 -> check physical button finish
13:52:19.042 -> check physical button finish
13:52:19.110 -> check physical button finish
13:52:19.212 -> check physical button finish
13:52:19.313 -> check physical button finish
13:52:19.415 -> check physical button finish
13:52:19.516 -> check physical button finish
13:52:19.619 -> check physical button finish
13:52:19.721 -> check physical button finish
13:52:19.822 -> check physical button finish
13:52:19.923 -> check physical button finish
13:52:20.024 -> check physical button finish
13:52:20.127 -> check physical button finish
13:52:20.230 -> check physical button finish
13:52:20.331 -> check physical button finish
13:52:20.432 -> check physical button finish
13:52:20.533 -> check physical button finish
13:52:20.635 -> check physical button finish
13:52:20.736 -> check physical button finish
13:52:20.836 -> check physical button finish
13:52:20.938 -> check physical button finish
13:52:21.040 -> check physical button finish
13:52:21.141 -> check physical button finish
13:52:21.141 -> [5198] Connecting to blynk-cloud.com:80
13:52:21.376 -> check physical button finish
13:52:21.411 -> check physical button finish
13:52:21.512 -> check physical button finish
13:52:21.546 -> [5631] Ready (ping: 187ms).
13:52:21.887 -> check physical button finish
13:52:21.920 -> check physical button finish
13:52:22.022 -> check physical button finish
13:52:22.124 -> check physical button finish
13:52:22.158 -> Update Widget V1
13:52:22.193 -> Update Widget V2
13:52:22.193 -> Update Widget V3
13:52:22.228 -> check physical button finish
13:52:22.330 -> check physical button finish
13:52:22.432 -> check physical button finish
13:52:22.465 -> Update Widget V4
13:52:22.533 -> check physical button finish
13:52:22.633 -> check physical button finish
13:52:22.735 -> check physical button finish
13:52:22.834 -> check physical button finish
13:52:22.936 -> check physical button finish
13:52:23.037 -> check physical button finish
13:52:23.141 -> check physical button finish
13:52:23.242 -> check physical button finish
13:52:23.310 -> check physical button finish
13:52:23.412 -> check physical button finish
13:52:23.512 -> check physical button finish
13:52:23.613 -> check physical button finish
13:52:23.714 -> check physical button finish
13:52:23.817 -> check physical button finish
13:52:23.921 -> check physical button finish
13:52:24.021 -> check physical button finish
13:52:24.121 -> check physical button finish
13:52:24.222 -> check physical button finish
13:52:24.323 -> check physical button finish
13:52:24.424 -> check physical button finish
13:52:24.525 -> check physical button finish
13:52:24.627 -> check physical button finish
13:52:24.728 -> check physical button finish
13:52:24.829 -> check physical button finish
13:52:24.931 -> check physical button finish
13:52:25.033 -> check physical button finish
13:52:25.135 -> check physical button finish
13:52:25.237 -> check physical button finish
13:52:25.338 -> check physical button finish
13:52:25.439 -> check physical button finish
13:52:25.541 -> check physical button finish
13:52:25.642 -> check physical button finish
13:52:25.711 -> check physical button finish
13:52:25.814 -> check physical button finish
13:52:25.916 -> check physical button finish
13:52:26.017 -> check physical button finish
13:52:26.118 -> check physical button finish
13:52:26.356 -> temperature update L
13:52:26.425 -> temperature update M
13:52:26.494 -> temperature update N
13:52:26.527 -> check physical button finish
13:52:26.561 -> check physical button finish
13:52:26.628 -> check physical button finish

I guess it depends on where you’ve put your serial print statements in relation to your Blynk connection code.

Also, you don’t appear to be printing any variables, and I think you’ve positioned your serial print statements in ways that will simply produce lots of constant output (noise) rather than in locations where they give you useful information.

Pete.

here are all the variables

int relais1Sate = LOW;
int btn1State = HIGH;
int ledV1State = LOW;
int ledR1State = LOW;

int relais2Sate = LOW;
int btn2State = HIGH;
int ledV2State = LOW;
int ledR2State = LOW;

int relais3Sate = LOW;
int btn3State = HIGH;
int ledV3State = LOW;
int ledR3State = LOW;

int relais4Sate = LOW;
int btn4State = HIGH;
int ledV4State = LOW;
int ledR4State = LOW;

do i have to write this code for any

   Serial.println(relais1Sate);
   Serial.println(btn1State);
   Serial.println(ledV1State);
   Serial.println(ledR1State);

   Serial.println(relaisXSate);
   Serial.println(btnXState);
   Serial.println(ledVXState);
   Serial.println(ledRXState);

If I was doing this debugging, I’d do something like this…

void checkPhysicalButton1()
{
  if (digitalRead(btn1) == LOW
  {
    Serial.print("Location A, btn1 = ");
    Serial.print(btn1);
    Serial.print("btn1State = ");
    Serial.println(btn1State);
                
    // btnState is used to avoid sequential toggles
    if (btn1State != LOW)
    {
      Serial.print("Location B, btn1 = ");
      Serial.print(btn1);
      Serial.print("btn1State = ");
      Serial.println(btn1State);
      
      // Toggle LED state
      relais1Sate = !relais1Sate;

      Serial.print("Location C, writing ");
      Serial.print(relais1Sate);
      Serial.println(" to Relay 1 ...");     
              
      digitalWrite(relais1, relais1Sate);
      
      ledV1State = !ledV1State;
      
      Serial.print("Location D, writing ");
      Serial.print(!ledV1State);
      Serial.print(" to LED V1 and ");   
      Serial.print(ledV1State);
      Serial.println(" to LED R1 ...");        
       
      digitalWrite(ledV1, !ledV1State);
      digitalWrite(ledR1, ledV1State);

      // Update Button Widget

      Serial.print("Location E, writing ");
      Serial.print(!relais1Sate);
      Serial.println(" to Button Widget on pin V1 ...");   
     
      Blynk.virtualWrite(V1, !relais1Sate);
    }
    btn1State = LOW;
  }
  else
  {
    btn1State = HIGH;
  }

and

BLYNK_WRITE(V1)
{
  relais1Sate = param.asInt();
  Serial.print("Widget Button V1 changed. Writing ");
  Serial.print(relais1Sate);
  Serial.print(" to Relay 1 and LED V1, and ");
  Serial.print(!relais1Sate);
  Serial.println(" to LED R1 ...");
  
  digitalWrite(relais1, relais1Sate);
  digitalWrite(ledV1, relais1Sate);
  digitalWrite(ledR1, !relais1Sate);
}

Pete.

I turn on my esp
when I press the widget button

18:26:55.752 -> Location A, btn4 = 33btn4State = 0
18:26:55.786 -> Widget Button V4 changed. Writing 0 to Relay 4 and LED V4, and 1 to LED R1 ...
18:26:55.887 -> Location A, btn1 = 27btn1State = 0
18:26:55.920 -> Location A, btn2 = 26btn2State = 0
18:26:55.954 -> Location A, btn3 = 25btn3State = 0
18:26:55.987 -> Location A, btn4 = 33btn4State = 0
18:26:56.021 -> Location A, btn1 = 27btn1State = 0
18:26:56.055 -> Location A, btn2 = 26btn2State = 0
18:26:56.089 -> Location A, btn3 = 25btn3State = 0
18:26:56.123 -> Location A, btn4 = 33btn4State = 0
18:26:56.156 -> Location A, btn1 = 27btn1State = 0
18:26:56.224 -> Location A, btn2 = 26btn2State = 0
18:26:56.257 -> Location A, btn3 = 25btn3State = 0
18:26:56.291 -> Location A, btn4 = 33btn4State = 0
18:26:56.325 -> Location A, btn1 = 27btn1State = 0
18:26:56.359 -> Location A, btn2 = 26btn2State = 0
18:26:56.393 -> Location A, btn3 = 25btn3State = 0
18:26:56.427 -> Location A, btn4 = 33btn4State = 0
18:26:56.461 -> Widget Button V1 changed. Writing 0 to Relay 1 and LED V1, and 1 to LED R1 ...
18:26:56.562 -> Location A, btn1 = 27btn1State = 0
18:26:56.595 -> Location A, btn2 = 26btn2State = 0
18:26:56.629 -> Location A, btn3 = 25btn3State = 0
18:26:56.663 -> Location A, btn4 = 33btn4State = 0
18:26:56.697 -> Widget Button V2 changed. Writing 0 to Relay 2 and LED V2, and 1 to LED R2 ...
18:26:56.799 -> Location A, btn1 = 27btn1State = 0
18:26:56.832 -> Location A, btn2 = 26btn2State = 0
18:26:56.867 -> Location A, btn3 = 25btn3State = 0
18:26:56.901 -> Location A, btn4 = 33btn4State = 0
18:26:56.934 -> Location A, btn1 = 27btn1State = 0
18:26:56.969 -> Location A, btn2 = 26btn2State = 0
18:26:57.003 -> Location A, btn3 = 25btn3State = 0
18:26:57.037 -> Location A, btn4 = 33btn4State = 0
18:26:57.107 -> Widget Button V3 changed. Writing 0 to Relay 3 and LED V3, and 1 to LED R3 ...
18:26:57.172 -> Location A, btn1 = 27btn1State = 0
18:26:57.206 -> Location A, btn2 = 26btn2State = 0
18:26:57.240 -> Location A, btn3 = 25btn3State = 0
18:26:57.274 -> Location A, btn4 = 33btn4State = 0
18:26:57.308 -> Location A, btn1 = 27btn1State = 0
18:26:57.375 -> Location A, btn2 = 26btn2State = 0
18:26:57.410 -> Location A, btn3 = 25btn3State = 0
18:26:57.443 -> Location A, btn4 = 33btn4State = 0
18:26:57.476 -> Widget Button V3 changed. Writing 0 to Relay 3 and LED V3, and 0 to LED R3 ...
18:26:57.545 -> Location A, btn1 = 27btn1State = 0
18:26:57.579 -> Location A, btn2 = 26btn2State = 0
18:26:57.614 -> Location A, btn3 = 25btn3State = 0
18:26:57.681 -> Location A, btn4 = 33btn4State = 0
18:26:57.715 -> Widget Button V2 changed. Writing 1 to Relay 2 and LED V2, and 0 to LED R2 ...
18:26:57.782 -> Location A, btn1 = 27btn1State = 0
18:26:57.816 -> Location A, btn2 = 26btn2State = 0
18:26:57.851 -> Location A, btn3 = 25btn3State = 0
18:26:57.886 -> Location A, btn4 = 33btn4State = 0
18:26:57.952 -> Location A, btn1 = 27btn1State = 0
18:26:57.986 -> Location A, btn2 = 26btn2State = 0
18:26:58.020 -> Location A, btn3 = 25btn3State = 0
18:26:58.054 -> Location A, btn4 = 33btn4State = 0
18:26:58.087 -> Location A, btn1 = 27btn1State = 0
18:26:58.121 -> Location A, btn2 = 26btn2State = 0
18:26:58.155 -> Location A, btn3 = 25btn3State = 0
18:26:58.189 -> Location A, btn4 = 33btn4State = 0
18:26:58.223 -> Widget Button V1 changed. Writing 1 to Relay 1 and LED V1, and 0 to LED R1 ...
18:26:58.325 -> Location A, btn1 = 27btn1State = 0
18:26:58.359 -> Location A, btn2 = 26btn2State = 0
18:26:58.394 -> Location A, btn3 = 25btn3State = 0
18:26:58.428 -> Location A, btn4 = 33btn4State = 0
18:26:58.462 -> Location A, btn1 = 27btn1State = 0
18:26:58.496 -> Location A, btn2 = 26btn2State = 0
18:26:58.563 -> Location A, btn3 = 25btn3State = 0
18:26:58.597 -> Location A, btn4 = 33btn4State = 0
18:26:58.631 -> Widget Button V4 changed. Writing 1 to Relay 4 and LED V4, and 0 to LED R1 ...

I then press the physical button, I need two presses to change the state

18:31:33.947 -> Location B, btn2 = 26btn2State = 1
18:31:33.981 -> Location C, writing 0 to Relay 1 ...
18:31:34.048 -> Location D, writing 1 to LED V2 and 0 to LED R2 ...
18:31:34.081 -> Location E, writing 1 to Button Widget on pin V1 ...
18:31:34.149 -> Location A, btn3 = 25btn3State = 0
18:31:34.183 -> Location A, btn4 = 33btn4State = 0
18:31:34.216 -> Location A, btn1 = 27btn1State = 0
18:31:34.249 -> Location A, btn2 = 26btn2State = 0
18:31:34.283 -> Location A, btn3 = 25btn3State = 0
18:31:34.351 -> Location A, btn4 = 33btn4State = 0
18:31:34.384 -> Location A, btn1 = 27btn1State = 0
18:31:34.417 -> Location A, btn3 = 25btn3State = 0
18:31:34.451 -> Location A, btn4 = 33btn4State = 0
18:31:34.485 -> Location A, btn1 = 27btn1State = 0
18:31:34.518 -> Location A, btn2 = 26btn2State = 1
18:31:34.551 -> Location B, btn2 = 26btn2State = 1
18:31:34.585 -> Location C, writing 1 to Relay 1 ...
18:31:34.651 -> Location D, writing 0 to LED V2 and 1 to LED R2 ...
18:31:34.686 -> Location E, writing 0 to Button Widget on pin V1 ...
18:31:34.753 -> Location A, btn3 = 25btn3State = 0
18:31:34.787 -> Location A, btn4 = 33btn4State = 0
18:31:34.821 -> Location A, btn1 = 27btn1State = 0
18:31:34.855 -> Location A, btn2 = 26btn2State = 0

is it possible to remove this second pressure so that it works the first time ?

Okay, you’ve complicated this enormously by adding that debug code to each physical and widget button instead of following with my advice that…

I think you had your btn1State = LOW; command in the wrong place. I’ve changed it in the code below.
Please just test button 1 and widget button 1 with this code.

void checkPhysicalButton1()
{
  if (digitalRead(btn1) == LOW
  {
    Serial.print("Location A, btn1 = ");
    Serial.print(digitalRead(btn1));
    Serial.print(" btn1State = ");
    Serial.println(btn1State);
                
    // btnState is used to avoid sequential toggles
    if (btn1State != LOW)
    {
      Serial.print("Location B, btn1 = ");
      Serial.print(btn1);
      Serial.print("btn1State = ");
      Serial.println(btn1State);
      
      // Toggle LED state
      relais1Sate = !relais1Sate;

      Serial.print("Location C, writing ");
      Serial.print(relais1Sate);
      Serial.println(" to Relay 1 ...");     
              
      digitalWrite(relais1, relais1Sate);
      
      ledV1State = !ledV1State;
      
      Serial.print("Location D, writing ");
      Serial.print(!ledV1State);
      Serial.print(" to LED V1 and ");   
      Serial.print(ledV1State);
      Serial.println(" to LED R1 ...");        
       
      digitalWrite(ledV1, !ledV1State);
      digitalWrite(ledR1, ledV1State);

      // Update Button Widget

      Serial.print("Location E, writing ");
      Serial.print(!relais1Sate);
      Serial.println(" to Button Widget on pin V1 ...");   
     
      Blynk.virtualWrite(V1, !relais1Sate);
      
      btn1State = LOW;     
    }
  }
  else
  {
    btn1State = HIGH;
  }

Pete.

Thanks for the code, but the problem is still there.

  1. I turn on my ESP and I press the button widget, the change of state is very good.
  2. I press the physical button for the first time, nothing happens
  3. I press a second time, the change of state is to pass
  4. if I press the physical button again there is a change of state
  5. I press again on the button widget, the change of state is done very well.
  6. I press the physical button again, nothing happens
  7. I press a second time, the change of state is to pass

And your latest code and serial monitor output when performing the actions you’ve outlined?

Pete.