Can't get VitualPin to work

I need to add a button which will control the GPIO2 pin on ESP8266.
Since I need it to change from HIGH to LOW and the platform offers LOW to HIGH, I decided to use VirtualPin for this logic.
This is the code I’m using:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "259fe2b81c084596axxxxxxxxxxxxx";

void setup()
  Blynk.begin(auth, "xxxx", "xxxxxx");
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);

    if (param.asInt()==0)    
        digitalWrite(2, HIGH);
        digitalWrite(2, LOW);

void loop()

But when I add a button on the app and assign it to V1, nothing happens when it’s pushed.

BTW, when I’m assigning the button to digital pin 2 it works (but inverse).

What am I doing wrong?

I think you need to declare pinmode in void setup()

@idan88 your sketch works fine here on a WeMos D1 Mini.

Android or iPhone?

Which ESP?


And @Pavel - I have.

@idan88 I was thinking you were trying to control the builtin LED on the ‘advanced’ ESP’s rather than the basic ESP-01.

I believe the blue LED is on GPIO 1 (TX pin) for the ESP-01’s.

What do you have attached to GPIO 2 to know if pushing a Blynk button (virtual or otherwise) controls the ESP-01?

It is connected to a “IN” pin in a relay. LOW turns on the relay.
As I mentioned, the same wiring works when I change in Blynk to digital pin 2 - the relay turns on. But in virtual mode it doesn’t.

All I can suggest is switching the relay for an LED with the LED going off when you press the ON and vice versa. My builtin LED works with your sketch so that suggests a problem with the relay but you say it works ok if you tie it directly to GPIO 2 (but working in reverse).

Not ideal but I guess if you can only get it to work directly from GPIO 2 you could code up a timer to reverse the logic i.e. if signal has changed from high to low then set low to high (but only within a short time frame or the relay will be clicking away like mad).

OK I’ll try. Thank you for your help.