Relay : green led and red led

This second set of serial output doesn’t start from the same point as the first one - i.e. you’ve not just booted-up the device and connected to Blynk.

Can you post a repetition of what you do to create the first serial output, but leaving the button widget in the OFF position before pressing the physical button?

Pete.

12:52:47.720 -> [196] Connecting to blynk-cloud.com:80
12:52:52.732 -> [5197] Connecting to blynk-cloud.com:80
12:52:53.587 -> [6042] Ready (ping: 307ms).
12:52:54.198 -> Widget Button V1 changed. Writing 0 to Relay 1 and LED V1, and 1 to LED R1 ...
12:53:01.220 -> Location A, btn1 = 0 btn1State = 1
12:53:01.253 -> Location B, btn1 = 27, btn1State = 1
12:53:01.286 -> Location C, writing 1 to Relay 1 ...
12:53:01.356 -> Location D, writing 0 to LED V1 and 1 to LED R1 ...
12:53:01.391 -> Location E, writing 0 to Button Widget on pin V1 ...
12:53:01.459 -> Location A, btn1 = 0 btn1State = 0
12:53:02.926 -> Location A, btn1 = 0 btn1State = 1
12:53:02.959 -> Location B, btn1 = 27, btn1State = 1
12:53:02.993 -> Location C, writing 0 to Relay 1 ...
12:53:03.026 -> Location D, writing 1 to LED V1 and 0 to LED R1 ...
12:53:03.094 -> Location E, writing 1 to Button Widget on pin V1 ...
12:53:03.162 -> Location A, btn1 = 0 btn1State = 0
12:53:04.218 -> Location A, btn1 = 0 btn1State = 1
12:53:04.251 -> Location B, btn1 = 27, btn1State = 1
12:53:04.285 -> Location C, writing 1 to Relay 1 ...
12:53:04.354 -> Location D, writing 0 to LED V1 and 1 to LED R1 ...
12:53:04.387 -> Location E, writing 0 to Button Widget on pin V1 ...
12:53:04.456 -> Location A, btn1 = 0 btn1State = 0
12:53:04.489 -> Location A, btn1 = 0 btn1State = 0
12:53:05.411 -> Location A, btn1 = 0 btn1State = 1
12:53:05.446 -> Location B, btn1 = 27, btn1State = 1
12:53:05.513 -> Location C, writing 0 to Relay 1 ...
12:53:05.546 -> Location D, writing 1 to LED V1 and 0 to LED R1 ...
12:53:05.580 -> Location E, writing 1 to Button Widget on pin V1 ...
12:53:05.648 -> Location A, btn1 = 0 btn1State = 0
12:53:09.634 -> Location A, btn1 = 0 btn1State = 1
12:53:09.667 -> Location B, btn1 = 27, btn1State = 1
12:53:09.701 -> Location C, writing 1 to Relay 1 ...
12:53:09.735 -> Location D, writing 0 to LED V1 and 1 to LED R1 ...
12:53:09.803 -> Location E, writing 0 to Button Widget on pin V1 ...

Also, you’ve made an error when changing this piece of code…

You’ve left the NOT in the serial print statement, so what you see in the serial monitor is exactly the opposit of what the code is doing.

You should:

  1. Fix this and post your updated code
  2. repeat the tests from boot-up in each case and post the setial output for each.

Pete.

my last code :

#define BLYNK_PRINT Serial


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



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


BlynkTimer timer;

char auth[] = "xxxxxxxxx";

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

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

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





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



}







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






 
}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V1);


}

void checkPhysicalButton1();



void setup()
{
  WiFi.begin(ssid, pass);

  Blynk.config(auth);

  // Debug console
  Serial.begin(9600);


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

  


  timer.setInterval(100L, checkPhysicalButton1);




}



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!
}

Widget buton : OFF

13:01:04.878 -> [135] Connecting to blynk-cloud.com:80
13:01:09.897 -> [5136] Connecting to blynk-cloud.com:80
13:01:10.302 -> [5564] Ready (ping: 307ms).
13:01:10.915 -> Widget Button V1 changed. Writing 0 to Relay 1 and LED V1, and 1 to LED R1 ...
13:01:16.684 -> Location A, btn1 = 0 btn1State = 1
13:01:16.718 -> Location B, btn1 = 27, btn1State = 1
13:01:16.751 -> Location C, writing 1 to Relay 1 ...
13:01:16.784 -> Location D, writing 0 to LED V1 and 1 to LED R1 ...
13:01:16.853 -> Location E, writing 1 to Button Widget on pin V1 ...
13:01:16.922 -> Location A, btn1 = 0 btn1State = 0
13:01:20.467 -> Location A, btn1 = 0 btn1State = 1
13:01:20.500 -> Location B, btn1 = 27, btn1State = 1
13:01:20.568 -> Location C, writing 0 to Relay 1 ...
13:01:20.602 -> Location D, writing 1 to LED V1 and 0 to LED R1 ...
13:01:20.636 -> Location E, writing 0 to Button Widget on pin V1 ...
13:01:20.704 -> Location A, btn1 = 0 btn1State = 0
13:01:23.164 -> Location A, btn1 = 0 btn1State = 1
13:01:23.233 -> Location B, btn1 = 27, btn1State = 1
13:01:23.266 -> Location C, writing 1 to Relay 1 ...
13:01:23.300 -> Location D, writing 0 to LED V1 and 1 to LED R1 ...
13:01:23.335 -> Location E, writing 1 to Button Widget on pin V1 ...
13:01:23.405 -> Location A, btn1 = 0 btn1State = 0
13:01:24.664 -> Location A, btn1 = 0 btn1State = 1
13:01:24.732 -> Location B, btn1 = 27, btn1State = 1
13:01:24.766 -> Location C, writing 0 to Relay 1 ...
13:01:24.800 -> Location D, writing 1 to LED V1 and 0 to LED R1 ...
13:01:24.835 -> Location E, writing 0 to Button Widget on pin V1 ...
13:01:24.904 -> Location A, btn1 = 0 btn1State = 0

Widget bouton ON

13:04:18.557 -> [135] Connecting to blynk-cloud.com:80
13:04:23.576 -> [5136] Connecting to blynk-cloud.com:80
13:04:24.152 -> [5715] Ready (ping: 306ms).
13:04:24.764 -> Widget Button V1 changed. Writing 1 to Relay 1 and LED V1, and 0 to LED R1 ...
13:04:36.082 -> Location A, btn1 = 0 btn1State = 1
13:04:36.117 -> Location B, btn1 = 27, btn1State = 1
13:04:36.150 -> Location C, writing 0 to Relay 1 ...
13:04:36.184 -> Location D, writing 0 to LED V1 and 1 to LED R1 ...
13:04:36.252 -> Location E, writing 0 to Button Widget on pin V1 ...
13:04:36.286 -> Location A, btn1 = 0 btn1State = 0
13:04:38.362 -> Location A, btn1 = 0 btn1State = 1
13:04:38.395 -> Location B, btn1 = 27, btn1State = 1
13:04:38.429 -> Location C, writing 1 to Relay 1 ...
13:04:38.497 -> Location D, writing 1 to LED V1 and 0 to LED R1 ...
13:04:38.531 -> Location E, writing 1 to Button Widget on pin V1 ...
13:04:38.600 -> Location A, btn1 = 0 btn1State = 0
13:04:39.968 -> Location A, btn1 = 0 btn1State = 1
13:04:40.001 -> Location B, btn1 = 27, btn1State = 1
13:04:40.035 -> Location C, writing 0 to Relay 1 ...
13:04:40.069 -> Location D, writing 0 to LED V1 and 1 to LED R1 ...
13:04:40.138 -> Location E, writing 0 to Button Widget on pin V1 ...
13:04:41.470 -> Location A, btn1 = 0 btn1State = 1
13:04:41.503 -> Location B, btn1 = 27, btn1State = 1
13:04:41.536 -> Location C, writing 1 to Relay 1 ...
13:04:41.570 -> Location D, writing 1 to LED V1 and 0 to LED R1 ...
13:04:41.638 -> Location E, writing 1 to Button Widget on pin V1 ...
13:04:41.707 -> Location A, btn1 = 1 btn1State = 0

So, back in post #31 I said…

But when I start to study your code in detail I see this…

You need to delete these two lines from the top of your code:

then deal with any compilation errors that this throws-up by replacing them with relais1Sate or !relais1Sate as appropriate.

Once you’ve done that, re-test your code and see what results you’re getting. If it’s still the same then repeat the process of posting your revised code and the two new sets of serial outputs.

Pete.

I think the synchronization problem is solved.
I will continue to do tests and add the other 3 physical buttons
here is my last code

#define BLYNK_PRINT Serial


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



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


BlynkTimer timer;

char auth[] = "xxxxxx";

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

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

int relais1Sate = LOW;
int btn1State = HIGH;






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



}







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



      Serial.print("Location D, writing ");
      Serial.print(!relais1Sate);
      Serial.print(" to LED V1 and ");
      Serial.print(relais1Sate);
      Serial.println(" to LED R1 ...");

      digitalWrite(ledV1, !relais1Sate);
      digitalWrite(ledR1, relais1Sate);

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







}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V1);


}

void checkPhysicalButton1();



void setup()
{
  WiFi.begin(ssid, pass);

  Blynk.config(auth);

  // Debug console
  Serial.begin(9600);


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




  timer.setInterval(100L, checkPhysicalButton1);




}



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!
}

is this disconnected popup normal?

So around 55 wasted posts because you didn’t get rid of these variables!

I’m not sure what the pop up you’re referring to looks like, but you should probably look at the Notification options in the Project Settings.

Pete.

thank you very much @PeteKnight.
How can I thank you for your help?