Esp01 relay is powered on after reboot

Sharing your code would be a good start.

Pete.

/*************************************************************

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID           "TMPL9CKTk3Be"
#define BLYNK_DEVICE_NAME           "SmartGate"
#define BLYNK_AUTH_TOKEN            "xpto"


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


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

char auth[] = BLYNK_AUTH_TOKEN;

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

// define the GPIO connected with Relays and switches
#define RelayPin1 5  //D1
#define RelayPin2 4  //D2
#define RelayPin3 14 //D5
#define RelayPin4 12 //D6

//Change the virtual pins according the rooms
#define VPIN_BUTTON_1    V1
#define VPIN_BUTTON_2    V2
#define VPIN_BUTTON_3    V3 
#define VPIN_BUTTON_4    V4

// Relay State
bool toggleState_1 = LOW; //Define integer to remember the toggle state for relay 1
bool toggleState_2 = LOW; //Define integer to remember the toggle state for relay 2
bool toggleState_3 = LOW; //Define integer to remember the toggle state for relay 3
bool toggleState_4 = LOW; //Define integer to remember the toggle state for relay 4

BlynkTimer timer;

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED() {
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
}

BLYNK_WRITE(VPIN_BUTTON_1) {
  toggleState_1 = param.asInt();
  toggleState_2 = param.asInt();
  if(toggleState_1 == 1){
    digitalWrite(RelayPin1, LOW);
    digitalWrite(RelayPin2, LOW);
  }
  else { 
    digitalWrite(RelayPin1, HIGH);
    Blynk.virtualWrite(VPIN_BUTTON_2, HIGH);
    timer.setTimeout(5000, []() {
  if(toggleState_2 == 1){
    digitalWrite(RelayPin2, LOW);
  }
  else { 
    digitalWrite(RelayPin2, HIGH);
  }
  Blynk.virtualWrite(VPIN_BUTTON_2, LOW);
    });
  }
}


BLYNK_WRITE(VPIN_BUTTON_2) {
  toggleState_2 = param.asInt();
  if(toggleState_2 == 1){
    digitalWrite(RelayPin2, LOW);
  }
  else { 
    digitalWrite(RelayPin2, HIGH);
  }
}

// This function sends Arduino's uptime every second to Virtual Pin 6.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V6, millis() / 1000);
}

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

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

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  pinMode(RelayPin1, OUTPUT); // Set ESP pin to output to the relay
  pinMode(RelayPin2, OUTPUT); // Set ESP pin to output to the relay
  digitalWrite(RelayPin1, HIGH); // activate = LOW, deactivate = HIGH
  digitalWrite(RelayPin2, HIGH); // activate = LOW, deactivate = HIGH
  Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
  Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
}

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

So I got it doing the following:
A ESP8266 ESP-12F v3 Loli connected to a 4 relay module (currently just using 2 outs). On the blynk I got 2 buttons, one controls a warning big orange rotative led (like the tractors) on and off. The other, controls the gate itself. On the gate buttons, I’ve made a change that so anytime we press the button to open, it opens the gate, and also turns the led for 5seconds.

Thanks you for any input, improvement and mostly how can I fix this annoying issue.

Regards

EDIT: Still, I don’t think its a coding issue, as if I leave just the basic, the relays still turn on when node mcu does, seems it turns on the relays at boot.

This needs to be at the top of your void setup….

And I’m not clear why you’re only doing this for two of the relays.

Having these two lines of code in your void setup probably won’t have the desired effect. You’d be better putting them in your BLYNK_CONNECTED() callback function…

On that subject, I don’t understand why you’re synchronising pins V1 and V2 in BLYNK_CONNECTED. If these are ON in the app the gate will open when power is restored to the ESP, which makes no sense to me from a security perspective.

I don’t really understand what you’re doing with the BLYNK_WRITE(vPin) callbacks either, as the logic seems reversed.

I also don’t understand this…

You’ve said that your relays are active HIGH, so why aren’t you setting these tracking variables HIGH too?

Pete.

Hi Pete,
Thank you for your comments.
Indeed this:

  pinMode(RelayPin1, OUTPUT); // Set ESP pin to output to the relay
  pinMode(RelayPin2, OUTPUT); // Set ESP pin to output to the relay
  digitalWrite(RelayPin1, HIGH); // activate = LOW, deactivate = HIGH
  digitalWrite(RelayPin2, HIGH); // activate = LOW, deactivate = HIGH

Had to be on top of the void setup! That solved my issue!!

I also though on the security issue you said, and it makes sense, I’ve removed the BLYNK_CONNECTED() function.

Everything is working as expected now.

Thank you for your time and patience.
Tiago Silva

1 Like