BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Sync button in app with relay status

I have struggled with this for hours now, an i just can’t get it to work.

So here is what i want:
I want to switch on/off an relay from blynk app. I have an ordinary ON/OFF button and so long all works fine. I can turn ON/OFF Relay. Jey!!

But i want the button to tell me the status of the relay. For example if i removes power from relay or it looses power etc.

When that happens i want the button state to change from ON to OFF.

I will also add an timer that turns off the relay after 10 minutes automaticly in code. That part i know how to do. But i also want the button to show OFF when timer turns off the relay.

Can somebody here help me with that?

Here is my code so far. It works, but without timer as i mentioned and the button status change.

It’s the button status change i need help with.

Here is my code.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

//Put WiFi cridentials and token here----------------------------------

char auth[] = "*****";
char ssid[] = "*****";
char pass[] = "*****";

//---------------------------------------------------------------------

int coffeeSwitch = 5;

BlynkTimer timer;


void setup() {
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600);
  digitalWrite(coffeeSwitch, HIGH);
  pinMode(coffeeSwitch, OUTPUT);
  //timer.setInterval(1000L, buttonUpdate);
}

BLYNK_CONNECTED() {
  Blynk.syncAll();
}

BLYNK_WRITE(V1) {
  int pinValue = param.asInt();
  digitalWrite(coffeeSwitch, pinValue);
}

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

In your timer you need a virtualWrite sending the relay state to Blynk. If you want to know the actual state of the relay get another “mains” relay hook the coil to the other relay. Run one pin from the Arduino through the contacts and when the contacts open virtualWrite that state to the Blynk app.

Perhaps a timed function like this? Untested, but should work.

void buttonupdate() {
Blynk.virtualWrite(V1, digitalRead(coffeeSwitch));  // If already the same state nothing really changes. If NOT same, vPin reverts to physical state
}

Either wait for other check timer to run… or simply add this to your “turn off physical pin” function…

Blynk.virtualWrite(V1,0); // EDIT - Or 1 if relay is Active Low

Thanks GTT :slight_smile:

I’m pretty sure i allready have tried that function, but i will try again and get back to you very soon :slight_smile:

Is your relay Active LOW?
If so, how is your switch configured in the app, is it the default of 0 when off and 1 when on?

Pete.

1 Like

I really wish it was so easy GTT.

When i add that function, nothing happend. The button works , but no feedback when i remove power from Relay or my Wemos D1 Mini.

Btw. in serial print i only get “0” state all the time when i tries to read the coffee switch.

Here is the code i tried:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

//Put WiFi cridentials and token here----------------------------------

char auth[] = "****";
char ssid[] = "****";
char pass[] = "****";

//---------------------------------------------------------------------

int coffeeSwitch = 5;

BlynkTimer timer;


void setup() {
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600);
  digitalWrite(coffeeSwitch, HIGH);
  pinMode(coffeeSwitch, OUTPUT);
  timer.setInterval(1000L, buttonUpdate);
}

void buttonUpdate() {
Blynk.virtualWrite(V1, digitalRead(coffeeSwitch));
Serial.print(digitalRead(coffeeSwitch));
}

BLYNK_CONNECTED() {
  Blynk.syncAll();
}

BLYNK_WRITE(V1) {
  int pinValue = param.asInt();
  digitalWrite(coffeeSwitch, pinValue);
}

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

Just for clarification:

The relay keep it’s state when power loss and if i disconnect and connect the controller. But the button does not updates.

And the answer to my question is?

Pete.

Sorry. Didn’t see that :slight_smile:

Yes, the relay is active when LOW.

Here is my button setup:

That’s your problem. You are sending a 0 (LOW, relay ON) when the button is off.

Pete.

I have tried to reverse the values, but i do not get it to work. Can you give me an example code that will help me?

I have tried som more now and reversed the values etc. I just can’t get the button to sync with relay status. When i power off my Wemos the button in app stil says the relay is high…

Are you talking about when the Wemos is off, or once power is restored to the Wemos?

Pete.

When the coffee maker is on and The coffemaker shuts off after 10 minutes i want the button in the app to go from “ON” to “OFF”.

I want to always see the state of the relay on the button. (ON or OFF)

I also want the button show OFF when power loss or if i drag out the power to my wemos.

The current version of Blynk doesn’t have any server-side functionality that enables widget values to be updated if the hardware goes offline. The only functionality is the alert at the top of the app that tells you that the device is offline.

Pete.

Okay. But how can i get the app button to show OFF after 10 minutes when the relay shuts off?

Just like that:

What process is switching your relay off after 10 minutes?

Pete.

I show it in post number six.

I still don’t see the 10 minute relay thing you referred to, and it seems that you’re talking about a system where power is lost to your Wemos or relay.

As I said earlier, Blynk has no server-side process for changing widget values when your Wemos goes offline.
If there is a scenario where the mains power to the device controlled by the relay goes off, but the Wemos is still online, then you’d need to be monitoring power consumption or supply voltage to identify this.

Pete.