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

C++ BLYNK_WRITE() not called after virtualWrite() from Node.js lib

#1

In my Node.js (not Node-red) client, I am virtually ‘pushing’ a button for 4 seconds with

blynk.virtualWrite(3, 1);

setTimeout(function () {
    blynk.virtualWrite(3, 0);
}, 4000);

On my ESP32, I am using the C++ library to respond to these events with

BLYNK_WRITE(V3) {
    int pushed = param.asInt();
    log_line("Value is: " + String(pushed));
}

If I actually push the button in the app, both Value is: 1 and Value is: 0 state changes are shown in the logs.

Now if I use the virtualWrite() Node.js code above, the button in the app changes state for 4 seconds accordingly, but I don’t see any log output.

Funny thing is that another MCU using the C++ lib and virtualWrite() causes both to show up as well. Is there something special about the Node.js virtualWrite() implementation?

0 Likes

#2

Your title implies the code isn’t working… but then…

… this says it works, just no logs.

So, what is the issue? Not working or working but not reporting so in some logs… and what logs are you referring to?

0 Likes

#3

To summarize: BLYNK_WRITE() is only called on app button pushes, not when I use virtualWrite() from the Node.js Blynk client. Note that the app Styled Button visual state changes properly when using virtualWrite() from the Node.js Blynk client. If this is known behavior, then how do I ‘push’ a button from code?

0 Likes

#4

Again, you are not clear of the issue… now you say the App button changes to show the intended action, but the action doesn’t happen??? If so, you need to follow up the state change with a sync command (that “pushes” the state change to the Server/device code), just like as is needed in C++ code.

setTimeout(function () {
    blynk.virtualWrite(3, 0);  // Changes state of Blynk Button
    blynk.syncVirtual(3);  // Pushes state change to Server
}, 4000);

And where does the physical button code come into play?.. you are not showing any code for that.

0 Likes

#5

Thanks. Sorry for the confusion.

The problem:
Calling virtualWrite() for virtual pin X in a Node.js client causes the Blynk iOS app’s button to change visual state, as expected, but it does not cause BLYNK_WRITE() to be called on the other Blynk client.

I’ve never needed to do this, but upon your suggestion, I added a syncVirtual() call immediately after the virtualWrite()s and that does not change the behavior. Again, why is the app’s button changing state but the BLYNK_WRITE() event isn’t fired?

0 Likes

#6

Look at my code example above…

It should change the state and fire the function (both are separate tasks)… and does in my JS sketches.

0 Likes

#7

Thanks.

It definitely doesn’t fire the BLYNK_WRITE() handler on a virtualWrite() from another client. I tried creating another iOS app button and moving the clients’ code to that pin, but the same behavior happens.

Are multiple Blynk clients (in this case, Node.js and C++) using the same Blynk auth token supported? Have you seen cases where BLYNK_WRITE() is called only when an iOS/Android app button is pushed but not after a virtualWrite()? I’m going to try the WidgetBridge interface next.

Alternatively, is there any other way for one Blynk client to cause an event to be fired on another client?

0 Likes

#8

Different clients/devices with same Auth… NO… it is always best to have each device using its own AUTH code, even if the devices are using the same Project.

Device Selector Widget and Tags may be an option, but the Blynk Bridge is probably your best way.

0 Likes

#9

Switched to a bridge and it works now.

The thing that threw me off with the two-clients case (and may throw off future developers) was that virtualWrite() causes the app button to change state but not fire the BLYNK_WRITE() event. Perhaps the second client connection should be rejected outright…

Anyway, thanks and feel free to close the thread.

0 Likes

#10

Because sending data/state to a Widget (Virtual Write) and triggering a Widget & thus its function (Virtual Sync) are two separate things and NOT always desired to happen together. Thus need two separate commands if both actions are needed…

0 Likes