How do i keep the relay switch from powring on, on boot up

Hi,

I am trying to switch the relay off/on with the ESP8266 NodeMCU via a wifi connection.
The issue is whenever i power on the NodeMCU the relay switch turns on as well. So i want the relay switch to be turned off whenever i power the esp on or when i press the reset button. Here is the code.

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

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

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

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

void loop()
{
  Blynk.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!
}

This will happen if you attach your relay control widgets to digital pins.
The solution is to use virtual pins and modify your code accordingly.

Is your relay active LOW or active HIGH?

Pete.

Thanks for the reply. Can i just switch to Virltual pin from the app ? Or do need to modify the code as well ?.

The switch is active low.

As I said, you’d need to modify your code.
Something like this should get you going…

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

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

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

#define relay_pin 5 // Relay pin connected to GPIO5 (Pin D1) - change as required


BLYNK_WRITE(V1) // callback function that is triggered automatically when the widget attached to pin V1 changes
{
  int widget_value = param.asInt(); // capture the value from the widget

  if (widget_value = 1)
  {
    digitalWrite(relay_pin,HIGH); // Turn the relay on
  }
  else
  {
    digitalWrite(relay_pin,LOW); // Turn the relay off   
  }
}


void setup()
{
  // Debug console
  Serial.begin(9600);
  pinMode(relay_pin,OUTPUT);
  digitalWrite(relay_pin,HIGH);
  
  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);
}

void loop()
{
  Blynk.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!
}

The code assumes that your relay is attached to GPIO1 (D5) and that you have a button widget (in Switch mode) attached to pin V1.

Pete.

I think his problem come from the choice of the mcu pinout.
Remember, some pins are high at MCU start !
He has to use safe pins.
Blynk V2 is on the way :joy:

its been so long waiting for V2 i have got white hairs to show how long we are waiting !!:joy:

1 Like

Only one year waiting :joy::joy:

1 Like

Not quite a year yet, the “sign up for Beta” announcement was on 19th May, but its still been too long :crazy_face:

Pete.

1 Like

They lost the sign up list :scream:

1 Like