-
Notifications
You must be signed in to change notification settings - Fork 0
ReadMe Raspberry Pi Advanced
#Raspberry Pi - Advanced This version of working with the Raspberry Pi uses the WiringPi libraries so gives you much richer control of all Pi GPIO pins and also access to the PiFace expansion board. For more details about WiringPi see http://wiringpi.com/
##Installation
Wiring-Pi - download, install, configure and test Wiring Pi as per the website - http://wiringpi.com/download-and-install/
Node-RED - as per main INSTALL file, or... in the Pi terminal
cd ~
wget https://github.com/node-red/node-red/archive/master.zip
unzip master.zip
cd node-red-master
npm install
npm install wiring-pi
Edit the node-red-master/settings.js file to contain
//functionGlobalContext: { }
functionGlobalContext: { wpi:require('wiring-pi') }
This makes the wiring-pi library available to any functions you write. As we are now accessing the library globally, there are NO new nodes added to the palette. All the interaction now has to happen inside function blocks. This is why we call this advanced :-) It is accessible by declaring in within a function (for example) :
var wpi = context.global.wpi;
Then start Node-RED,
cd node-red-master
node red.js
then browse to http://{your-pi-address}:1880
##Blink
To run a "blink" flow that uses the WiringPi pin 0 - Pin 11 on the header - as per Gordon's blink sketch. https://projects.drogon.net/raspberry-pi/gpio-examples/tux-crossing/gpio-examples-1-a-single-led/
Import the following flow, by cutting the line below into your clipboard.
[{"id":"860e0da9.98757","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.setup();\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.modes.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,"x":333.16666412353516,"y":79.16666793823242,"wires":[["574f5131.36d0f8"]]},{"id":"14446ead.5aa501","type":"inject","name":"tick","topic":"","payload":"","repeat":"1","once":false,"x":113.16666412353516,"y":59.16666793823242,"wires":[["860e0da9.98757"]]},{"id":"574f5131.36d0f8","type":"debug","name":"","active":true,"x":553.1666641235352,"y":99.16666793823242,"wires":[]}]
Then in the Node-RED browser - Use Options (top right) - Import From - Clipboard, or the shortcut Ctrl-i, and paste in the text, Ctrl-v.
Hit Deploy - and the flow should start running. The LED should start toggling on and off once a second.
##Making Node-RED autostart on boot (optional)
The easiest way to autostart Node-RED is to use screen so you can easily get to the console at any time. To install screen - if not already there.
sudo apt-get install screen
Then edit the etc/rc.local file to include the lines
cd /home/pi/node-red-master
screen -dmS red node red.js
This assumes that you have installed Node-RED to the default (pi) user's home directory.
Once you reboot you should then be able to type
screen -ls
and see red listed. To get to the terminal type
screen -r red
type Ctrl-A-D to detach and leave it running