py-gpio-input

A simple subflow to control Rapsberry GPIO input.

Dependencies

  • function node
  • python-function node
  • python GPIO library

Usage

The node has a single configuration to set the BCM GPIO pin number to read and the edge (a string that can be falling, rising or both). If an invalid value is set to the edge input, both the edges will be read. Then the subflow will send a message every time an edge change is detected with msg.payload equal to the readen value.

Notes

Still under test to add a proper input debounce and pull-up/down configuration.
[{"id":"bbe23d7aefa92cf3","type":"subflow","name":"PyGpioIn","info":"","category":"Raspberry Pi","in":[],"out":[{"x":660,"y":60,"wires":[{"id":"082f895ec1ca1008","port":0}]}],"env":[{"name":"pin","type":"num","value":"0"},{"name":"edge","type":"str","value":"rising"},{"name":"debounce_ms","type":"num","value":"40"}],"meta":{},"color":"#3FADB5","icon":"node-red/rpi.svg"},{"id":"082f895ec1ca1008","type":"python-function","z":"bbe23d7aefa92cf3","name":"PyGpioIn","func":"from RPi import GPIO\nimport time\n\nGPIO.setwarnings(False)\nGPIO.setmode(GPIO.BCM)\nGPIO.setup(msg['gpioPin'], GPIO.IN, pull_up_down=GPIO.PUD_UP)\n\nif msg['edge'] == \"falling\":\n    GPIO.wait_for_edge(msg['gpioPin'], GPIO.FALLING)\nelif msg['edge'] == \"rising\":\n    GPIO.wait_for_edge(msg['gpioPin'], GPIO.RISING)\nelse:\n    GPIO.wait_for_edge(msg['gpioPin'], GPIO.BOTH)\n    \ntime.sleep(msg['debounce_ms']/1000)\n\nif GPIO.input(msg['gpioPin']) == True:\n    msg['payload'] = True\nelse:\n    msg['payload'] = False\n\ntime.sleep(msg['debounce_ms']/1000)\n\nreturn msg","outputs":1,"x":520,"y":60,"wires":[["df514dc437e13759"]]},{"id":"df514dc437e13759","type":"function","z":"bbe23d7aefa92cf3","name":"GetPin","func":"msg.gpioPin = env.get(\"pin\");\nmsg.edge = env.get(\"edge\");\nmsg.debounce_ms = env.get(\"debounce_ms\");\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":60,"wires":[["082f895ec1ca1008"]]},{"id":"d8a6f522550c1f80","type":"inject","z":"bbe23d7aefa92cf3","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":60,"wires":[["df514dc437e13759"]]}]

Flow Info

Created 3 years, 4 months ago
Rating: 3.6666666666666665 3

Owner

Actions

Rate:

Node Types

Core
  • function (x1)
  • inject (x1)
Other

Tags

  • RaspberryPi
  • GPIO
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option