BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Raspberry wiringPi: virtual Pin control output

Hi everybody,

I have a problem controlling a GPIO of the raspberry with a virtual Pin which is connected to a button in the app.
My set up: Raspberry Pi with wiringPi

Detaild problem:
I have a Button as switch connected to V0. With this I would like to control GPIO27. If I do it with only this Button in the app and nothing else it do not work. If I put a button conntectet to GPIO27 on the screen as well then it works. So my idea was that GPIO is not initialized as OUTPUT. So I googled a lot and find example from Costas, from the following post: Blynk for Beginners and help with your project ,which I modified a litte bit but with the I think important command “pinMode(relayPin, OUTPUT);” As you can se in the code below.

But it still do not work. The LED over the virtual Pin get “high” and “low” but only my GPIO not.
So can please anybody help me out with a hint.

Thanks in advance.

Code:


// #define BLYNK_DEBUG
#define BLYNK_PRINT stdout
#ifdef RASPBERRY
 #include <BlynkApiWiringPi.h>
#else
 #include <BlynkApiLinux.h>
#endif
#include <BlynkSocket.h>
#include <BlynkOptionsParser.h>
#include <SimpleTimer.h>

static BlynkTransportSocket _blynkTransport;
BlynkSocket Blynk(_blynkTransport);

#include <BlynkWidgets.h>

#define vRelayBtn V0
#define vLED      V1
#define vDisplay  V2

#define vLEDHigh 255
#define vLEDLow    0
#define relayPin   27   // (D4 on WeMos, builtin LED, active LOW)

unsigned int vRelayStatus;

BLYNK_WRITE(vRelayBtn) {     // Blynk app WRITES button status to server
  vRelayStatus = param.asInt();
  if(vRelayStatus == 1){
    digitalWrite(relayPin, LOW);        // see note at line 8
    Blynk.virtualWrite(vLED, vLEDHigh);
 }
  else{
    digitalWrite(relayPin, HIGH);       // see note at line 8
    Blynk.virtualWrite(vLED, vLEDLow);
  }
}


void setup()
{
  pinMode(relayPin, OUTPUT);
}

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

int main(int argc, char* argv[]){
    const char *auth, *serv;
    uint16_t port;
    parse_options(argc, argv, auth, serv, port);
    Blynk.begin(auth, serv, port);
    while(true) {
                loop();
    }
    return 0;
}

@schmersgahoven you might want to take a look at the Using C++ on a Raspberry Pi with Blynk thread at Using C++ on a Raspberry Pi with Blynk

Hi Costas,

thanks for your quick reply. From the post you mentioned I got my first information about setting up c++ on the raspberry and played around with your example code. Thanks for this, helped me a lot. But in the example you used as well a virtual button an a digital button for the same GPIO. I want to use only the virtual button for the GPIO. So if I delete the digital Button out of my app screen and just want to use the virtual button with the code out of your link then it did not work as well.

I think your hint to me was the following sentence from you that GPIOs can be used without “initialize” in void setup? Your sentence: “It’s basically a PushData for Pi, with a few extras, using wiringPi so you can select “GPIO” pins directly in the project. Without messing around with various libraries the revised main.cpp is as follows”

But where is my fault. Or is it just not possible?

@schmersgahoven I don’t see why you would need a button tied to a GPIO in addition to the virtual pin. I just wanted to include both to show the interaction between the 2 types of pins in the sample sketch.

Is your wiringPi all set up correctly?

Can you paste a screenshot from gpio readall

Presumably you are looking at physical pin 13 on the Pi for BCM27.

@costas I want to control the GPIO only with the virtual pin tied to the button and not with the digital button. It is because I want to have another action done when the button is pushed and with the virtual pin tied to the button I can do so with BLYNK_WRITE.

So I installed everthink on the raspberry new including jessie and wiring pi and so. But stll the same problem. GPIO only reacts on Button with virtual pin when I add a second button with digital pin.

Screenshot from gpio readall attached. First only with virtual pin tied to Button, second one with second button tied to digital pin. First one all of them are defined as IN only at the second one when I added the button tied to the digital Pin it is defined as OUT. Some idea what I did wrong?

Code is now the one from your example thread.

First:

Second:

@schmersgahoven OK I have hooked up the Pi Zero to check out a few things.

If I remove the gp17 button from the project and press the button tied to the virtual pin then gpio readall confirms gp17 is going high and low as required.

I notice you have a Pi 3 and the Zero’s are effectively Pi 2’s.
I’m not convinced that Blynk is 100% functional with Pi 3’s from other posts I have read on this site.

Do you happen to have any Pi 2’s?

@costat maybe next week I will have the possibility to check with a raspberry 2. But after your last post I think I forgot a very important think to write. I reboot the raspberr after each change. Have you done this after remove the digital button from the app? If I have both buttons available and everything running and remove digital button during operation, no problem. But if I reboot with deleted digital button virtual button is not working with GP17. I reboot after each change to be sure that all my changes will withstand real life handling. Or if I just set up a code with only virtual button and in the app only virtual button from start if raspberry it did not work. Do you know where the GP are initialized by the digital button. My thinking is just to do so as if there is a digital button in the app and clone this. But for sure I am not deep enough in the topic but intersted to find out and try.

@schmersgahoven what I have just done is set up a test project to use the Zero as the server, so the Zero is now server and client.
The new project I created never had any direct digital pin widgets (button), just the virtual pin widgets (button). I can confirm that the virtual pin controls the digital pin as expected.

But, I thought I would also reboot the Zero as it might still have details of the digital pins from when the Zero was just a client yesterday. As you have established if there aren’t any widgets tied directly to a digital pin the virtual pin will not control a digital pin. Nice work from you to track down the issue.

As soon as I add a button tied to a digital pin the original virtual pin button will control the digital pin.

I’m guessing this is something to do with Blynk’s magic. Perhaps @vshymanskyy can comment on the issue.

As far as your project is concerned then surely the fix is just to ensure that you include digital pin widgets even though you are not going to use them directly i.e. you want the power of virtual pins to be able to control multiple digital pins from a single widget.