Send "On" or "Off" from button

I don’t seem to be able to change the default behaviour of a “button”?

I don’t want to send “1” or “0”, I want to be able to send “on” or “off”

Is this possible?

Not directly… it is a button not the Terminal :stuck_out_tongue_winking_eye:

But you can get the required results with a bit of code. Why do you need “on” and “off”, and where are you sending these strings?

I am sending the string to my MQTT server and then onto my LINGAN SWA1 plugs… Which like to hear “on/off”.

I guess I will have to write a small function to convert from 1/0 to on/off… but was hoping that the button would have the ability?

String ButtonValue;

BLYNK_WRITE(Vx) {
  if (param.asInt() == 1) {
    String ButtonValue = "On";
  } else {
    String ButtonValue = "Off";
  }
  // Send string ButtonValue to your MQTT server
}

No change since my last post :stuck_out_tongue:

Ok - I am going to ask a really stupid newbie question.

Where do I put that code?

That’s OK… I know nada about MQTT :stuck_out_tongue: How would you normally send the value, if not via code?

The String ButtonValue; goes in your pre-setup of your Blynk sketch

And the function can go anywhere else, as long as is is NOT in another function or loop. So stick it after the void loop() or something like that.

Ah, apologies.

I explained incorrectly. I am actually using Node-Red. So the message comes in from the Blynk app into Node-Red, then I need to convert it from 1/0 to on/off and then forward the on/off to the plugs using MQTT…

I will write a small function in Node-Red

Thanks for your help!

1 Like

Well, at least one of us know what to do now :stuck_out_tongue: I didn’t know the App could talk directly to Node-Red… I figured all that info was sent via Webhook commands in the Blynk sketch. But then I know nada about Node-red as well :confused:

Yes, there is a Blynk “Pallette” that plugs straight into Node-Red.

1 Like

Well in that case, if you can send direct vPin data to this Palette, then just use Eventor to send your string message…

image

I am just using my vPins for the Button and the Terminal to confirm the process worked… it did.

image

Hi Trevor,
The easiest way is to handle it in Node-Red with a Change Node:

It’s also a very handy way of converting 1 or “On” to 255 when you want to turn an LED widget on.

You could also use a Node-Red Function Node that contains this code:

if(msg.payload === 0)
{
    msg.payload="Off";
    return msg;
}


if(msg.payload === 1)
{
    msg.payload="On";
    return msg;
}

instead of the Change Node.

Pete.

1 Like

Perfect!

Thanks Pete!