Installation de Node.js et Node-RED sur Raspberry PI

From air
Jump to navigation Jump to search


ssh pi@192.168.0.10

Installation de Node.js

Lire http://raspberryalphaomega.org.uk/2014/06/11/installing-and-using-node-js-on-raspberry-pi/

sudo su -
cd /opt
wget http://nodejs.org/dist/v0.11.12/node-v0.11.12-linux-arm-pi.tar.gz
tar xvzf  node-v0.11.12-linux-arm-pi.tar.gz
ln -s  node-v0.11.12-linux-arm-pi node-v0.10.25 node
chmod a+rw /opt/node/lib/node_modules
chmod a+rw /opt/node/bin
echo 'PATH=$PATH:/opt/node/bin' > /etc/profile.d/node.sh
^D
node --version
npm --version
sudo npm install -g node-gyp

Au 2014/10/15, la version 0.10.25 est la version la plus récente de Node.js qui fonctionne correctement sur Raspberry Pi

Installation de Node-RED

Lire http://nodered.org/docs/hardware/raspberrypi.html

NODERED_VERSION=0.9.0
wget https://github.com/node-red/node-red/archive/${NODERED_VERSION}.zip
unzip ${NODERED_VERSION}.zip
rm ${NODERED_VERSION}.zip
cd node-red-${NODERED_VERSION}
npm install --production

Installez Wiring PI http://wiringpi.com/download-and-install/

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git-core
git clone git://git.drogon.net/wiringPi
gpio -v
gpio readall

Installez la dépendance rpi-gpio

npm install rpi-gpio


Créez flow-gpio.json avec

[{"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":[]}]

Lancez Node

node --max-old-space-size=128 red.js flow-gpio.json

Naviguez sur http://192.168.0.10:1880

Extra: Banchement d'un Arduino

Installez la dépendance firmata si vous prévoyez de connecter un Arduino

npm install firmata

Deployez ce Flow flow-blink.json

[{"id":"d7663aaf.47194","type":"arduino-board","repeat":"25","device":"/dev/ttyUSB0"},{"id":"8c09ca6c.a975d","type":"arduino out","name":"","pin":"13","state":"OUTPUT","arduino":"d7663aaf.47194","x":509.16667556762695,"y":162.16666984558105,"wires":[]},{"id":"e37b6a97.610968","type":"inject","name":"tick","topic":"","payload":"","repeat":"0.5","once":false,"x":116.16668319702148,"y":62.16666507720947,"wires":[["60b4aeaa.800d58"]]},{"id":"60b4aeaa.800d58","type":"function","name":"Toggle output on input","func":"\n// initialise level as a context variable if currently undefined \n// (context variables persist between calls to the function)\ncontext.level = context.level || false;\n\n// if it's a 0 make it a 1 else make it a 0...\ncontext.level = !context.level;\n\n// set the payload to the level and return\nmsg.payload = context.level;\nreturn msg;","outputs":1,"x":298.1666793823242,"y":113.16665458679199,"wires":[["8c09ca6c.a975d"]]}]


Naviguez sur http://192.168.0.10:1880

Configuration de Node-RED en service

Lire la section Making Node-RED autostart on boot (optional) dans http://nodered.org/docs/hardware/raspberrypi.html

Copiez le script https://gist.github.com/Belphemur/cf91100f81f2b37b3e94 dans /etc/init.d/node-red