Skip to content

Instantly share code, notes, and snippets.

@rc1
Last active April 29, 2022 09:33
Show Gist options
  • Select an option

  • Save rc1/add47f93b7b91d9f4e9c to your computer and use it in GitHub Desktop.

Select an option

Save rc1/add47f93b7b91d9f4e9c to your computer and use it in GitHub Desktop.
Wiring-pi for Node-Red on Raspberry Pi 2 (July 2015)

Installing the wiring-pi module for node-red on Raspberry Pi 2

July 2015

Note: the wiring-pi module for node.js is bindings around the c wiring-pi SDK, and not the same as wiring-pi

Step 1

Locate the node-red root folder (where settings.js is). You can use locate if it is installed

$ locate settings.js

This example assumes it is installed in /usr/lib/node_modules/node-red/

Step 2

Install this fork of the wiring-pi module which work on the RPi 2

$ cd /usr/lib/node_modules/node-red/
$ npm install git://github.com/nekuz0r/wiring-pi.git#a186b9ce0135ca

Step 3

Require the wiring-pi module in the settings.js file.

Edit the file with nano

$ nano settings.js

Add the following line below functionGlobalContext

functionGlobalContext: {
    wpi: require('wiring-pi')

Step 4

Start node-red as sudo (wiring-pi needs sudo)

$ sudo node-red-pi --max-old-space-size=128

Step 5

Go to node-red in your browser. Select "Import from Clipboard" and paste the following:

[{"id":"ccd669e9.332998","type":"function","name":"Toggle LED on input","func":"\n// select wpi pin 0 = pin 11 on header (for v2)\nvar pin = 0;\n\n// initialise the wpi to use the global context\nvar wpi = context.global.wpi;\n\n// use the default WiringPi pin number scheme...\nwpi.wiringPiSetup();\n\n// initialise the state of the pin if not already set\n// anything in context.  persists from one call to the function to the next\ncontext.state = context.state || wpi.LOW;\n\n// set the mode to output (just in case)\nwpi.pinMode(pin, wpi.OUTPUT);\n\n// toggle the stored state of the pin\n(context.state == wpi.LOW) ? context.state = wpi.HIGH : context.state = wpi.LOW;\n\n// output the state to the pin\nwpi.digitalWrite(pin, context.state);\n\n// we don't \"need\" to return anything here but may help for debug\nreturn msg;","outputs":1,"noerr":0,"x":458,"y":229,"z":"5cce63ad.a3319c","wires":[["19c35ac7.e63ca5"]]},{"id":"9fe943da.6016c","type":"inject","name":"tick","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":238,"y":209,"z":"5cce63ad.a3319c","wires":[["ccd669e9.332998"]]},{"id":"19c35ac7.e63ca5","type":"debug","name":"","active":true,"x":678,"y":249,"z":"5cce63ad.a3319c","wires":[]}]

Note: That code above adds a node called "Toggle LED on input" with the following wiring-pi code. See here to find out more you can do with the wiring pi module

// select wpi pin 0 = pin 11 on header (for v2)
var pin = 0;

// initialise the wpi to use the global context
var wpi = context.global.wpi;

// use the default WiringPi pin number scheme...
wpi.wiringPiSetup();

// initialise the state of the pin if not already set
// anything in context.  persists from one call to the function to the next
context.state = context.state || wpi.LOW;

// set the mode to output (just in case)
wpi.pinMode(pin, wpi.OUTPUT);

// toggle the stored state of the pin
(context.state == wpi.LOW) ? context.state = wpi.HIGH : context.state = wpi.LOW;

// output the state to the pin
wpi.digitalWrite(pin, context.state);

// we don't "need" to return anything here but may help for debug
return msg;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment