Esp01 relay is powered on after reboot

Yes, I know you have used an example and modified it.

thanks to Mr.Donnelly, Mr. Knight,and Blynk

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example runs directly on ESP8266 chip.

  Note: This requires ESP8266 support package:
    https://github.com/esp8266/Arduino

  Please be sure to select the right ESP8266 module
  in the Tools -> Board menu!

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

/* Comment this out to disable prints and save space */
#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[] = "your authorization code";

/*// This function will run every time Blynk connection is established
  BLYNK_CONNECTED() {
  // Request Blynk server to re-send latest values for all pins
  Blynk.syncAll();
  }
*/
  // Your WiFi credentials.
  // Set password to "" for open networks.
  char ssid[] = "your ssid";
  char pass[] = "your password";


  void setup()
  {
    // Debug console
    Serial.begin(9600);
    pinMode(0,OUTPUT);
    digitalWrite(0,HIGH);
    Blynk.begin(auth, ssid, pass);

  }

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

Is your relay energized with an output low or high?

Whether it does nor not I recommend using a 555 timer to install a delay to your relay power line. This way the ESP01 can go through its boot process before the relay power becomes available. The 555 timer circuit is simple and that’s what I have done to get around the problem you describe.

In my application I use a 8 second delay upon power up before power becomes available to the relays. It works great and its a simple circuit.

Regards,

Roger

Rega

I’ll have to check that out!
Thanks

I use a great many devices which contain relays that are controlled by ESP8266s and adding additional circuitry of this type is totally unnecessary.
In fact, in many off the shelf devices such as Sonoff and Shelly smart switches it’s impractical to add additional hardware.
Using a pinMode statement and a corresponding digitalWrite early in the void setup is all that is needed - provided that you (or the 3rd party manufacturer) chooses appropriate pins to connect to the ESP8266.
If the ESP8266 doesn’t have sufficient suitable pins to support your custom built device then using a port expander or an ESP32 is the more appropriate course of action in my opinion.

Pete.

Hey Pete,

He says he’s using an ESP01. Are you aware that the ESP01 only has 4 I/O pins? In addition it initiates serial data on at least the TX pin during power up for debug purposes that can’t be eliminated. If TX is used as an output to a relay, the relay it will be energized and de-energized several times.

If you have a better work around please share it because I may want to use it in future projects. I will anxiously look forward to your response because I am currently working on another project with the ESP01.

Again this is very exciting , even though the 555 timer works extremely well in my applications to delay power to the relays until ESP01 power up is complete it would be nice to remove it and make life a little simpler.

Regards,

Roger

Jmarins2,

Here is a link to a very good resource for the ESP-01; it helped me a lot hope it helps you.

How to Use the ESP8266-01 Pins and Leds : 5 Steps - Instructables

Regards,

Roger

Yes, I’m fully aware that only four GPIOs are broken-out on an ESP-01 board.

The Tx pin is LOW for around 50ms at start-up and then it depends what you do with it after that. But, it falls into the category of “not recommended as a GPIO pin” in the table below…

and is therefore unsuitable for driving a relay, which is why I said…

In my opinion, the ESP-01 is really only useful as a WiFi modem for a non IoT enabled device, or to use in a project where physical space is at an absolute premium. However, in this scenario you probably wouldn’t have space for the 555s or relays. My go-to board is the Wemos D1 Mini, or D1 Mini Pro if an external antenna is needed. These boards are significantly smaller than the NodeMCU without the loss of any useable GOIOs.

If you want to stick with ESP8288’s then a port expander such as the MCP23017 is one option. I’ve never tried using one of these with an ESO-01, but in theory it’s possible but I prefer to use them with a D1 Mini…

Pete.

I read your post on that MCP23017 IO Expansion Board looks interesting; I may have to try it sometime. I have used numerous boards in the past where I felt IO limited so this may be a solution. I would like to try one of those mini’s and will definitely pick up some in the future but I still use the ESP01 when I can; at a dollar a piece they are hard to beat for small projects.

Regards,

Roger

Compared to the outlandish $2.50 for a D1 Mini?
Sometimes it pays to go that extra mile!

TBH, the biggest issue I have with the ESP-01 (other than the limited number of broken-out GPIOs) is that fact that they are so awkward to work with compared to a D1 Mini. The lack of a USB port for debugging is a pain, and although you can stick the ESP-01 onto a USB adapter it’s far less convenient in my opinion.
The thing I love about the D1 Mini is the range of accessories that can be stacked on top, or alongside the main board. I use the double and triple bases quite a bit, along with the prototyping shields, and it’s very convenient to work with and even use as a final solution like the gate release setup shown in this topic…

Pete.

Hi people. I was looking for some info and came back across this. I would really like to thank Pete knight and bill Donnelly for patience several years ago!

1 Like

Hi,
I’ve read a lot about this, and since this thread is exactly my question, I’m seeking guidance here.
I’m building a small project to control a garage door. To this end, I’m using a ESP8266 nodemcu, connected to a 4 module relay, and I’m having this exact same problem! Everytime the power goes down, or the ESP8266 reboots, it turns on the relays for a second. I’ve tried to sync with the last value, which is always 0, and also tried to force it to be HIGH on the void setup. Both things didin’t work. Could someone kindly give me some guidance?
Thank you
Tiago Silva

You are not using safe pins, it’s a known issue.
Search Esp8266 pinout on Google.

Hi @Blynk_Coeur ,
Thank you for the fast reply.
I did search before, and from my understanding I’m using safe pins. I’m currently using D1 and D2, which correspond to GPIO5 and GPIO4.
I’m new to this and might have miss understood something, if so, please tell me the correct pints to use.
Thank you,
Tiago Silva

1 Like

Read this article

Hi @John93 ,

I have looked into that, but also found another article on that forum that explains even better:

Where I quote:
“Taking this into account, the safest ESP8266 pins to use with relays are: GPIO 5, GPIO 4, GPIO 14, GPIO 12 and GPIO 13.”

Which include both pins I’m using.

Really confused here.

Thank you

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