How to use button in dashboard to toggle Led in raspberry pi?

Hi,
i want to add a button in a dashboard which toggle a led in my raspberry pi.
I know it works with switch but i want a button.

i tried this:

i have 2 leds in gpios output
i used 2 inject nodes for on and off

here is ths json:

[{"id":"75c0100d.c45f6","type":"inject","z":"a27ded96.41535","name":"ON","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":130,"y":180,"wires":[["383593a3.09489c","c888f03e.9b134"]]}]

You didn't paste the code correctly. Use the </> button.

This is a very quick bit of code I wrote.
Not knowing what the LED wants (in terms of signals) I just used true and false.

I didn't use a button node, but an inject only in that you can see it all happening there in the edit screen.

Hook the output of the function node to the GPIO node and edit the code in the function node if needed.

Pressing the inject button will toggle the output from false to true to false to true as you press it.

Hope that helps.

Hi,
edit the question.
me too i used inject and it works.
but in dashboard i can use only a switch.
If i add a button instead of a switch in dashboard it happens nothing.

Silly me, I forgot to paste the flow I wrote.

[{"id":"fbc0b791.beb0b8","type":"inject","z":"8aae5d25.29cda","name":"BUTTON NODE","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":780,"wires":[["617bd3ee.854e0c"]]},{"id":"617bd3ee.854e0c","type":"function","z":"8aae5d25.29cda","name":"","func":"//  Modify the `false` and `true` to what works\nlet x = context.get(\"count\") || 0;\nif (x === 0)\n{\n    msg.payload = false;\n} else\nif (x === 1)\n{\n    msg.payload = true;\n}\nx = (x + 1) % 2;\ncontext.set(\"count\",x);\n\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":780,"wires":[["8909dca0.245bf"]]},{"id":"8909dca0.245bf","type":"debug","z":"8aae5d25.29cda","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":730,"wires":[]}]

The button node and the inject node are the same in that they send a message when you press them.

The switch node (dashboard) sends one of two messages, depending on where the switch is.

I think you are confusing yourself with what you want and how to do it.

The flow I have now just posted is a BASIC one. It can be made a lot more fancy and the button also indicate the state of the LED.

That isn't what you asked for.

Try the flow I just posted and explain what is wrong with it.

That is a very confusing statement. only

What do you want the button to do? Do you want it to drive the LED while the button is down or do you want it to toggle, so the first press switches it off and the second switches it off or do you you want it to switch on for certain time when you press it and then off, or something else?

If you want it to toggle then does a ui_switch node do everything you want except that it looks like a switch instead of a button?

Hi,

i'm going to explain:
On electronic side:

  1. i have a Led connected to a gpio in a raspberry pi.
  2. there is no physically a button in the breadboard, only a led.

On node-red side:
i want to create a dashboard with a button which will when i press the first it should switch on the Led, on second press it should switch off the Led.

That's all.

What i tried:
in node-red, i created a dashboard and added a button node, inject node.
It happens nothing.
If i replace the button by a switch (on/off) it works fine.

Use the flow I posted and connect the output of the function node to the GPIO node input.

1 Like

It works !
Thanks a lot now i can see a button in my dashboard and i can send command to the gpio out.
We can close this thread.