Pilot light

Hello everyone,
I have programmed a push button to operate a relay. No problem.
I would like to add a led that works with the relay, to see if it is open or closed.
Could you help me with the programming because the led won’t light up.


#define BLYNK_PRINT Serial


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

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

char auth[] = "xxxxxx";

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

// Set your LED and physical button pins here
const int relais1 = 18;
const int btnPin = 19;
const int ledBleu = 34;

BlynkTimer timer;


void checkPhysicalButton();

int relais1Sate = LOW;
int btnState = HIGH;
int ledBleuState = LOW;


// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V2);

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
  relais1Sate = param.asInt();
  digitalWrite(relais1, relais1Sate);
  ledBleuState = param.asInt();
  digitalWrite(ledBleu,ledBleuState);
  
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      relais1Sate = !relais1Sate;
      digitalWrite(relais1, relais1Sate);
      ledBleuState =!ledBleuState;
      digitalWrite(ledBleu, ledBleuState);

      // Update Button Widget
      Blynk.virtualWrite(V2, relais1Sate);
      Blynk.virtualWrite(V2, ledBleuState);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  pinMode(relais1, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  digitalWrite(relais1, relais1Sate);
  pinMode(ledBleu, OUTPUT);


  timer.setInterval(100L, checkPhysicalButton);

}

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


LED widgets operate on a scale of brightness from 0 (off) to 255 (fully on).
128 will light the LED widget at half brightness. 1 will light the LED so dimly that it appears to be off.

This:

writes either a 0 or a 1 to the LED widget, so it appears to be off all of the time (although if you look very carefully you should be able to see it glow very faintly when you turn the relay on.

The solution is to add 254 to the value you write to the LED widget. 0+254 = 0, 1+254 = 255…

ledBleuState =!ledBleuState;
Blynk.virtualWrite(ledBleu, ledBleuState + 254);

Pete.

I come to try, it does not work.
I don’t want to put a widget in app, I want to put a real led which is synchronized with the relay

Sorry! That’s what happens when you type a reply when watching a movie on TV. I don’t have the genes for multi-tasking!

Pin 34 on the ESP32 is input only…

Pete.

1 Like

thank you for your answer, the synchronization works perfectly.
If I lose my internet connection, the push button does not work, is there a way to solve this problem

#define BLYNK_PRINT Serial


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

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

char auth[] = "xxxx";

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

// Set your LED and physical button pins here
const int relais1 = 18;
const int btnPin = 19;
const int ledBleu = 5;

BlynkTimer timer;


void checkPhysicalButton();

int relais1Sate = LOW;
int btnState = HIGH;
int ledBleuState = LOW;


// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V2);

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
  relais1Sate = param.asInt();
  digitalWrite(relais1, relais1Sate);
  ledBleuState = param.asInt();
  digitalWrite(ledBleu,ledBleuState);
  
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      relais1Sate = !relais1Sate;
      digitalWrite(relais1, relais1Sate);
      ledBleuState =!ledBleuState;
      digitalWrite(ledBleu, ledBleuState);

      // Update Button Widget
      Blynk.virtualWrite(V2, relais1Sate);
      
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  pinMode(relais1, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  digitalWrite(relais1, relais1Sate);
  pinMode(ledBleu, OUTPUT);


  timer.setInterval(100L, checkPhysicalButton);

}

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

Blynk.begin is a blocking function.

You should switch to setting-up your own WiFi connection and if this is successful then using Blynk.config and Blynk.begin to establish the connection to the Blynk server.

Search the forum and you’ll find plenty of examples.

Pete.

Could you share a link with me, so that I can adapt to my project, I find lots of different solutions

what do you think of this code?
When starting my esp32 my relay jumps twice before stabilizing. :rage:

#define BLYNK_PRINT Serial


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

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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxx";

char ssid[] = "xxxxx";
char pass[] = "xxx";



// Set your LED and physical button pins here
const int relais1 = 18;
const int btnPin = 19;
const int ledBleu = 5;

BlynkTimer timer;
void checkPhysicalButton();

int relais1Sate = LOW;
int btnState = HIGH;
int ledBleuState = LOW;

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V2);

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
  relais1Sate = param.asInt();
  digitalWrite(relais1, relais1Sate);
  ledBleuState = param.asInt();
  digitalWrite(ledBleu,ledBleuState);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      relais1Sate = !relais1Sate;
      digitalWrite(relais1, relais1Sate);
      ledBleuState = !ledBleuState;
      digitalWrite(ledBleu, ledBleuState);

      // Update Button Widget
      Blynk.virtualWrite(V2, relais1Sate);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  
  pinMode(relais1, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  digitalWrite(relais1, relais1Sate);
  pinMode(ledBleu, OUTPUT);

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

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

Well, it still uses Blynk.begin

I’d put your pinMode and digitalWrite command before the Blynk connection code.

Also, you’re starting with relais1Sate = LOW is that what you want?

Pete.

here is a new code with : Blynk.config(auth);

my push button works fine, with or without connection.

But my Widget button on the application no longer works, why is you ??

#define BLYNK_PRINT Serial


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

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

char auth[] = "xxxxxx";

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

// Set your LED and physical button pins here
const int relais1 = 18;
const int btnPin = 19;
const int ledBleu = 5;



BlynkTimer timer;


void checkPhysicalButton();

int relais1Sate = LOW;
int btnState = HIGH;
int ledBleuState = LOW;




void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      relais1Sate = !relais1Sate;
      digitalWrite(relais1, relais1Sate);
      ledBleuState = !ledBleuState;
      digitalWrite(ledBleu, ledBleuState);

      // Update Button Widget
      Blynk.virtualWrite(V2, relais1Sate);
      Blynk.virtualWrite(V2, ledBleuState);

    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}

void setup()
{
  // 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(btnPin, INPUT_PULLUP);
  digitalWrite(relais1, relais1Sate);
  pinMode(ledBleu, OUTPUT);


  timer.setInterval(100L, checkPhysicalButton);

}

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

Because you’ve removed the BLYNK_WRITE(V2) callback function.

Pete.

Thanks a lot for your help. Now that everything works perfectly, on this code I add the code of my 3 ds18B20 to connect in onewire.
This code works well separately but when I combine it with my relay code, the temperature does not appear in the application

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

BlynkTimer timer;

char auth[] = "xxxxx";

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

// Set your LED and physical button pins here
const int relais1 = 18;
const int btnPin = 19;
const int ledBleu = 5;

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






void checkPhysicalButton();
void temperature();


int relais1Sate = LOW;
int btnState = HIGH;
int ledBleuState = LOW;


BLYNK_WRITE(V2) {
  relais1Sate = param.asInt();
  digitalWrite(relais1, relais1Sate);
  ledBleuState = param.asInt();
  digitalWrite(ledBleu,ledBleuState);
  
}

void temperature()
{
  // 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);
    
  
  }
}


void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      relais1Sate = !relais1Sate;
      digitalWrite(relais1, relais1Sate);
      ledBleuState = !ledBleuState;
      digitalWrite(ledBleu, ledBleuState);

      // Update Button Widget
      Blynk.virtualWrite(V2, relais1Sate);
      Blynk.virtualWrite(V2, ledBleuState);

    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}





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(btnPin, INPUT_PULLUP);
  digitalWrite(relais1, relais1Sate);
  pinMode(ledBleu, OUTPUT);

 
  Serial.begin(9600);


  timer.setInterval(100L, checkPhysicalButton);
  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!
}

Isn’t this meant to be 3, if you have 3 temperature sensors?

Pete.

1 Like