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
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/
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
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')
Start node-red as sudo (wiring-pi needs sudo)
$ sudo node-red-pi --max-old-space-size=128
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;