diff --git a/hardware/PiGpio/36-rpi-gpio.js b/hardware/PiGpio/36-rpi-gpio.js index 159f7d54..5a997fb0 100644 --- a/hardware/PiGpio/36-rpi-gpio.js +++ b/hardware/PiGpio/36-rpi-gpio.js @@ -1,42 +1,17 @@ module.exports = function(RED) { "use strict"; + var execSync = require('child_process').execSync; var exec = require('child_process').exec; var spawn = require('child_process').spawn; var fs = require('fs'); + var testCommand = __dirname+'/testgpio.py' var gpioCommand = __dirname+'/nrgpio'; var allOK = true; try { - var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString(); - if (cpuinfo.indexOf(": BCM") === -1) { - allOK = false; - RED.log.warn("rpi-gpio : "+RED._("rpi-gpio.errors.ignorenode")); - } - try { - fs.statSync("/usr/share/doc/python-rpi.gpio"); // test on Raspbian - // /usr/lib/python2.7/dist-packages/RPi/GPIO - } catch(err) { - try { - fs.statSync("/usr/lib/python2.7/site-packages/RPi/GPIO"); // test on Arch - } catch(err) { - try { - fs.statSync("/usr/lib/python2.7/dist-packages/RPi/GPIO"); // test on Hypriot - } catch(err) { - try { - fs.statSync("/usr/local/lib/python2.7/dist-packages/RPi/GPIO"); // installed with pip - } catch(err) { - RED.log.warn("rpi-gpio : "+RED._("rpi-gpio.errors.libnotfound")); - allOK = false; - } - } - } - } - if ( !(1 & parseInt((fs.statSync(gpioCommand).mode & parseInt("777", 8)).toString(8)[0]) )) { - RED.log.warn("rpi-gpio : "+RED._("rpi-gpio.errors.needtobeexecutable",{command:gpioCommand})); - allOK = false; - } + execSync(testCommand); } catch(err) { allOK = false; RED.log.warn("rpi-gpio : "+RED._("rpi-gpio.errors.ignorenode")); diff --git a/hardware/PiGpio/README.md b/hardware/PiGpio/README.md new file mode 100644 index 00000000..b575f73e --- /dev/null +++ b/hardware/PiGpio/README.md @@ -0,0 +1,61 @@ +node-red-node-pi-gpio +===================== + +A set of Node-RED nodes to interact with Pi GPIO using the RPi.GPIO python library that is part of Raspbian. + +It also include a simple node that detect mouse buttons and also keyboard clicks. Note: this +picks up mouse keys direct from the keyboard so should work even when the app does not have +focus, but YMMV. + +If you need servo control then look at the node-red-node-pi-gpiod node as this is a lot more accurate timing wise, and more suitable for driving servos + +## Install + +Either use the Node-RED Menu - Manage Palette option to install, or run the following +command in your Node-RED user directory - typically `~/.node-red` + + npm i node-red-node-pi-gpio + +The python library may also work with other distros on on Pi (like Ubuntu on Pi) - you will need to install the PIGPIO package and run the following commands in order to gain full access to the GPIO pins as this ability is not part of the default distro. + + sudo apt-get install python-pip python-dev + sudo pip install RPi.GPIO + sudo addgroup gpio + sudo chown root:gpio /dev/gpiomem + sudo adduser $USER gpio + echo 'KERNEL=="gpiomem", NAME="%k", GROUP="gpio", MODE="0660"' | sudo tee /etc/udev/rules.d/45-gpio.rules + sudo udevadm control --reload-rules && sudo udevadm trigger + +## Usage + +**Note:** the pin numbers refer the physical pin numbers on connector P1 as they are easier to locate. + +### Input node + +Generates a `msg.payload` with either a 0 or 1 depending on the state of the input pin. + +##### Outputs + + - `msg.payload` - *number* - the level of the pin (0 or 1) + - `msg.topic` - *string* - pi/{the pin number} + +You may also enable the input pullup resistor ↑ or the pulldown resistor ↓. + +### Output node + +Can be used in Digital or PWM modes. + +##### Input + + - `msg.payload` - *number | string* + - Digital - 0, 1 - set pin low or high. (Can also accept boolean `true/false`) + - PWM - 0 to 100 - level from 0 to 100% + +*Hint*: The `range` node can be used to scale inputs to the correct values. + +Digital mode expects a `msg.payload` with either a 0 or 1 (or true or false), +and will set the selected physical pin high or low depending on the value passed in. + +The initial value of the pin at deploy time can also be set to 0 or 1. + +When using PWM mode, the input value should be a number 0 - 100, and can be floating point. diff --git a/hardware/PiGpio/testgpio.py b/hardware/PiGpio/testgpio.py new file mode 100755 index 00000000..57305fd2 --- /dev/null +++ b/hardware/PiGpio/testgpio.py @@ -0,0 +1,7 @@ +#!/usr/bin/python +import sys +try: + import RPi.GPIO as GPIO + sys.exit(0) +except ImportError: + sys.exit(1)