Pages

Using Resin.io with Node-RED

I’ve been involved with Oxford Flood Network which is currently one of the showcase projects in the recently opened Digital Catapult Centre. For about a year now I’ve been using Node-RED, including developing the Twilio node for sending SMS. Node-RED is being used on the Raspberry Pi as a gateway between the sensor nodes and the backend systems. Building and managing one or two Raspberry Pi is a fairly simple task, but trying to replicate the build for each individual gateway soon becomes a major challenge.

I was at a recent IoT London meetup 37 where it had moved to a new venue and new sponsors. One of the new sponsors is Resin.io who claim to be Making hardware as easy to hack on as the cloud!. I was interested in seeing how Resin.io could be used to deploy changes to multiple Raspberry pi simultaneously and how it could be used with the Oxford Flood network deployments.

To get started you need to follow the basic getting started guide, I’m not going to repeat these steps here as they are documented well by Resin.io themselves. This will ensure that you have a working system and all your ssh keys are correct. They also provide example projects that demonstrate how to create applications running in Node.Js, Python, Java or .NET. What I am describing here is how you can develop Node-RED flows on one Raspberry Pi and push it to multiple Raspberry Pi with a few commands. It is possible to develop using another linux or windows system providing you don’t use any Pi specific file locations and features. As this project uses both of these, the development platform will be a Raspberry Pi.

Requirements

  • Raspberry Pi for development, Model B or B+ is recommended as we are using Ethernet
  • One or more Raspberry Pi for target deployment testing.
  • Compatible USB WiFi modules. I’ve used the Edimax modules successfully
  • SD and microSD memory cards for all the Pi, min 4GB, preferably Class 10 for speed.
  • Any additional hardware, e.g. wireless modules, LEDs etc that are used by your application

Setup Development environment

To develop your Node-RED flows you need your Raspberry Pi working correctly on your network, either connected via WiFi or Ethernet. If you’ve not got Node-RED installed then the instructions at Node-RED describe how to install it.

The git command is needed to manage the code repository and push the code to the Resin.io repository. If its not installed then install with:

sudo apt-get install git

You need SSH keys to authenticate against the Resin.io repository, again this is documented elsewhere, this time at Github. Skip the part where you add the keys to Github. Instead copy the contents of /home/pi//ssh/id_rsa.pub to the SSH key section of the Resin.io preferences option. If you have other keys on Resin.io, for example your github keys from when you initially registered then these can be either removed or left as is.

Create Resin.io App

Create the Resin.io app, in this case I’ve called it NodeRED. Currently only Raspberry Pi is available as the target device.

createapp

To create your devices you need to download a zip file containing the initial base system. This doesn’t do much other than indicate the device is online and able to talk to the Resin.io systems.

newdevice

The download has two options, Ethernet or WiFi. If WiFi is selected then you need to enter the connection details for your WiFi network.

downloadzip

When the zip files have been downloaded, add wifi or eth to the filename to indicate which version was downloaded. The files need to be written to a FAT32 formatted SD card. I have found using a tool called SDFormatter on Windows (link?) and selecting the resize options will restore previously used cards to the full size. Using something like 7-Zip on Windows you can extract the contents of the zip files to the root of the freshly formatted SD card.

Install to target device

The prepared SD cards should be put into a Raspberry Pi and then it can be powered on. The ACT or OK LEDs should flash occasionally as its preparing the SD card. If using a WiFi adaptor, then the LED in this should start to flash when it is connecting to the network. After a few minutes (upto 10!) a new device should appear on the Resin.io dashboard for your application, provided you entered the correct SSID and passphrase.

newdevices

The devices seem to be given random names which can be changed if you dont like what has been given as a default. Clicking through to the device will show more detail. For some reason its not showing an IP address, this could be because its on a local LAN. Something to check with Resin.io.

devicedetail

The page also has a log window which will show application logs when you have your application loaded onto the device. There is a handy IDENTIFY DEVICE button that causes the LED to flash on the selected device. Useful if you have number of identical looking devices.

devicedetail2

Setup Node-RED for device

The way applications are built is not the traditional package it up and deploy to the target. The scripts presented here are used to build an image that is created on the Resin.io system and transferred to the device once its been built. This is how I understand it to work.

The files to build a node.js application, which is what Node-RED is, are created in a git repository on your development Raspberry Pi. Firstly, create your git repository.

pi@raspberrypi ~ $ mkdir resin
pi@raspberrypi ~ $ cd resin
pi@raspberrypi ~/resin $ mkdir nodered
pi@raspberrypi ~/resin $ cd nodered
pi@raspberrypi ~/resin/nodered $ git init
Initialized empty Git repository in /home/pi/resin/nodered/.git/

Files can then be created in the repository using your favourite text editor.

pi@raspberrypi ~/resin/nodered $ ls
deps.sh flows_raspberrypi.json package.json README.md settings.js

deps.sh – This is basically a series of commands to install all the dependancies and packages required to get Node-RED running on the target device. The commands follow the installation instructions from the Node-RED Raspberry Pi page along with a number of other packages to resolve dependancy issues.

#!/bin/bash

set -o errexit

apt-get -q update
apt-get -q -y upgrade
apt-get -q -y install python build-essential python-dev python-pip
# Install python modules
#pip install RPi.GPIO
dpkg -i python-rpi.gpio_0.5.8-1_armhf.deb

git clone git://git.drogon.net/wiringPi
cd wiringPi
./build
cd ..
git clone https://github.com/node-red/node-red.git
cd node-red
cd nodes
git clone https://github.com/node-red/node-red-nodes.git
cd ..
npm install wiring-pi
npm install js2xmlparser
npm install serialport
npm install feedparser
npm install –production

cp -f ../settings.js .
cp -f ../flows_raspberrypi.json .

flows_raspberrypi.json – Copy from your working flows file on your development Pi. The example flow I’ve used is to blink a LED connected to pins 6 and 11, once a second. If you have one connected you should see it start blinking when the application is fully loaded.

package.json – Copy from the Node-RED directory and make the follwoing change:

“scripts” : {
“preinstall”: “bash deps.sh”,
“start”: “node red.js”,
“test”: “./node_modules/.bin/grunt”
},

to

“scripts” : {
“preinstall”: “bash deps.sh”,
“start”: “node node-red/red.js -v”,
“test”: “./node_modules/.bin/grunt”
},

README.md – Simple readme to say what the project is.

settings.js – Copy from the Node-RED directory and make the following change to add the WiringPi functionality:

functionGlobalContext: {
// os:require(‘os’),
// bonescript:require(‘bonescript’),
// arduino:require(‘duino’)
}

to

functionGlobalContext: {
wpi: require(‘wiring-pi’)
// os:require(‘os’),
// bonescript:require(‘bonescript’),
// arduino:require(‘duino’)
}

The files should be added to the git repository:

pi@raspberrypi ~/resin/nodered $ ls
deps.sh flows_raspberrypi.json package.json README.md settings.js
pi@raspberrypi ~/resin/nodered $ git add *
pi@raspberrypi ~/resin/nodered $ git commit -m “First commit” [master (root-commit) 03a4c2d] First commit
5 files changed, 223 insertions(+)
create mode 100644 README.md
create mode 100644 deps.sh
create mode 100644 flows_raspberrypi.json
create mode 100644 package.json
create mode 100644 settings.js
pi@raspberrypi ~/resin/nodered $

The Resin.io remote endpoint needs to be added, the command for this is shown at the top of the Resin.io application page and looks something like this:

git remote add resin git@git.resin.io:yourusername/nodered.git

Next you can push your application to your devices. The first time you use this you may have to confirm the key from the host git.resin.io:

pi@raspberrypi ~/resin/nodered $ git push resin master
The authenticity of host ‘git.resin.io (54.165.162.194)’ can’t be established.
ECDSA key fingerprint is f3:d9:38:97:47.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘git.resin.io,54.165.162.194’ (ECDSA) to the list of known hosts.
Enter passphrase for key ‘/home/pi/.ssh/id_rsa’:
Counting objects: 7, done.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 3.94 KiB, done.
Total 7 (delta 0), reused 0 (delta 0)

At this point there may be a pause of a few minutes before anything else happens. It then starts to build the image and runs through the deps.sh script which may produce familiar output. Keep an eye out for any errors that could make the build fail. So far with this scripts I’ve not had a failure. If all is successful you should see:

—–> Image created successfully!
—–> Uploading image..
Image uploaded successfully!
To git@git.resin.io:yourusername/nodered.git
* [new branch] master -> master
pi@raspberrypi ~/resin/nodered $

The device status should change to downloading and the log window on the Resin.io device page should start to show that it is installing the application.

When complete you should then be greeted by a unicorn and a LED flashing on your Raspberry Pi!

unicorn

Expanding the device logs should give you a full log of what has been going on. The -v option was added to the node command line in order to output any missing module dependencies.

devicelog

Pushing new version of Node-RED

If you find there is an update to Node-RED but your flows haven’t changed then the easiest way to get the latest Node-RED onto your devices is to fake a change by adding a line to the README.md file, committing it and pushing to resin.io.

pi@raspberrypi ~/resin/nodered $ vi README.md
pi@raspberrypi ~/resin/nodered $ git commit -m “refresh” README.md
[master ffb8ae6] refresh
1 file changed, 2 insertions(+)
pi@raspberrypi ~/resin/nodered $ git push resin master
Enter passphrase for key ‘/home/pi/.ssh/id_rsa’:
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 300 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)

Then sit back and watch the build happen.

And finally

As a final test, you can locate the IP addresses of your target devices and using a web browser access the IP address and port 1880 to view the Node-RED screen and the flow that was created.

In future, when you develop new flows on your development Raspberry Pi, to deploy them to all the connected devices its just a matter of updating the flows_raspberrypi.json file in the git repository, committing the changes and pushing the new version to Resin.io. If additional Node-RED modules are needed then these can be added to the deps.sh file in the same way as a regular Node-RED installation.

As Resin.io is still being developed, the screen layouts may be slightly different to those shown here.

The files used in my test repository are available on Github

Bookmark this post: bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Comments are closed.