Virtual or GPIO pin control for relay?

esp-01 has only 2 pins out (2 and 0). Normally we use pin 2 for application,e g like your case of triggering the relay. Hence I revised a bit from your code that you can try here with some notes:

  • on Blynk app the virtual pin is supposed V2
  • the code is for relay with HIGH trigger so if your one is LOW trigger then adjust as comments. If the LOW trigger then the Blynk app with the preset 1 for OFF and 0 for ON
#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[] = "17047b963f2b4e0aa5d305d7d3ac8936";

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

BLYNK_CONNECTED()
{
  // if your relay is HIGH triggering 
  if (digitalRead(2) == 1) Blynk.virtualWrite(V2, digitalRead(2));
  else Blynk.syncVirtual(V2);

/* uncomment this section if your relay is LOW triggering
 * if (digitalRead(2) == 0) Blynk.virtualWrite(V2, digitalRead(2));
  else Blynk.syncVirtual(V2);
 */
  
}

BLYNK_WRITE(V2)
{
  int pinData = param.asInt();
  digitalWrite(2, pinData);
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW); // if your relay is HIGH triggering
  // digitalWrite(2, HIGH); // uncomment this if your relay is LOW triggering
  
  //Blynk.begin(auth, ssid, pass);
  Blynk.begin(auth, ssid, pass, IPAddress(192,168,43,7), 8080);
}

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

Hope this could help.

1 Like