ESP-12-f Standalone + Blynk+ Relays

Hi. I’m new to this and I need some help.
I got a ESP-12 f wired to a 2 relay board.
I am using Arduino 1.8.1, and the libraries are updated.
I used the ESP8266_standalone program.
The problem is:
Then I power the system, one relay stays on until it receives command from my phone. After that, it works fine.

The code is:`
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

// Your WiFi credentials.
// Set password to “” for open networks.
char ssid[] = “xxxx”;
char pass[] = “xx”;

void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}

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


You can see in the video that one relay starts automatically when I power up the schematic. After I push the button, it works fine

There are a couple of things at play here.

First is that some of the GPIOs on the ESP are high at boot (due to the way the ESP boots), others are low. From the video I can’t tell which pins you’ve used, but I’d avoid 0, 2 and 15 unless you run out of pins, as they are involved in the boot process.
If you want to be sure of the state of a GPIO at boot time, you need to initialise them in setup() using

digitalWrite(PIN, 0);
pinMode(PIN, OUTPUT;

This writes a 0 to the before setting it as an output, which is the safest way to ensure your output does not go high at boot.

One thing I might need some clarification on is Blynk itself; I was under the impression that when Blynk first connects it syncs all widget states, but you can see in the video the relay stays on despite the button not being pressed.
You could try a Blynk.syncAll(); after Blynk.begin(); to ensure that widget states are synced.

Also whenever you post code:

I used pin 2 and 4. But the problem is with all the pins. Because I tried first with 5.if I delete one button from blynk, then add it again at the same port, it will change the led which stays on when i first power up.The way I see it, the problem is with one of the libraries.


#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

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

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
}

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

That’s better. I highly doubt it’s a Blynk problem; this basic stuff has been pretty well figured out at this point.

I actually had a very similar setup with that same relay board and I had it behaving itself relatively easily.

Have you tried

syncAll();

?

Or initialising the pins yourself?

I will try to initialize the pins and let you know. And thank you for the help. I started with arduino and coding 2 weeks ago, so I am new to this.

@cristi, you need to move away from direct GPIO pins and start working with Virtual Pins.
This is the only way you will be able to make your idea work correctly.

Also your code has nothing in it! So im not surprised nothing works correctly.

You need to think of Blynk as just a way to see the data. You really need to write a sketch that can control the relay module on its own or via the Serial monitor.

Here is a quick example for you to study. Also check out my own relay project with timers etc

// define the physical pins the relays are connected to. Make sure you use GPIO numbers and not D numbers. D6 = GPIO12 etc 
#define RELAY1_PIN 12  // D6
#define RELAY1_PIN 13  // D7

// add the following setup() to your own sketch
void setup(){
  pinMode(RELAY1_PIN,OUTPUT);
  pinMode(RELAY2_PIN,OUTPUT);
  digitalWrite(RELAY1_PIN, HIGH); // most modules are LOW active... so on boot start inactive (HIGH)
  digitalWrite(RELAY2_PIN, HIGH); // most modules are LOW active... so on boot start inactive (HIGH)
}

BLYNK_WRITE(1){ // virtual pin 1... create a button widget in SWITCH mode
  if(param.asInt()){
    digitalWrite(RELAY1_PIN, LOW); // if button is HIGH, turn on relay1
  } else {
    digitalWrite(RELAY1_PIN, HIGH); // if button is LOW, turn off relay1
  }
}

BLYNK_WRITE(2){ // virtual pin 2... create a button widget in SWITCH mode
  if(param.asInt()){
    digitalWrite(RELAY2_PIN, LOW); // if button is HIGH, turn on relay1
  } else {
    digitalWrite(RELAY2_PIN, HIGH); // if button is LOW, turn off relay1
  }
}

2 Likes