-
Notifications
You must be signed in to change notification settings - Fork 0
ReadMe BeagleBoneBlack
#Beaglebone Black As the BeagleBoneBlack already has node.js baked into it's default Angstrom OS then some of these tips are optional...
##Updating node.js and bonescript to latest version (optional)
ssh into the BBB as root, and at the prompt
opkg update
opkg install screen nodejs bonescript
This also installs screen which we use as the preferred way to be able to keep an eye on the Node-RED console.
##Installation
As per other distributions.
Edit the node-red/settings.js file to contain
//functionGlobalContext: { }
functionGlobalContext: { bonescript:require('bonescript') }
This makes the entire bonescript library available to any functions you write. They are accessible by declaring in a function (for example) :
var b = context.global.bonescript;
Then manually start Node-RED,
cd node-red
node red.js
then assuming you haven't changed the beaglebone's hostname use a connected browser to browse http://beaglebone.local:1880
##Blink
To run a "blink" flow that uses the USR3 and USR2 LEDs import the following flow, by cutting the line below into your clipboard.
[{"id":"b1fce44.9ce3c18","type":"inject","name":"on","topic":"","payload":"1","repeat":"","once":false,"x":127.16666412353516,"y":109.16666412353516,"wires":[["b2a66e5e.6da6f"]]},{"id":"591aef4d.b55f38","type":"inject","name":"off","topic":"","payload":"0","repeat":"","once":false,"x":127.16666412353516,"y":149.16666412353516,"wires":[["b2a66e5e.6da6f"]]},{"id":"36f2a960.164f76","type":"inject","name":"tick","topic":"","payload":"","repeat":"1","once":false,"x":127.16666412353516,"y":49.166664123535156,"wires":[["7ee460bc.df48e"]]},{"id":"55249ec9.21e61","type":"debug","name":"","active":true,"x":567.1666641235352,"y":89.16666412353516,"wires":[]},{"id":"7ee460bc.df48e","type":"function","name":"Toggle USR3 LED on input","func":"\nvar pin = \"USR3\"\nvar b = context.global.bonescript;\ncontext.state = context.state || b.LOW;\n\nb.pinMode(pin, b.OUTPUT);\n\n(context.state == b.LOW) ? context.state = b.HIGH : context.state = b.LOW;\nb.digitalWrite(pin, context.state);\n\nreturn msg;","outputs":1,"x":347.16666412353516,"y":69.16666412353516,"wires":[["55249ec9.21e61"]]},{"id":"b2a66e5e.6da6f","type":"function","name":"Set USR2 LED on input","func":"\nvar pin = \"USR2\";\nvar b = context.global.bonescript;\n\nb.pinMode(pin, b.OUTPUT);\n\nvar level = (msg.payload === \"1\")?1:0;\nb.digitalWrite(pin, level);\n\nreturn msg;","outputs":1,"x":347.16666412353516,"y":129.16666412353516,"wires":[["55249ec9.21e61"]]}]
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. USR3 LED should start toggling on and off once a second. USR2 LED can be set on or off depending on which button is clicked.
##Making Node-RED autostart on boot (optional)
As Angstrom uses the Upstart way of doing things we need to create a couple of files.
One in /lib/systemd/system called node-red.service containing
[Unit]
Description=Start Node-RED
[Service]
Environment=HOME=/home/root
WorkingDirectory=/home/root
ExecStart=/home/root/go.sh
SyslogIdentifier=Node-RED
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
and one in /home/root called go.sh containing
#!/bin/bash -
ntpdate pool.ntp.org
export NODE_PATH=/usr/lib/node_modules
cd /home/root/node-red/
/usr/bin/screen -dmS red /usr/bin/node red.js
then run the following commands
systemctl enable node-red.service
systemctl start node-red.service
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
Notice that go.sh also tries to set the clock correctly using ntpdate. You shouldn't have to do this but I find the BBB does sometimes take an age to sync up - so forcing it helps.
Other useful hints http://www.gigamegablog.com/2012/01/29/beaglebone-linux-101-configuring-angstrom-linux/