Control a virtual switch from WeMos

Dear Sirs,
I need something to implement and I am not sure if it possible and how can do it.
In a project that I have, I use a virtual switch in SWITCH mode. This switch is acting as “appliance on/off switch”. From my WeMos MCU I need in some cases to take control this switch and to put it on or off (without the user doing anything on Blynk application).
Is it possible to implement this? If yes, when I put the virtual switch from WeMos at off, the related routine BLYNK_WRITE(Vxx)
{ if (param.asInt()) …
will be executed?

Please give me your light !

Thanks and Best Regards,
Mike Kranidis

Yes that is right, if you do Blynk.syncVirtual(Vx) then whatever is in BLYNK_WRITE(Vx) will be actioned.

Hello Costas. I hope that you are fine.

Please elaborate it more, for example how can turn off a virtual switch from WeMos (what is the command?).

And when I switch off the virtual switch, I suppose that then I have to do the Blynk.syncVirtual(Vx) right?

Thanks and Best Regards,
Mike Kranidis

Sync is to restore the value held on the server to the app.

If you want to control a virtual button in switch mode then it is simply:

Blynk.virtualWrite(Vx, 1); // for Switch ON
Blynk.virtualWrite(Vx, 0); // for Switch OFF

yes that is more clear to me now.(the commands to turn on or off the switch)
The only problem that I faced on is whereto put Blynk.syncVirtual(Vx) command?
I tried to put in my function and I got error that is undeclared to this scope or something equivalent…

Thanks and Best Regards,
Mike Kranidis

You can’t put it inside the BLYNK_WRITE(Vx) function as this is the actual function it needs to call.

So it goes anywhere outside this function. Normally in setup() after connection to Blynk and when checking for reconnection to Blynk server etc.

I didn’t tried to put Blynk.syncVirtual(Vx) command inside BLYNK_WRITE(Vx) function but inside my function and got the reported error.
Sp the only possible/permmited places to put the Blynk.syncVirtual(Vx) command is inside setup region or inside BLYNK_CONNECTED() etc. Am I right?

It can go anywhere (except inside the function itself) and not in loop() as it will flood the server.

If you are seeing an error you have bad code or bad IDE (1.6.5 to 1.6.8 I think).

you are absolut right @Costas. I tried : syncVirtual(Vx) command ( missed the Blynk. ).

All are ok now… THANKS!

1 Like