Set RPI GPIO OUT pin dynamically

Hi,

Newbie question:

How can I set the pin number of a gpio out node using a function (or something else)?

A short context:

I want to get "recipes" from a MySQL database and send high/low signals to a series of relays. The pin no information, duration etc will come from the database.

Thanks,

Ole I.

I recently came up with this (poor but works) idea :slight_smile:

[{"id":"d1a4329.d13a1d","type":"rpi-gpio out","z":"1d6ca75e.4c2489","name":"","pin":"8","set":"","level":"0","freq":"","out":"out","x":790,"y":160,"wires":[]},{"id":"4391f16d.244f1","type":"function","z":"1d6ca75e.4c2489","name":"Switch to Pin","func":"node.status({fill:\"green\", shape:\"ring\", text:\"start\"});\nvar pinArray = [];\nvar loop;\nfor (loop =1;loop < msg.pin;loop++) {\n    pinArray.push(null);\n}\nnode.status({fill:\"red\", shape:\"ring\", text:[msg.pin,msg.payload]});\npinArray.push(msg);\nnode.send(pinArray);\n","outputs":40,"noerr":0,"x":470,"y":320,"wires":[[],[],["8f1bf80c.e0a4c8"],[],["d4b3eb1a.6a7148"],[],["f2d5c497.a96748"],["d1a4329.d13a1d"],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]},{"id":"52d1218.b23d1e","type":"inject","z":"1d6ca75e.4c2489","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":200,"wires":[["c4b76c47.7eec5"]]},{"id":"8f1bf80c.e0a4c8","type":"rpi-gpio out","z":"1d6ca75e.4c2489","name":"","pin":"3","set":"","level":"0","freq":"","out":"out","x":650,"y":20,"wires":[]},{"id":"d4b3eb1a.6a7148","type":"rpi-gpio out","z":"1d6ca75e.4c2489","name":"","pin":"5","set":"","level":"0","freq":"","out":"out","x":650,"y":80,"wires":[]},{"id":"f2d5c497.a96748","type":"rpi-gpio out","z":"1d6ca75e.4c2489","name":"","pin":"7","set":"","level":"0","freq":"","out":"out","x":650,"y":140,"wires":[]},{"id":"c4b76c47.7eec5","type":"function","z":"1d6ca75e.4c2489","name":"Write code here","func":"msg.pin = 8;\nmsg.payload = 1;\nreturn msg;","outputs":1,"noerr":0,"x":240,"y":320,"wires":[["4391f16d.244f1"]]},{"id":"eb59086b.b605d8","type":"function","z":"1d6ca75e.4c2489","name":"Write more code here","func":"msg.pin = 8;\nmsg.payload = 0;\nreturn msg;","outputs":1,"noerr":0,"x":260,"y":380,"wires":[["4391f16d.244f1"]]},{"id":"959ff2f1.8c54","type":"inject","z":"1d6ca75e.4c2489","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":440,"wires":[["eb59086b.b605d8"]]}]

So in a function node feeding the switch you can just specify what pin you want using msg.pin

image

To control two pins in same function you can do this

image

Apologies for being so dumb. I forgot I wrote this node a long time ago https://flows.nodered.org/node/node-red-node-piliter
(Of course it may no longer work) but it was for driving a small board of 8 LEDs and had various modes so you could drive them all at once, or like a bar chart, or a meter etc. It only handled the base 8 gpio pins but may help.

Thank you both!

I will look into your suggestions asap.

Thank you.

All, picking up this topic again - i find it very hard to believe that there still (Feb.2022, with NR V2.2.0)
is no way to:

  1. read the current OUTPUT state of GPIO without touching it and
  2. pass the GPIO pin or gpio number plus the desired state to 'the magic' node which then sets the output accordingly other than the attached screenshot with a 'split' node and significant amount of gpio nodes, each one dealing with just one pin ...
    Are there any plans to implement such a node soon ?
    Or can anyone provide examples, like creating function node, which deals with gpio pins directly (or via pigpiod) (example code for that would be VERY helpful ...) which would simplify and streamline dealing
    with many (calculated) gpio pins efficiently in a flow?
    Thanks a LOT,
    Martin
    Screenshot _GpIoPinSelect

well - no-one has written the code - feel free to do so.

The main consideration is that as the name suggests most of the pins can be input or outputs or indeed other functions. You could easily get into all sorts of bother if you change type dynamically. In the case you have shown where they are all outputs you could indeed predefine them all as outputs and then feel free to set as you like - but that is not the totally general case. So you would probably need to write a custom node to handle this case. (And somehow ensure no other node also wanted to set them some other way.)

1 Like

Hello dceejay ! thanks 4 quick reply ... i do think that for writing nodes myself i am a bit 'undereducated' - but of course willing to learn ! I did 'compose' a python script, which does what i need, interacting with NR via local MQTT broker - its just that i really like to keep things simple and easy to maintain and therefore was hoping to find something in the repository. Nevertheless, as soon as time allows, i'm going to look into the 'piliter' code and see, if i can develop an output-only node from there, keeping the current state of output pins in nonvolatile context and use that upon redeployment or NR restart (by no means i want to 'hard initialize' output pins).

1 Like