How to deploy Node-RED flows with Isaax

You probably heard about Node-RED. Most likely you are a passionate fan of it already. You can manage a Node-RED instance with ease out of the box, but what if you need to deploy flows on tens or hundreds of devices? Isaax Cloud to the rescue! In this article we’ll learn how to set up an Isaax project so that it can run Node-RED flows as systemd service and update it just by committing to a github repo.

What is Node-RED?

I’m not going to go through the basics of Node-RED, this is out of scope of this article. If you don’t know what it is – I urge you to go to nodered.org immediately! Seriously, you are missing out on a great piece of tech. Node-RED is a simple tool for IoT automation, but, I daresay, it looks like the future of coding. You can:
  • represent your program as a flow of wired nodes;
  • pick a node for just about any task from a vast library, just import new nodes via NPM or write your own;
  • manipulate flows in any imaginable way: have multiple flows run together, link them, make them share data, use any NPM package within nodes and much more;

Getting flows ready

I’ve prepared a simple flow that injects a timestamp into a function node where it is formatted via MomentJS, which then passes the formatted date and time to a johnny5 node, which in turn displays it on a 2×16 LCD mounted on top of a NodeMCU connected via Firmata: By the way this flow results in a nice LCD watch: Now if we import this flow into any Node-RED runtime it will work just fine. But what if we need this running (and maintained!) on hundreds of devices? What if those devices left our local network and are scattered somewhere all over the globe (but still have internet access)? We can utilize Isaax Cloud to deploy our flow on a cluster of devices!

Setting up a device

To run Node-RED, you will need Node.js and Node-RED installed on your device. You can find exact instructions on the Node-Red website. Since we want Node-RED to be started on boot and reloaded on every update we need to introduce some form of process supervision. This can easily be done via pm2. PM2 is a node.js process control package, very similar to the popular supervisord. But with Isaax we don’t actually need to worry about it all that! Isaax agent will turn our Node-RED flowinto a systemd service that will be autostarted on boot and follow the usual systemctlcommands. We just need to give Isaax a proper start script so lets see how to do that.

Creating Isaax project

You can learn how to create an Isaax Project here. A new project always comes with a default cluster with which we can manage our device fleet. For creating a project we need a Github (or Bitbucket) repository so that Isaax can see our updates and deploy them on a cluster. Here is an example with the above mentioned flow: https://github.com/yentsun/node-red-watch. The important thing to notice is the mandatory isaax.json file in the repo. This file essentially tells isaax-agent what to run and how to name the systemd service:
{
  "name": "node-red-watch",
  "version": "0.1.0",
  "description": "A sample Node-RED flow, displaying date and time on a 2x16 LCD",
  "main": "info-watch.json",
  "author": "Max Korinets",
  "license": "MIT",
  "language":"json",
  "scripts": {
    "start": "node-red -v --userDir="./" --settings settings.js info-watch.json"
  },
  "dependency": null
}
Resembles npm’s package.json, huh? Look at the start script – we are explicitly setting userDir (for some reason I couldn’t get the script to work without it), the settings and our flow file. This is assuming you installed Node-RED globally with npm as root user.

Setting-up Isaax scripts

Now, we wouldn’t need to do anything else if we didn’t add any extra nodes or npm dependencies to our flow. But we have this in our settings.js:
  functionGlobalContext: {
    moment: require('moment')
  }
This code adds moment package as a globally accessible object. We have added moment as a dependency in our package.json, so we just need an npm install command before Isaax agentupdates our service. Enter Isaax scripts! Lets add a simple pre-update script in our Isaax project pane (tab SHELL SCRIPT):
#!/bin/bash
echo "Installing npm dependencies..."
npm install
exit 0
That’s it. Now with every update via Isaax we’ll have our dependencies (declared in package.json) installed.

Registering devices

Before we can see our devices updated via Isaax we need to add them to a project’s cluster. There is a tutorial on how to do this here.

See it in action

Ok, we have our Isaax project and a device with a running Isaax agent in place, time to put all our work together and see the magic of Isaax cloud:
  1. Add some changes to your flow (on your flow design machine)
  2. Export the flow into your repo (a simple way is just copy/paste from clipboard), don’t forget to declare any new dependencies in the package.json and commit it
  3. Observe your device output in Isaax device logs pane:
  4. Give the device some time and watch the update take effect.
So just imagine you had a swarm of devices attached to an Isaax cluster – they would all be updated at once. Now you are ready to run your industrial production line with the beauty of Node-RED flow design and power of Isaax Cloud deployment… WRITTEN BY: Max Korinets. Max is an IoT developer and evangelist at isaax.io.]]>

上部へスクロール