Correct use of BLYNK_WRITE

I think we have different aspects of the same problem. In my case, I have this in my sketch:

BLYNK_WRITE(D12) {
poolState = param.asInt();
}

and the value of poolState never changes, although in my iPhone app I have a button tied to D12, and although I have electronics tied to physical D12 and they see the pin toggle.

Did you ever find an answer?

I’ve moved this to a new topic, rather than having it tagged on to the end of a 4 year old topic that is not connected to your issue.

You can only use BLYNK_WRITE with Virtual pins, not Digital pins:

BLYNK_WRITE(vPIN)

BLYNK_WRITE is a function called every time device gets an update of Virtual Pin value from the server (or app):

To read the received data use:

BLYNK_WRITE(V0)
{   
  int value = param.asInt(); // Get value as integer

  // The param can contain multiple values, in such case:
  int x = param[0].asInt();
  int y = param[1].asInt();
}

BLYNK_WRITE can’t be used inside of any loop or function. It’s a standalone function.

Note: For virtual pins with numbers > 127, please use BLYNK_WRITE_DEFAULT() API

https://docs.blynk.cc/#blynk-firmware-blynktimer-blynk_writevpin

If you want to read the value of a digital pin then you would need to do that in exactly the same way as if you weren’t using Blynk - either with an interrupt or by polling the pin with a timer.

In reality, you’d be better using a virtual pin, declaring your digital pin mode in void setup, then changing your digital pin value with a digitalWtite or analogWrite command triggered from your BLYNK_WRITE(vPin) callback.

Pete.

I’m hoping we have different aspects of the same problem. In my case, I have this in my sketch:

BLYNK_WRITE(D12) {
poolState = param.asInt();
}

but the value of poolState never changes, although in my iPhone app I have a button tied to D12, and although I have electronics tied to physical D12 and they see the pin toggle.

Is my problem that I’m not permitted to read the digital value? What would determine that?

I’ve also moved your same question asked in a different topic to this new topic.

In future, it’s best to start a new topic and provide all the relevant information, and provide links to what you think are related topics if appropriate, rather than asking the same question in multiple places - that just leads to disjointed responses and conversations split across multiple topics.

Pete.

Thanks. I appreciate the guidance.

I think I’m still somewhat confused. I have a heartbeat LED via D13, and I’m setting that to on/off successfully with digitalWrite. That and the next line are

  digitalWrite(ledPin, ledState);
  Blynk.virtualWrite(V0, ledState);

If I understood correctly, the LED I have in my app tied to V0 would blink along with the LED on the board, but in practice it’s always off. What did I miss?

Probably not quite off all the time, but the On brightness will be so low that it’s almost imperceptible.

LED widgets have 255 levels of brightness. You’re setting it to level 1, which is very, very dim.
Try …

  Blynk.virtualWrite(V0, ledState x 255);

This should give you 0 x 255 = 0 or 1 x 255 = 255

Pete.

1 Like

Worked just as you knew it would. Thanks again!

1 Like