Virtual or GPIO pin control for relay?

• Esp01-S with relay
• Latest Android version
• local server

I feel a little embarrassed asking for help with this project considering that i have successfully switched on/off my relay last year but i find it not working anymore. The code below was gotten from a finished project on the forum. I tweaked it to fit my requirement but so far it has not worked for me again.
I’m not sure if the problem is coming from the code or from with virtual/gpio pin assignments.
My connection does not have a physical button. Just the on/off button from the blynk app. So help me take a look at tell me according to the code i have posted what pin I should use (virtual or direct gpio)?

EDIT
I think i was asking the wrong question.
the code below can turn on/off gpio2 which controls the onboard led of the esp-01. but it does not trigger the relay

#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";

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

  //Blynk.begin(auth, ssid, pass);
  Blynk.begin(auth, ssid, pass, IPAddress(192,168,43,7), 8080);
}

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


1 Like

There are so many things wrong with this that whatever code you used as the basis for your project, you made a bad choice of starting point.

The code that tests whether you received a 1 or a 0 from the switch widget on V5, along with the code to switch the relay and LED on and off should be within the BLYNK_WRITE(V5) function.

You should remove all references to the physical switch and the relayState variable, and remove the delay.

It baffles me sometimes to discover that users who have virtually no coding skills are running a local server. Presumably it’s bexause of local internet issues, but still seems odd.
Hopefully your local server is fully up to date, and you are using the latest versions of the ablynk library and the ESP core.

Pete.

I do have coding skills. Just not arduino.

Thanks for nothing. I fixed it anyway.

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