What happens on virtual write?

Hi All,

This code:

BLYNK_WRITE(V40){
    Serial.println("I'm in BLYNK_WRITE(V40");
}

will print if I push the attached button in the app.
But it does not print if i call this code:

Blynk.virtualWrite(V40,1);

But this code does change the button in the app.

Is that the correct behavior?

I ask because in the docs it says that calling a webhook that way will work, and so far it does not on my ESP32 …

If V40 is assigned to a button in the app how and why should “Blynk.virtualWrite(V40,1);” trigger a Webhook widget that it is not assigned to? I don’t see that in the documentation.

Yes, it is.

This function is called on the hardware side if you press a button assigend to V40 in your app.

Thank you, IBK.

It seemed logical to me that
-pressing a button in the app or
-calling Blynk.virtualWrite

would have the same effect. But I now understand it does not. So i don’t have to search in that direction.

For the webhook, it doesn’t work yet. I don’t want to assign it to a button, i want to call it from the hardware.

So I attached a webhook to V40 and added this code:

BLYNK_WRITE(V40) {
Serial.println(" V40");
String webhookdata = param.asStr();
Serial.println(webhookdata);
}

The question now is, how to call this code? I tried

Blynk.virtualWrite(V40,1);

but that does not work.

try:
Blynk.syncVirtual(V40);

Hi Marvin7

Thanks for your advice. I’ve tried this before, although the docs don’t mention it. Now i’ve changed my request to a test site: https://httpbin.org/get and tried your suggestion again, Doesn’t help. I’ve put it in like this;

Blynk.virtualWrite(V40,1);
Blynk.syncVirtual(V40);

Well, You asked how to trigger BLYNK_WRITE(){ } handler from code.The answer is syncVirtual(),and it works. The Blynk.virtualWrite() is not needed, as all it does is to send data (in your case it is"1") to server. It does not trigger BLYNK_WRITE()

I think your problem might be the place, where you declared the webhookdata variable. You declared it locally.

Ah, ok this is new to me, since the docs state:

Any write operation from hardware side will trigger Webhook Widget

Anyway I changed my code to

//Blynk.virtualWrite(V40,1);
Blynk.syncVirtual(V40);

But i still don’t see the output.

Also i set

#define BLYNK_MAX_READBYTES 2048

I do declare webhookdata locally, but i also use it locally, so i don’t see a problem in that.

You mean the response is empty, or Serial is not printing anything (even “V40”)?

Serial is not printing anything, not even “V40”

Yet it should :slight_smile:

Haha, i agree.

perhaps you should declare the BLYNK_USE_128_VPINS?
Normally it uses up to 32, I think…

I selected the pin from the list, so i think it’s fine. The list has 128 pins.

The list is in APP, the the device is working on the basis of Blynk library, where it may (should?) be declared… I don’t know, as I’ve never used Vpins that high…

For a first test I would suggest using the button as a trigger. Put the button on e.g. V39 and the Webhook on V40.
Then do this in your program:

BLYNK_WRITE(V39) {
  Serial.println("Button pressed on V39");
  String webhookRequestData = "anything";
  Serial.println(webhookRequestData);
  Blynk.virtualWrite(V40,webhookRequestData);
}

To parse the response on the hardware side add this:

BLYNK_WRITE(V40)
{
  String webhookResponseData = param.asStr();
  Serial.print("webhookResponseData: ");
  Serial.println(webhookResponseData);
}

If that works fine you can move to a next test like e.g using a switch attached to the hardware that triggers the webkook.

I put everything on V0. Same.

That won’t make a difference. Try the test in my post above.

Yes, i saw your post a little later.

So i tried it, but it doesn’t work. I get:

Button pressed on V39
anything

in the console. And that is it.

Wait @Remco, are you trying to trigger BLYNK_WRITE() handler attached to Webhook’s Vpin? If so, then I must say, I’ve NEVER tried that. The syncVirtual() is working great, but it is with values, which are stored on server. How the Webhook works? I dunno…

Me neither.