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

Cannot make virtualWrite work

I cannot make virtualWrite work in my sketch. I have generated a bare bones sketch to illustrate my problem. I’m pretty sure that I am not connecting something correctly, but need help figuring this out. I have a pushbutton connected to pin D3 of my ESP8266. I also have a pushbuton widget connected to datastream V2. When I press the button widhet on the iPad dashboard, I get to the BLYNK_WRITE(V2) code. But when I press the physical button, I get to the button pressed code, and issue a Blynk.virtualWrite(V2, 1); This does not get to the BLYNK_WRITE(V2) code.
here are screen shots of the test code and the Blynk 2 screens.

I pressed the physical button 4 times, then pressed the widget button 4 times

Hi, first of all, posting screenshots of your code and serial output isn’t really helpful.
You should copy/paste your code and serial data and post them to the forum between triple backticks to ensure that they display correctly.
Triple backticks look like this:
```

Secondly, Blynk.virtualWrite(V2) will not trigger a the BLYNK_WRITE(V2) callback function. This is to avoid creating an endless loop.
If you need to obtain the latest value from V2 then you need to use the Blynk.syncVirtual(V2) command to trigger the BLYNK_WRITE(V2) callback - but in your case you don’t need to know the latest value for V2, as you already know what value you’ve just written to it.

Thirdly, Blynk doesn’t like unnecessarily code 8n the void loop, so you should either attach an interrupt to your physical switch pin, or use a BlynkTimer to poll the switch on a frequent basis.

Pete.

OK, criticisms accepted. But the answer about virtual_write it totally confusing. If it doesn’t invoke the BLYNK_WRITE code, then what does it do? I can’t think of another use for it.

Blynk.virtualWrite(vPin) is used for writing values to be server. Usually used for writing sensor data such as temperature, humidity etch that will be displayed in the app. It can also be used for changing the status of a widget such as an on/off switch.

Pete.

Well, it worked OK in Blynk 1. I have close to 20 devices and have used that feature extensively in all of them. I know that there are other ways to accomplish what I was doing, but it will require a LOT of rewrite. Very disappointing. I also used Bridge extensively and must redo all of that. Maybe Blynk 2 isn’t my way forward.

Blynk Legacy was exactly the same.

If you used Bridge to send the virtualWrite from another Auth token then it would trigger the BLYNK_WRITE(vPin) callback on the receiving device.

Pete.

You misunderstand, Pete. Without Bridge being involved, the test code that I wrote would work fine on Bridge Legacy.

If you post the code properly then I’ll look at it.

Pete.

#define BLYNK_TEMPLATE_ID "TMPLFqSIu36y"
#define BLYNK_DEVICE_NAME "Mailbox Monitor"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#define SwitchPin D3

#define USE_NODE_MCU_BOARD
#include "BlynkEdgent.h"
#include <PushButton.h>

PushButton physicalButton(SwitchPin);



BLYNK_WRITE(V2) {   // switch
  Serial.println("got the V2");  // I get this only when I press the button on the iPad.
  //                             // The Blynk.virtualWrite(V2, 1); does not get here

}


void setup()
{
  // Debug console
  Serial.begin(115200);
  delay(1000);
  
  BlynkEdgent.begin();
  
  pinMode(SwitchPin, INPUT_PULLUP);

  physicalButton.setActiveLogic(LOW);

}


void loop()
{
  BlynkEdgent.run();

  physicalButton.update();
  if (physicalButton.isClicked() ) {
    Serial.println("button clicked");   // I get here each time I press the physical button
    Blynk.virtualWrite(V2, 1);          // This should activate the BLYNK_WRITE(V2) code, but it doesn't
  }

}

Would you like to post the Blynk Legacy version of this same sketch, along with the serial output that proves that it works as you said under Blynk Legacy?

Pete.

Color me embarrassed! The code I remembered actually was setting leds on the iPad app. You are correct - it works the same. I’m so sorry to have wasted your time and apologize for taking such a negative view. I’ll be more careful in the future.

No problem!

Pete.