Trouble using simple button to turn power on using esp8266

i made a simple virtual button to turn on a relay. My issue is that if the blynk app is running and I power up the esp8266, the relay turns on as soon as the esp connects to the blynk server even if the virtual button in the app is in the off state. If I then cycle the button in the app, then it works correctly. If the app isn’t running and I power up the esp, then the power starts off and it works like it should.

using nodemcu esp8266 with blynk 2.27.15 on samsung s8
using blynk server
using blynk .6.1 library

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.  
// Go to the Project Settings (nut icon). 
char auth[] = "2VHAbwOfPzuRxkar726jLi7JJV2Y24by";  
// Your WiFi credentials. 
// Set password to " " for open networks. 
char ssid[] = "ASUS"; 
char pass[] = "";  
void setup() { 
// Debug console 
Serial.begin(115200);  
Blynk.begin(auth, ssid, pass); 
// You can also specify server:  
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442); 
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442); 
}
void loop() {
Blynk.run(); 
}

What pin do you have the relay wired to?
Is the relay active HIGH, or Active LOW?
How is your button configured? Send 1 or 0 when pressed?

You need to synchronise the device with the Blynk server on startup:
https://docs.blynk.cc/#blynk-firmware-blynktimer-blynksyncvirtualvpin

However, your code has no provision to respond to virtual pin changes,so I’m confused by your mention of the use of virtual pins.

It makes no difference whether the app is open or not, the data is stored on the server, not the app. I think that if you investigate and test further you’ll see that the status of the app is irrelevant.

Pete.

pin d5 on the arduino and digital gp14 in the blynk app
the relay is normally off when the esp is powered up
the button is 1 and then 0 but i’ve tried reversing it and that doesn’t stop the issue i’m having which is as soon as the esp connects to the server, it powers the relay on no matter what my app button says. it only matches my app button when i cycle the app button.

It may have something to do with your button settings. You did not mention if the relay is Active HIGH or Active LOW. Does the relay trigger with a HIGH or LOW signal. Maybe provide a link to the relay you are using.

If it is Active LOW, make sure the button settings are opposite to the default settings. Like Below. Also, make sure the button is not pressed when starting the board, as it will take the button settings upon connection to BLYNK.

thanks so much for responding.
i’m not sure what you mean by synchronise the device with blynk server. i tried adding the sync code the app mentions in this variation of the programs and it didn’t seem to do anything:

//create button and choose gp13 for output.  this will give power to pin D7 on esp when button is pushed
#define BLYNK_PRINT Serial



#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

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

WidgetLED led1(V1);

BlynkTimer timer;

//BLYNK_CONNECTED() {Blynk.syncVirtual(V1);}
 
//BLYNK_WRITE(V1) {int buttonState = param.asInt();}

// V1 LED Widget is blinking
void blinkLedWidget()
{
  if (led1.getValue()) {
    led1.on();
    Serial.println("LED on V1: off");
  } else {
    led1.off();
    Serial.println("LED on V1: on");
  }
}

void setup()
{
  Blynk.virtualWrite(V1,LOW);
  
  
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
 
  timer.setInterval(1000L, blinkLedWidget);
}

void loop()
{

  
  Blynk.run();
  timer.run();
}

i wan’t talking about a virtual pin, I said the virtual button in the app i am using to control the relay.

if it doesn’t matter if the app is open then i’m confused as to why the esp only turns the relay on if the app is running. if the app is not running and i power up the esp the relay doesn’t turn on. if i then hit the start button in the app, the relay turns on no matter what my blynk button is set to. then when i cycle the blynk button, it works as it should until the power on the esp is cycled.

thanks for the reply
the relay is set to be off when the esp pin is set to low. this works as expected as long as the blynk app isn’t running when i power up the esp. the esp changes the pin to high as soon as i push the play button on the blynk app no matter what state the blynk button is showing. I’ve switched the button from 1 to 0 with 0 to 1 and that doesn’t change the core problem.
everything works perfectly as expected but only when i cycle the blynk button.

@xixxixxixxi please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

i switched to an LED and it works as expected so the problem apparently is how i’m connecting the relay. i’m hooking the D5 pin of the esp to the “in” port on the relay, the ground to ground, and the vcc to the 3 volt pin on the esp. when i do this, the relay always get powered on when the esp connects to blynk when the blynk app is running. if the blynk app is stopped, this doesn’t happen. if i cycle the blynk button on, off then it starts working as expected but only until the esp is power cycled.

The 3v3 pin is for powering the board off of a steady 3.3VDC source, that is, it bypasses the voltage regulator. You would be better to connect it to the VCC pin of the ESP. This would allow the relay to be powered from the same source that is powering the ESP board.

Additionally, from the picture you posted, the relay is a “low-level trigger”. That means a LOW signal will turn the relay ON (active LOW). So you would want to have the button configured as shown in the picture I provided.

thanks so much, i never thought about the relay working that way. this is the nodemcu esp which doesn’t appear to have a vcc pin.

the issue i’m still confused about is why i have to cycle the bylink button before things work as expected. i have the button programmed as 1 to 0 but the first time i power up the esp, the relay turns on, which is expected now that i know its low triggered, but why doesn’t the esp turn the relay off when it connects to the blynk server and sees that my button is in the off position. why do i have to cycle the blynk button to get the esp to start recognizing the button state?

Because you have to tell your device to ‘see’ that the button is in the off position, you do this via the Blynk.sync process.

You initially used the word “virtual” instead of “widget”, which made me think you were using virtual pins. As you aren’t, then you need to use Blynk.syncAll();

It’s all in the documentation I linked to earlier.

Pete.

And while your at it, I would add in a pinMode(14, OUTPUT); and a digitalWrite(14, HIGH); // or LOW if you want the relay to start ON to your setup. This will allow you to start the relay in the correct state for your application.

2 Likes

that fixed it, thanks so much for the help

Thanks! this is the exact issue i was having and this solution helped. Combination of blynk.sync and pinmode + digital write high in void(setup).