BLYNK_WRITE activation

Hello there friends,
I have a few functions that are quite the same which are defined under virtual button functions - meaning BLYNK_WRITE(1 TO 6).
Beside activating those functions using a push button (they are activated only once) in the app I would like to activate these functions using another method, is there any way to “call” this once without pushing the actual button? I was thinking of using blynk.virtualWrite(pushbuttonpin,high). Would that be the right way to do that?
Thank you in advance.

If you need to change the value then, a combo like this will do the job…

Blynk.virtualWrite(vPin, Value);   // Sends new value
Blynk.syncVirtual(vPin);  // processes the vPin as if the widget itself was touched/moved (App need not be active)

But if you just want to resend the existing (last) value, then just the…

Blynk.syncVirtual(vPin);

…is all your need.

And the vPin obviously needs to be the same one as the function you are “calling”.

So if I want to call that function only once I would need to use
Blynk.virtualWrite(vPin, HIGH);
Blynk.syncVirtual(vPin);
Blynk.virtualWrite(vPin, LOW);
Blynk.syncVirtual(vPin);
otherwise the button will be HIGH forever and the function will run indefinetely right?
I want it to run only once

you need to use only “Blynk.virtualWrite(vPin, HIGH)”

It really depends on what your BLYNK_WRITE code looks like, and what you’re trying to achieve.

BLYNK_WRITE will be triggered once when you run the virtualWrite and sync. If you’ve written a HIGH or 1 to the vPin then your switch widget will be On.
If the code that’s triggered when your BLYNK_WRITE function is triggered with an On value runs an endless loop, then yes, it’ll keep running until you turn the button widget Off in the app or through another virtualWrite/sync.

It may be that you’re not approaching this from the correct angle. If you’re triggering a piece of code with the BLYNK_WRITE function, and you also want to be able to trigger that code from within another part of your code than put your code into its own function. That function can then be called by the BLYNK_WRITE or by calling the function from elsewhere in your code.

Pete.

3 Likes

It guess it depends on what you have running in the associated function, if your Button widget is meant to be momentary, and acts based on a single press, regardless of how long it is held down, and ignores or expects the release (aka NOT a switch) then yes you need to toggle from the OFF state to ON state and back.to OFF again.

As to the sync, it only will be required after the ON “press”, unless your function actually requires to “see” the release.

Basically the virtualWrite just sets the widget’s state and the sync tricks the server into thinking you physically activated it with that last set state.

1 Like

@PeteKnight’s idea is good (way better then mine :blush: ) … you could have a function in a function like this…

BLYNK_WRITE(V0) {  // Blynk Button Widget
 if param.asInt() { // assumes default HIGH... use comparison operators if needing refined control
 MyOtherFunction(); // calls this other function if Button state is HIGH
 } 
}


MyOtherFunction() {
// do your stuffs here
}

This way your stuffs get done by either the Button Press in the App, or the command MyOtherFunction(); to call the function. No need for the other stuff.

2 Likes

How stupid can I be… That makes a lot of sense of course…
I am making changes right now to fit that method and will post the code soon !
A question though - is there a way to get the vitual pin number out of the BLYNK_WRITE function?
meaning something like
BLYNK WRITE(5){
int x = 5
{
getting that 5 somehow

by putting ‘int’ in front of that command, you’re defining it as a local variable with the type of integer. This means that it will only be available within that BLYNK_WRITE function.
Declare the variable at the top of your code, then assign a value to it by using

x = 5

Pete.

@PeteKnight yeah sorry it is a global variable ! the question is how can i get the pin associated with this BLYNK_WRITE function without using the actual number. something like -
BLYNK WRITE(5){
x = pinValue();
{

YOU chose the virtual pin number that you use in any given BLYNK_WRITE(vPin) function… so you should already know what that number it is :stuck_out_tongue: … perhaps you should explain what you are trying to do so that it makes sense to us.

BLYNK_WRITE(1)//Off
{
irSend(1);
}
BLYNK_WRITE(2)//On - Cool 21 ,Fan 2
{
irSend(2);
}
BLYNK_WRITE(3)//On - Auto 27
{
irSend(3);
}
BLYNK_WRITE(4)//On - Drying 23
{
irSend(4);
}
BLYNK_WRITE(5)//On - Fan 2
{
irSend(5);
}
BLYNK_WRITE(6)//On - Cool 24 - Fan 1
{
irSend(6);
}

Is there a way to use a local function variable instead of the X in the irSend(X)?

Not sure if this will work… but there is a bit of an obscure command called BLYNK_WRITE_DEFAULT() that you do NOT use a predefined vPIn and it will figure that out with request.pin;

I have no idea how to properly use that function :blush: but perhaps that command will work with the regular BLYNK_WRITE()??

You test it and let us know.

Well, I am trying to control an IR Led using IRRemote library using blynk app widgets but also by time input widget.
the code over here is the blynk button widget method. I tried doing it with switch instead of IFs and didn’t succeed
What do you think of it?

BLYNK_WRITE(1)//Off
{
  irSend(1);
}
BLYNK_WRITE(2)//On - Cool 21 ,Fan 2
{
  irSend(2);
}
BLYNK_WRITE(3)//On - Auto 27
{
  irSend(3);
}
BLYNK_WRITE(4)//On - Drying 23
{
  irSend(4);
}
BLYNK_WRITE(6)//On - Cool 24 - Fan 1
{
  irSend(6);
}


void irSend (int type){
  if (type==1){
    uint16_t rawData[] = {4360, 4344, 536, 1628, 540, 548, 536, 1632, 536, 1628, 536, 552, 536, 552, 536, 1628, 540, 548, 540, 548, 536, 1628, 540, 548, 540, 548, 540, 1628, 536, 1628, 540, 548, 536, 1632, 540, 548, 536, 1628, 536, 1632, 536, 1628, 540, 1628, 536, 552, 536, 1628, 540, 1632, 536, 1628, 536, 552, 536, 548, 540, 548, 536, 552, 536, 1632, 536, 548, 540, 552, 536, 1628, 536, 1628, 540, 1628, 536, 552, 536, 548, 540, 548, 540, 548, 540, 548, 540, 548, 536, 552, 536, 548, 540, 1628, 536, 1632, 536, 1628, 540, 1628, 536, 1628, 540, 5176, 4360, 4340, 536, 1628, 540, 548, 536, 1632, 536, 1628, 540, 548, 536, 552, 536, 1632, 536, 552, 536, 548, 540, 1628, 536, 552, 536, 548, 540, 1628, 536, 1632, 536, 552, 536, 1632, 536, 548, 536, 1632, 536, 1628, 536, 1632, 536, 1628, 540, 548, 536, 1632, 536, 1632, 536, 1628, 536, 552, 536, 552, 536, 548, 540, 548, 540, 1628, 536, 552, 536, 552, 540, 1624, 536, 1632, 536, 1628, 540, 548, 536, 552, 536, 552, 536, 552, 536, 552, 536, 548, 536, 552, 536, 552, 536, 1632, 536, 1628, 536, 1632, 536, 1628, 540, 1628, 540};
    irsend.sendRaw(rawData, sizeof(rawData) / sizeof(rawData[0]), 38);
    Blynk.notify("AC - Off");
  }
  if (type==2){
    uint16_t rawData[] = {4412, 4284, 544, 1624, 540, 548, 540, 1628, 536, 1632, 536, 548, 540, 548, 540, 1628, 540, 552, 536, 548, 540, 1628, 536, 552, 536, 552, 536, 1632, 536, 1628, 540, 548, 540, 1632, 536, 548, 536, 1632, 536, 552, 536, 1628, 540, 1628, 536, 1632, 536, 1628, 540, 1632, 536, 1628, 536, 552, 536, 1632, 536, 548, 540, 552, 536, 552, 536, 548, 540, 552, 536, 548, 536, 1632, 536, 1632, 536, 548, 540, 548, 540, 548, 536, 552, 540, 552, 536, 1628, 536, 552, 536, 548, 540, 1628, 536, 1632, 540, 1628, 536, 1632, 536, 1628, 600, 5120, 4412, 4284, 540, 1628, 536, 552, 536, 1632, 536, 1632, 536, 548, 536, 552, 536, 1632, 536, 552, 536, 548, 540, 1628, 540, 548, 536, 552, 536, 1632, 536, 1632, 536, 548, 540, 1632, 536, 548, 536, 1632, 536, 548, 540, 1628, 540, 1628, 536, 1632, 536, 1628, 540, 1628, 540, 1628, 536, 548, 540, 1628, 536, 552, 536, 552, 536, 552, 536, 548, 540, 552, 536, 548, 540, 1628, 536, 1632, 536, 552, 536, 548, 540, 548, 536, 552, 536, 552, 540, 1628, 536, 548, 540, 548, 540, 1628, 536, 1628, 540, 1628, 536, 1632, 536, 1632, 536};
    irsend.sendRaw(rawData, sizeof(rawData) / sizeof(rawData[0]), 38);
    Blynk.notify("AC - On, Cool 21 ,Fan 2");
  }
  if (type==3){
    uint16_t rawData[] = {4300, 4350, 500, 1600, 550, 550, 550, 1600, 500, 1650, 500, 550, 550, 550, 500, 1650, 500, 550, 550, 550, 500, 1650, 500, 550, 550, 550, 500, 1650, 500, 1650, 500, 550, 550, 1600, 550, 550, 500, 550, 550, 550, 500, 1650, 500, 1600, 550, 1600, 550, 1600, 550, 1600, 550, 1600, 550, 1600, 550, 1600, 500, 550, 550, 550, 550, 550, 500, 550, 550, 550, 500, 1650, 500, 550, 550, 550, 500, 1650, 500, 1600, 550, 550, 500, 600, 500, 550, 550, 550, 500, 1600, 550, 1600, 550, 550, 500, 550, 550, 1600, 550, 1600, 550, 1600, 600, 5050, 4400, 4250, 500, 1650, 500, 550, 550, 1600, 550, 1600, 500, 550, 550, 550, 500, 1650, 500, 550, 550, 550, 550, 1600, 500, 550, 550, 550, 500, 1650, 500, 1650, 500, 550, 550, 1600, 550, 550, 500, 550, 550, 550, 500, 1650, 500, 1650, 500, 1650, 500, 1600, 550, 1600, 550, 1600, 550, 1600, 550, 1600, 550, 550, 500, 550, 550, 550, 500, 550, 550, 550, 500, 1650, 500, 550, 550, 550, 500, 1650, 500, 1650, 500, 550, 550, 550, 500, 550, 550, 550, 500, 1650, 500, 1650, 500, 550, 550, 550, 500, 1650, 500, 1600, 550, 1600, 550};
    irsend.sendRaw(rawData, sizeof(rawData) / sizeof(rawData[0]), 38);
    Blynk.notify("AC - On, Auto 27");
  }
  if (type==4){
    uint16_t rawData[] = {4360, 4348, 540, 1628, 536, 552, 536, 1628, 540, 1628, 540, 548, 536, 552, 536, 1632, 536, 552, 540, 548, 536, 1628, 536, 552, 536, 552, 536, 1632, 536, 1628, 540, 548, 536, 1632, 540, 548, 536, 552, 540, 548, 536, 1628, 536, 1632, 536, 1632, 536, 1628, 536, 1632, 540, 1624, 540, 1628, 540, 1628, 536, 552, 536, 552, 536, 552, 536, 548, 540, 552, 536, 548, 536, 1632, 536, 552, 536, 1628, 540, 548, 540, 1628, 540, 548, 536, 552, 540, 1628, 536, 548, 540, 1628, 536, 552, 536, 1628, 540, 548, 540, 1628, 536, 1628, 540, 5180, 4356, 4340, 540, 1628, 536, 552, 536, 1632, 536, 1628, 536, 552, 540, 548, 536, 1632, 536, 552, 536, 548, 540, 1628, 536, 552, 540, 548, 536, 1632, 536, 1628, 540, 548, 536, 1632, 540, 548, 536, 552, 536, 552, 536, 1628, 536, 1632, 536, 1628, 540, 1628, 536, 1632, 540, 1624, 540, 1628, 536, 1632, 536, 548, 540, 548, 540, 556, 532, 548, 536, 552, 540, 548, 536, 1632, 536, 548, 540, 1628, 536, 552, 536, 1632, 536, 552, 536, 552, 536, 1632, 536, 548, 536, 1632, 536, 552, 536, 1628, 536, 552, 536, 1632, 536, 1632, 536};
    irsend.sendRaw(rawData, sizeof(rawData) / sizeof(rawData[0]), 38);
    Blynk.notify("AC - On, Dry 23");
  }
  if (type==6){
    uint16_t rawData[] = {4400, 4200, 550, 1600, 550, 550, 500, 1600, 550, 1600, 550, 550, 500, 550, 550, 1600, 550, 550, 500, 550, 550, 1600, 550, 500, 550, 550, 550, 1600, 500, 1650, 500, 550, 550, 1600, 550, 1600, 500, 550, 550, 550, 500, 1650, 500, 1650, 500, 1600, 550, 1600, 550, 1600, 550, 550, 500, 1600, 550, 1600, 550, 550, 500, 550, 550, 550, 500, 550, 550, 550, 500, 550, 550, 1600, 550, 550, 500, 550, 550, 550, 500, 550, 550, 550, 500, 550, 550, 1600, 550, 550, 500, 1600, 550, 1600, 550, 1600, 550, 1600, 550, 1600, 500, 1650, 600, 5050, 4350, 4250, 500, 1650, 500, 550, 550, 1600, 550, 1600, 500, 550, 550, 550, 500, 1650, 500, 550, 550, 550, 500, 1650, 500, 550, 550, 550, 500, 1600, 550, 1600, 550, 550, 500, 1650, 500, 1600, 550, 550, 550, 500, 550, 1600, 550, 1600, 550, 1600, 550, 1600, 500, 1650, 500, 550, 550, 1600, 550, 1600, 500, 550, 550, 550, 500, 550, 550, 550, 550, 500, 550, 550, 550, 1600, 500, 550, 550, 550, 500, 550, 550, 550, 500, 550, 550, 550, 500, 1650, 500, 550, 550, 1600, 550, 1600, 500, 1650, 500, 1650, 500, 1600, 550, 1600, 550};
    irsend.sendRaw(rawData, sizeof(rawData) / sizeof(rawData[0]), 38);
    Blynk.notify("AC - On, Cool 24, Fan 1");
  }
}

Regarding this comment, what will be the best way to make sure a BYLNK_WRITE runs only once even I press the button for 2 seconds?
I thought of making a lastButtonState check, is there a better way?

You could use a countdown timer, but I think I’d just grab the millis value when one of the buttons is pressed then check that this is 2000 greater than the last button press time.

Pete.

Pressing and releasing a button causes the function to run twice (once for each state change). By default it has no way of knowing the duration between press and release, so would only “Do something” once anyhow, even if pressed and held for a long time before releasing.

The act of process of making a button press time sensitive is altogether different and generally involves timers and state checks.

@PeteKnight suggestion is good if you do not want someone pressing the button rapidly to make something happen frequently in a short time.

1 Like

Sorry, I mis-read the requirement.
I was thinking you wanted to ignore multiple presses within a 2 second window.

What @Gunner said is what you need to do (as always!).

Pete.