Configuring the Johnny-Five robotics library to work in Node-Red

Node-RED is a wonderfully productive drag and drop development tool for creating Node.js apps. It is especially useful in IoT development since there are tons of useful nodes to interface with your Raspberry Pi. While Node-RED comes with a bunch of python based Raspberry Pi nodes that let you read/write from the GPIO pins, keyboard, and mouse, it does not come out of the box with nodes that let you work with I2C, SPI devices, or various sensors. This is where the Johnny-Five javascript robotics library comes in really handy. It comes with a huge library of pre-built components that work with almost every kind of sensor, servo, LED, button, board out there. Getting the Johnny-Five node to work in Raspbian has been a bit tricky so I thought I would document all the steps to make it work (this is for a Raspberry Pi 2 Model B):

  1. Format a MicroSD card with sdFormatter (you’ll need at least 8GB)

2. Grab a fresh copy of Raspbian from here and burn it or install it via NOOBs

3. The version of node.js that comes with Raspbian is too back level for Raspi-io to work so you’ll need to upgrade it. I uninstalled Node.js, npm, and Node-RED first:

sudo apt-get remove nodered
sudo apt-get remove nodejs nodejs-legacy
sudo apt-get remove npm   # if you installed np

4. Next you will reinstall Node.js

curl -sL https://deb.nodesource.com/setup_8.x | sudo bash -
sudo apt-get install -y build-essential python-rpi.gpio nodejs

5. Then reinstall Node-RED

sudo npm cache clean
sudo npm install -g --unsafe-perm  node-red

6. Make sure the python gpio libraries are up to date (although this doesn’t really help the Rpi.gpio nodes work)

sudo apt-get update && sudo apt-get install python-rpi.gpio

7. Create the nodered.service configuration file and scripts to start and stop Node-RED

sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service -O /lib/systemd/system/nodered.service
sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start -O /usr/bin/node-red-start
sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop -O /usr/bin/node-red-stop
sudo chmod +x /usr/bin/node-red-st*
sudo systemctl daemon-reload

8. edit /lib/systemd/system/nodered.service so that user=root. Save the file. Node-RED needs to run as root because the Raspberry Pi requires root in order to access GPIO, I2C, and other I/O.

9. Install Johnny-Five, Raspi-io, and node-red-contrib-gpio using –unsafe-perm and –force

sudo npm install -g johnny-five --unsafe-perm --force
sudo npm install -g raspi-io --unsafe-perm --force
sudo npm install -g node-red-contrib-gpio --unsafe-perm --force

10. Run ‘sudo raspi-config’ and enable I2C, SPI, serial, one-wire, etc.

11. Reboot your Pi

12. Finally start Node-RED with ‘sudo node-red-start’

13. You can find your IP address using ‘hostname -I’

14. Fire up a browser and navigate to http://ip-address:1880. The Node-RED console will appear. You should find your Johnny5, Gpio input, as well as Gpio output nodes (in yellow).  The first time you use these nodes you will have to configure them to point to your board/bot i.e. Raspberry Pi. You should see a ‘Connected!!!’ status message underneath your node if all goes well

15. If you are using the Johnny5 node, simply add your code into the main function block. The five and board variables should already be created for you

16. Any new nodes you wish to add should be installed in /root/.node-red/node_modules. i.e. run ‘npm install <module>’ from /root/.node-red as root

Note: The original blue colored python based Raspberry Pi GPIO nodes do not seem to work properly after Johnny-Five installed. I get an initial reading then after that it shows a status message of ‘Stopped.’ The error message is ‘ENOTTY Inappropriate iotctl for device’

Grateful acknowledgements to the creators of Johnny-Five @rwaldron, node-red-contrib-gpio @monteslu, and raspi-io @nebrius for all their contributions without which we wouldn’t be able to create fun projects.

Posted in IoT.

4 thoughts on “Configuring the Johnny-Five robotics library to work in Node-Red

  1. Hello Tim. Worked like a charm. Thanks a lot for putting all the steps together, it really helped me to get started.

    small correction, on step 9, its mentioned “sudo npm install -g johny-five –unsafe-perm –force” , i did a copy paste and it took some time for me to get the typo . johny, (n is missing).

    Thanks .

    Like

  2. Wow, i have struggled ages with the original instructions trying to get it to work on a pi 3. finally I reached this page and it works.
    Thanks a lot 🙂

    Like

Leave a comment