Relay : green led and red led

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

Well seen, it’s good I just added

I have to add which line of code to make it work ?

Pete.

When I switch on, I activate the push button, the green led lights up, but the red led does not work. Once I use blynk application everything works fine

You need to add a BLYNK_CONNECTED() callback, and use Blynk.syncVirtual for the pins you want to synchronise.

Read the documentation and you’ll see an example.

Pete.

I will try to find examples because I do not understand why it does not work.

I made a video, it can help to understand my problem : https://youtu.be/OvSr7YILZ_E

I fully understand the problem.
You don’t need to find examples, just read the documentation for BLYNK_CONNECTED()

Alternatively, you could read this:

and read the “Synchronising the output state with the app at startup” section, but it’s just a more wordy version of what is in the official documentation:

https://docs.blynk.cc/#blynk-firmware-blynktimer-blynk_connected

Pete.

in my void setup () I add these lines ??

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

}

No, it doesn’t belong in void setup, it’s a stand-alone function and you can’t place one function inside another.

Pete.

Here is a new video for you to see the changes https://youtu.be/b0EOmAw1xpM

My problem is still present

#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[] = "xxxxxxxxxxxxxxxxxx";

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

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


const int relais2 = 32;
const int btn2 = 26;
const int ledV2 = 5;
const int ledR2 = 2;


const int relais3 = 15;
const int btn3 = 25;
const int ledV3 = 18;
const int ledR3 = 4;

const int relais4 = 12;
const int btn4 = 33;
const int ledV4 = 19;
const int ledR4 = 16;

// 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);
  ledV1State = param.asInt();
  digitalWrite(ledV1, ledV1State);
  digitalWrite(ledR1, !ledV1State);

}

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

}

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

}

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

}

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


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

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

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

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

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

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

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

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

Your checkPhysicalButtonx Functions are also missing the code to update your red LEDs.

Also, your code would be much easier to follow and more efficient if it were streamlined. You don’t need the ledV1State variable in this function at all…

the relais1Sate variable could be used instead.

Pete.

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


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

    }
    btn1State = LOW;
  } else {

// ********for the red led i have to insert some code here ???? ***********************************

    btn1State = HIGH;
    


  }

This is your line of code that turns your green LED on. If you want to turn/off
If you want to have the red LED doing the exact opposite of the green LED then you need to follow that with a line of code to change the state of your red LED, just as I showed you before.

Pete.

the led problem is solved thank you very much for your help.

Could you please explain this part to me?

I tried to modify but it doesn’t work anymore

You are capturing the same widget switch value using param.asInt() and assigning it to two different variables.
Why? What’s the point in doing that?

Pete.

should I write like that

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

Yes.

Pete.

Synchronization with my button widget no longer works

#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[] = "xxxxxxxxxxxxx";

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

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


const int relais2 = 32;
const int btn2 = 26;
const int ledV2 = 5;
const int ledR2 = 2;


const int relais3 = 15;
const int btn3 = 25;
const int ledV3 = 18;
const int ledR3 = 4;

const int relais4 = 12;
const int btn4 = 33;
const int ledV4 = 19;
const int ledR4 = 16;

// 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);
  ledV1State = param.asInt();
  digitalWrite(ledV1, ledV1State);
  digitalWrite(ledR1, !ledV1State);

}

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

}

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

}

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

}

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);
      digitalWrite(ledV1, relais1Sate);
      digitalWrite(ledR1, !relais1Sate);


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


    }
    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);
      digitalWrite(ledV2, relais2Sate);
      digitalWrite(ledR2, !relais2Sate);

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

    }
    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);
      digitalWrite(ledV3, relais3Sate);
      digitalWrite(ledR3, !relais3Sate);

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

    }
    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);
      digitalWrite(ledV4, relais4Sate);
      digitalWrite(ledR4, !relais4Sate);

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

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

What exactly does that mean?

Is this your latest sketch? If so, what changes have you made since you said…

Pete.

Yes this is my last sketch

I modified

So I was talking about changes that you should make to your BLYNK_WRITE routines and you appear to have modified different parts of your code.

Pete.