Destroyed ReadMe Raspberry Pi Advanced (markdown)

Dave Conway-Jones 2014-05-06 13:15:59 -07:00
parent 36d0887bdc
commit dcfe7937d3
1 changed files with 0 additions and 85 deletions

@ -1,85 +0,0 @@
#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 <b>Deploy</b> - 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
## Making Node-RED run forever (on crash or reboot)
The answer should be daemonise it which could be done as a mod to node-red or by using forever.
Forever's ability to restart automatically would be useful, especially during development.
I have tried to get forever working in a stable way on pi but have found it erratic. It did not always daemonise and started throwing stdout onto the console despite the -o or -a switch. I suspect version incompatibilities although npm does not complain.
If anyone has been successful please update this.
EDIT - We've been able to create an init script that uses forever to direct stdout and stderr to log files - I've documented this here: http://l0l.org.uk/?p=179. We've also combined this with using monit to restart node-red automagically. So far we haven't seen any errors relating to the daemonising aspect, although we only switched to using forever approx. six weeks ago (24/7 usage in an embedded application).
Due to my limited understanding of npm I have been in the habit of running sudo npm update after finishing package installations just in case it helps update package dependencies. I'm not sure if that has actually updated anything or not tbh, nor if this sheds any light on the experiences of others, but I'm including it in case it does.