How to create function on rpi node red with two gpio - if gpio1 == 1 && gpio2 ==0 then gpio3=1

I know this is late in the game, but I am just reading this thread.

Isn't that a misake @cymplecy? He wasn't GPIO 3 to be active if GPIO 1 and 2 are == 1.

Your second line has GPIO 3 active if GPIO 1 (ONLY) is == 1

@Trying_to_learn - ignore my private message (I've deleted it) - your right - I got my table wrong :frowning:

No wonder this thread is confusing :slight_smile:

Don't worry.

I am the consummate at making mistakes.

1 Like

how to describe gpio in function?
I gave an example with an arduino.

It is not easy to explain to a beginner who doesn't speak English as first language.

Using logic on two separate sources is not simple to do using Node-RED

Maths and programming languages do not need knowledge of English, Chinese, Swedish, etc. The logic is clear here. This is an elementary description of the inscription of two pins, whether up or down, and a series of consequences. Plus, I'm not that new to getting to this forum.
Greetings.

@devifast - you don't describe a gpio pin in a function node. You need two gpio nodes connected to a 'join' node. That will result in having both both values from the gpio nodes in one message. You can then use a function node to test the two values and output the result to a gpio node pointing to pin 3

Hi again.

I am quickly looking at the RasPI GPIO node.

I can't find a GPIO 1

This is what I see when I open the GPIO node:

GPIO 2 and GPIO3 are there, but are maybe not the best choice to use. They look like they are used but the SPI bus.
Though if that isn't important, no problems.

For the sake of the exercise I will use GPIO 2, 3, and 4.
2 and 3 are the inputs and 4 is the output.

NOTE
the GPIO pins default to sending a 1 when not connected.
The topic nodes: they can be anything you like. I called them the GPIOx where x is the GPIO number.
Do not confuse that with the PIN number.

This flow does NOT work.
I am not smart enough to know how to do the trick where it checks if 'one' and 'two' are both set.

But I am sure you can work that bit out.

[{"id":"9a517fc5.ab935","type":"rpi-gpio in","z":"d29f28a7.551f88","name":"","pin":"3","intype":"tri","debounce":"25","read":false,"x":220,"y":2730,"wires":[["b054583c.f22a88"]]},{"id":"c9661668.04fc2","type":"rpi-gpio in","z":"d29f28a7.551f88","name":"","pin":"5","intype":"tri","debounce":"25","read":false,"x":220,"y":2770,"wires":[["180c6ef0.fa94f9"]]},{"id":"b054583c.f22a88","type":"change","z":"d29f28a7.551f88","name":"GPIO 2","rules":[{"t":"set","p":"topic","pt":"msg","to":"GPIO2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":2730,"wires":[["3b51d656.85415a"]]},{"id":"180c6ef0.fa94f9","type":"change","z":"d29f28a7.551f88","name":"GPIO 3","rules":[{"t":"set","p":"topic","pt":"msg","to":"GPIO3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":2770,"wires":[["3b51d656.85415a"]]},{"id":"58cde312.c6653c","type":"inject","z":"d29f28a7.551f88","name":"Test input 1 1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":2690,"wires":[["b054583c.f22a88"]]},{"id":"5cb46292.7c2e4c","type":"inject","z":"d29f28a7.551f88","name":"Test input 2 1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":2810,"wires":[["180c6ef0.fa94f9"]]},{"id":"1c4e2277.ee397e","type":"inject","z":"d29f28a7.551f88","name":"Test input 1 0","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":2650,"wires":[["b054583c.f22a88"]]},{"id":"5eefa522.aae874","type":"inject","z":"d29f28a7.551f88","name":"Test input 2 0","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":2850,"wires":[["180c6ef0.fa94f9"]]},{"id":"3b51d656.85415a","type":"function","z":"d29f28a7.551f88","name":"Magic happens here","func":"//  Process incoming messages and store in context.\nif (msg.topic == \"GPIO2\")\n{\n    context.set('one',msg.payload);\n}\nif (msg.topic == \"GPIO3\")\n{\n    context.set('two',msg.payload);\n}\n\n//  Set up context variables to store values.\nlet one = context.get('one') || 0;\nlet two = context.get('two') || 0;\n\nif ('one' == 1 && 'two' == 1)    //  You need to work on this line.   As is it doesn't work.\n{\n    //  Create message to be on-sent.\n    msg.payload = \"DO THINGS\";\n} else\n{\n    return;\n    //msg.payload = null;\n    \n}\n\nreturn msg;\n","outputs":1,"noerr":0,"x":540,"y":2750,"wires":[["808b5691.f144e8"]]},{"id":"808b5691.f144e8","type":"debug","z":"d29f28a7.551f88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":750,"y":2750,"wires":[]}]

I am not a good programmer.
Please forgive my mistakes.
But I hope you see what I am trying to explain to you.

Good luck.

Look at pins 1,2,3 rather like pins x, y, z or a, b, c 1,2, and 3 are just an example.
I noticed that people take it verbatim.

Well when you discribe a situation and give an example and don't say that your example it theoretical, what do you expect?

If you want to work with two GPIO pins, then you will have two GPIO nodes and two seperate msg's. To evaluate the contents of the two messages you have to join the msg's together or you could constantly put then into two flow variables.

in eithor case you need to define what happens in all four cases - a simple truth table. The truth table you gave:

gpio1 gpio2 gpio3
0 0 0
0 1 0
1 0 1
1 1 1
says
if gpio 1 is 0 the result is 0 - gpio 2 has no effect on the result
if gpio 1 is 1 the result is 1 - gpio 2 has no effect on the result

If this is the case then you don't need to worry about gpio2. If it's not the case, then you need to correct the truth table to display the actual results you need to see.

So you eithor have a situation where
if (gpio1 == 1) {gpio3 = 1} else {gpio3 = 0}
looking at your arduino code I think your truth table should be:

gpio1 gpio2 gpio3
  0     0     0
  0     1     0
  1     0     0
  1     1     1

which would result in an if statement like:
if (gpio1 == 1 && gpio2 == 1 ) {gpio3 = 1} else {gpio3 = 0}

It's supposed to happen, but how do I write into function. Thanks
if (gpio1 == 1 && gpio2 == 1 ) {gpio3 = 1} else {gpio3 = 0}

Have you done what I told you to do in the very first response in this thread?

Thanks to all of you who have responded to this issue. Maybe at the beginning I did not correctly describe the problem for which I apologize. In the end, I achieved the result I expected.
Special thanks to zenofmud and Colin. Thanks.
Here is the end result.

[{"id":"db1e7864.8ec9b8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"2b79041b.1cdbdc","type":"function","z":"db1e7864.8ec9b8","name":"","func":"if (msg.payload.gpio1 == 1 && msg.payload.gpio2 == 1) {\nmsg.payload = 1;\nreturn msg;\n}\nelse{\n msg.payload = 0;\nreturn msg;\n}\n\n ","outputs":1,"noerr":0,"x":490,"y":120,"wires":[["fe139b61.8e5248"]]},{"id":"fe139b61.8e5248","type":"debug","z":"db1e7864.8ec9b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":670,"y":120,"wires":[]},{"id":"19565d15.04c483","type":"inject","z":"db1e7864.8ec9b8","name":"gpio1 on","topic":"gpio1","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":160,"wires":[["2f810a4d.e5da16"]]},{"id":"2f810a4d.e5da16","type":"join","z":"db1e7864.8ec9b8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":370,"y":200,"wires":[["d7fb43c2.8024b","2b79041b.1cdbdc"]]},{"id":"d7fb43c2.8024b","type":"debug","z":"db1e7864.8ec9b8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":200,"wires":[]},{"id":"c7161e7f.1141d","type":"inject","z":"db1e7864.8ec9b8","name":"gpio2 on","topic":"gpio2","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":240,"wires":[["2f810a4d.e5da16"]]},{"id":"9d3d4ee9.66c38","type":"inject","z":"db1e7864.8ec9b8","name":"gpio1 off","topic":"gpio1","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":200,"wires":[["2f810a4d.e5da16"]]},{"id":"1cc91888.72de17","type":"inject","z":"db1e7864.8ec9b8","name":"gpio2 off","topic":"gpio2","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":280,"wires":[["2f810a4d.e5da16"]]}]
1 Like

The joy was brief. With inject node everything is ok but in practice with hardware pins, things don't work out that way. The attached picture shows the messages on the right. look at functions in a flow. Help.Preformatted text

[{"id":"7bbf6c01.b26624","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"2db2a33b.dee8dc","type":"function","z":"7bbf6c01.b26624","name":"","func":"if (msg.payload.gpio1 == 1 && msg.payload.gpio2 == 1) {\nmsg.payload = 1;\nreturn msg;\n}\nelse{\n msg.payload = 0;\nreturn msg;\n}\n\n ","outputs":1,"noerr":0,"x":490,"y":120,"wires":[["e7762b1.e6959d8"]]},{"id":"e7762b1.e6959d8","type":"debug","z":"7bbf6c01.b26624","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":690,"y":120,"wires":[]},{"id":"cf592dd9.6aa23","type":"inject","z":"7bbf6c01.b26624","name":"gpio1 on","topic":"gpio1","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":160,"wires":[["c1ee0201.be6d9"]]},{"id":"c1ee0201.be6d9","type":"join","z":"7bbf6c01.b26624","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":370,"y":200,"wires":[["2257545a.9ea27c","2db2a33b.dee8dc"]]},{"id":"2257545a.9ea27c","type":"debug","z":"7bbf6c01.b26624","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":200,"wires":[]},{"id":"7bddbf31.4d53b","type":"inject","z":"7bbf6c01.b26624","name":"gpio2 on","topic":"gpio2","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":240,"wires":[["c1ee0201.be6d9"]]},{"id":"b6bb8a6.b792678","type":"inject","z":"7bbf6c01.b26624","name":"gpio1 off","topic":"gpio1","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":200,"wires":[["c1ee0201.be6d9"]]},{"id":"50bd9c40.c68974","type":"inject","z":"7bbf6c01.b26624","name":"gpio2 off","topic":"gpio2","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":280,"wires":[["c1ee0201.be6d9"]]},{"id":"407a809e.71686","type":"rpi-gpio in","z":"7bbf6c01.b26624","name":"","pin":"37","intype":"up","debounce":"25","read":false,"x":160,"y":340,"wires":[["384d8adf.464636"]]},{"id":"5efa9c17.e96944","type":"rpi-gpio in","z":"7bbf6c01.b26624","name":"","pin":"35","intype":"up","debounce":"25","read":false,"x":160,"y":400,"wires":[["384d8adf.464636"]]},{"id":"db5c91e3.4380c","type":"function","z":"7bbf6c01.b26624","name":"","func":"if (msg.payload.pi/35 == 1 && msg.payload.pi/37 == 1) {\nmsg.payload = 1;\nreturn msg;\n}\nelse{\n msg.payload = 0;\nreturn msg;\n}\n\n ","outputs":1,"noerr":0,"x":490,"y":320,"wires":[["8c2e7942.8a9ef8"]]},{"id":"8c2e7942.8a9ef8","type":"debug","z":"7bbf6c01.b26624","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":650,"y":320,"wires":[]},{"id":"384d8adf.464636","type":"join","z":"7bbf6c01.b26624","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":330,"y":380,"wires":[["11365f4b.9ab771","db5c91e3.4380c"]]},{"id":"11365f4b.9ab771","type":"debug","z":"7bbf6c01.b26624","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":610,"y":380,"wires":[]}]

Please review my last post. I made a mistake again. Thanks.

Ah the joy of javascript....
your combined payloads look like this
image
all good so far
Then your function does this
if (msg.payload.pi/35 == 1 && msg.payload.pi/37 == 1) {

which is a natural thing to do... but javascript doesn't like / in property names so it will be treating it like a divide :-)... so instead you need to do

if (msg.payload["pi/35"] == 1 && msg.payload["pi/37"] == 1) {

1 Like

Now another problem has arisen. After deploy does not check the status of the pins and even if they are 1 or "high" msg.payload is 0. how to update this information after deploy to read real readings from pins?
Thanks.

If you do a full deploy then I think it should give you the initial status. If you do a partial deploy not including the i/o nodes then they do not know to send the data.

Thanks for all the answers, for other questions I will look for a solution and open a new topic

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.