ESP 8266 Pin Go high during boot with no activation from blynk

Hi,

The code below is used for controlling the inbuilt LED and a relay board connected to pin DO using the ESP8266 and Blynk. Everything works fine except that during reboot, both the inbuilt LED and the relay board are turned on without any activation from the Blynk app.

To troubleshoot the issue, the code attempted to set the relay pin to low using digitalWrite(relayPin, LOW), and added a logic flag using if and else statements after updating the Blynk state as follows:

int ledState = param.asInt();
if(ledState==0)
else(Ledstate ==0)

However, despite these changes, the inbuilt LED and port pin are still set to high during boot without activation from Blynk. Can you please help to solve this problem?
code:



#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "---"; // Blynk auth token
char ssid[] = "---"; // WiFi SSID
char pass[] = "----"; // WiFi password

int relayPin = D0;



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


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin,LOW);
}





BLYNK_WRITE(V0)
{

  int ledState = param.asInt();
  if(ledState==1)
  {
  digitalWrite(LED_BUILTIN,HIGH);
  digitalWrite(relayPin,HIGH);
  }

else if (ledState==0)
  {
  digitalWrite(LED_BUILTIN,LOW);
  digitalWrite(relayPin,LOW);
  }

}

@Cathode 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:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

done

I suspect that you’re experiencing some confusion regarding what type of signal (HIGH or LOW) is needed to activate your LED and relay.

The built-in LED on a No0deMCU is lit when pin GPIO2 (the pin labelled D4) is pulled LOW, so the logic is reversed from what you would normally expect.

In addition, many relay boards are either active LOW (relay energised when GPIO0 - pin labelled D0 - is pulled LOW in your case). Having said that, not all relays are like this, and some even have a jumper which allows you to select active HIGH or LOW operation.

In addition, when your device boots-up it doesn’t know what the current state of the widget attached to V0 is. The BLYNK_WRITE(V0) callback function is only triggered when the state of the widget changes, or you use Blynk.syncVirtual(V0) to force the Blynk server to send the latest value to the device, triggering BLYNK_WRITE(V0)
This is usually done with the BLYNK_CONNECTED function, by adding this code somewhere in your sketch…

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V0);
}

Adding some serial print statements to your BLYNK_WRITE(V0) function to show you the value of the ledState variable and which of the if statements is evaluating as true would give you more idea about what is happening.

Pete.

pin D0 always goes high during the boot up process.
This happens before any of your code starts. Run an empty sketch with no code to see.
The solution is not to use D0. Use a pin that does not go high during boot.
Such pins are: D1, D2, D5, D6, D7, D8, D10
Note that D10 is usually used by the Serial monitor.

Thank you for the information.

Here’s a very good guide on which ESO8266 pins to use and avoid, and their functions.

billd

1 Like

why other Pins goes “HIGH” a short pulse on boot such as D1,D2,D5,D6,D7,D8,D10.
which is not reflected while running a empty sketch.what would be reason.

Have you looked at the information that @Bill_Donnelly provided?

Have you translated the “D” numbers into GPIO numbers so that it makes sense?

Pete.

This pin activity is a characteristic of the ESP8266 MCU, nothing to do with Blynk. You need to work with the limitations of the pin outs and plan your projects accordingly.
Billd