Writ­ing Custom Add-ons for NodeRED

If you are plan­ning to kick­start your IoT de­vel­op­ment plans but aren’t com­fort­able with any par­tic­u­lar pro­gram­ming en­vi­ron­ment, Node-RED could prove to be just right for you. You can use it to start de­vel­op­ing ap­pli­ca­tions with zero or min­i­mal pro­gram­ming

OpenSource For You - - Contents - By: Ra­jesh Sola The au­thor is a fac­ulty mem­ber of C-DAC’s Ad­vanced Com­put­ing Train­ing School, and an evan­ge­list in the em­bed­ded sys­tems and IoT do­mains. You can reach him at ra­jesh­sola@gmail.com.

Node-RED is a vis­ual wiring tool ini­tially de­signed for IoT needs, but is more of a gen­eral-pur­pose tool these days. It can be used for in­ter­fac­ing sen­sor data, wired/wire­less com­mu­ni­ca­tion, cloud connectivity, data base connectivity, so­cial me­dia alerts, data analysis, sys­tem mon­i­tor­ing and, of course, any Node.js func­tion­al­ity. IBM Bluemix, SenseTec­nic FRED, AT&T Flow and RedCon­nect. io are some of the op­tions for get­ting an in­stance on the cloud with cus­tomised nodes and en­ter­prise grade sup­port. NodeRED forms a part of the In­tel IoT gate­ways as a bridg­ing so­lu­tion. And it also pow­ers IBM Wat­son Ser­vices.

In­stal­la­tion and ini­tial steps

Node-RED can be in­stalled on top of any Node.js run­time us­ing the npm pack­age man­ager, but cer­tain nodes are plat­form-spe­cific (say, with de­pen­den­cies in Linux), and some are pre­cisely tar­geted at boards like Rasp­ber­ryPi for phys­i­cal com­put­ing pur­poses. More nodes can also be added us­ing npm. Node-RED can be easily man­aged us­ing pm2, a process man­ager for Node.js for start­ing and stop­ping the in­stance. It can even run on An­droid us­ing Ter­mux sup­port, and talk to Ar­duino-type of mi­cro­con­trollers through the Fir­mata pro­to­col. Node-RED can be de­ployed on any cloud en­vi­ron­ment with Node.js run­time. Node-RED can also be run in a Docker con­tainer, as doc­u­mented in nodered.org/docs/plat­forms/docker. It pulls the im­age the first time and runs in a con­tainer. You can add all nec­es­sary add-ons to make a custom bun­dle, as men­tioned in the Cus­tomis­ing sec­tion.

Once Node-RED has started, fol­low the Get­ting Started sec­tion of the doc­u­men­ta­tion and cre­ate sim­ple flows us­ing the UI edi­tor avail­able on Port No. 1880 of the host. Data flows across the nodes through in­put, out­put con­nec­tors in the form of JavaScript ob­jects named msg, with msg.pay­load as the key prop­erty; func­tion nodes can be used to pro­vide custom logic in be­tween with min­i­mal JavaScript cod­ing.

Here are some nodes of in­ter­est for pro­to­typ­ing

IoT so­lu­tions:

MQTT out, MQTT in for pub­lish and sub­scribe. Http-re­quest for in­vok­ing REST APIs.

JSON node for stringi­fi­ca­tion or pars­ing.

Trig­ger, de­lay nodes to con­trol the mes­sage flow.

Twit­ter nodes for send­ing or re­ceiv­ing tweets.

Tcp, udp, Web­socket nodes for custom com­mu­ni­ca­tion. Here is a list of some more in­ter­est­ing nodes avail­able through add-ons, for pro­to­typ­ing or build­ing pow­er­ful bridg­ing so­lu­tions:

Dash­board nodes through the node-red-dash­board add-on for quick UI de­vel­op­ment, which can ren­der the view well in desk­top or mo­bile browsers for mon­i­tor­ing and con­trol­ling ser­vices.

Nodes for dis­cov­er­ing and ac­cess­ing CoAP re­sources through coap-re­quest node.

Nodes for pe­riph­eral ac­cess in Rasp­berry Pi, Bea­gleBone Black, In­tel Edi­son, Ar­duino, etc.

Nodes for so­cial me­dia ac­cess to plat­forms like Han­gouts, Tele­gram Bot, Facebook, etc.

Nodes for BLE motes, bea­cons like TI Sen­sortag, Ed­dy­s­tone, the Phys­i­cal Web, etc.

Nodes for data­base connectivity with MySQL,

Mon­goDB, In­fluxDB, etc.

Nodes for plat­form connectivity like IBM Bluemix, Things­peak, Emon­cms, ThingWorx, etc.

Nodes for cloud ser­vices like Azure, AWS, Wat­son ser­vices and Kafka mes­sag­ing.

Nodes for com­mu­ni­cat­ing with OpenHAB,

SiteWhere frame­works.

Nodes for in­dus­trial and au­to­mo­tive needs like Mod­bus, CANBus, OPC UA, etc.

This is just a sub-set of the avail­able nodes; so stay tuned at flows.nodered.org, which gives the cat­a­logue of custom nodes (around 1500 nodes, at present) and ex­am­ple flows, con­trib­uted from time to time. Node-RED can be con­sid­ered for el­e­gant gate­way de­signs and edge com­put­ing with this rich library sup­port. You can iden­tify user con­trib­uted nodes with the nam­ing con­ven­tion of node-red-con­trib-xxxx and nodes de­vel­oped by the Node-RED team with the nam­ing con­ven­tion of node-red-node-xxxx.

An ex­am­ple: Pub­lish­ing sen­sor data to the IoT plat­forms

For bet­ter un­der­stand­ing of Node-RED capabilities, let’s look at an ex­am­ple of pub­lish­ing sen­sor data from a Rasp­berry Pi at­tached with SenseHAT to OpenSen­sors.io with zero cod­ing ef­fort. SenseHAT is add-on hard­ware for Rasp­berry Pi in­tro­duced by the Astro Pi mis­sion, which comes with the fol­low­ing pe­riph­er­als and the Python library built on top of RTIMULib, of­fi­cially main­tained by the Rasp­berry Pi Foun­da­tion. Tem­per­a­ture, hu­mid­ity and pres­sure sen­sors for en­vi­ron­ment mon­i­tor­ing

Ac­celerom­e­ter, gy­ro­scope, mag­ne­tome­ter for mo­tion sens­ing with 9 de­grees of free­dom

Five-but­ton joy stick

8x8 LED ma­trix with RGB sup­port

The re­cent Rasp­bian images come with Node.js and Node-RED pre-in­stalled and for­tu­nately, SenseHAT nodes are also pre-in­stalled in Rasp­bian, through the add-on node-rednode-pi-sense-hat de­vel­oped by the Node-RED team.

OpenSen­sors.io is an IoT plat­form through which de­vices can send the data us­ing MQTT pub­lish or HTTP post, and data can be re­trieved us­ing MQTT sub­scribe or Re­al­time REST API sup­port. To get started, cre­ate an ac­count un­der pub­lisher.opensen­sors.io/lo­gin, go to the De­vices sec­tion, cre­ate a new de­vice with a suit­able name (say, rpisense), the ap­pli­ca­ble tags and, op­tion­ally, geo co­or­di­nates for the de­vice lo­ca­tion. Note down the gen­er­ated client ID and de­vice pass­word. Go to the user pro­file sec­tion and re­quest for a new API key, and note down the gen­er­ated one. You need not use a por­tal lo­gin pass­word for fur­ther data man­age­ment; this de­vice pass­word and/or gen­er­ated API key are use­ful for autho­ri­sa­tion.

All mes­sages are or­gan­ised ac­cord­ing to top­ics as per MQTT in­flu­ence. A topic can be cre­ated on-the-fly based on pub­lished mes­sages or can be cre­ated ex­plic­itly un­der the Top­ics sec­tion. To avoid col­li­sions, topic names start with /users/xxxx/, where xxxx is the user name in the ac­count cre­den­tials. A topic is pub­lic, by de­fault, as this plat­form comes with good sup­port for Open Data poli­cies, so that any one can ac­cess the data from this topic with­out autho­ri­sa­tion. But only an au­then­ti­cated user can

up­date the data. Top­ics and de­vices can also be brought in un­der or­gan­i­sa­tions for bet­ter col­lab­o­ra­tive ap­pli­ca­tions, where ap­pli­ca­ble pub­lic top­ics can be kept un­der suit­able or­gan­i­sa­tions. Re­fer to the API docs once, for the avail­able REST APIs to ac­cess real-time and his­tor­i­cal data.

Cre­ate a new flow, drag the SenseHAT in­put node (or sim­u­la­tor node) from the Rasp­berry Pi cat­e­gory and connect it to a MQTT out node as well as to a de­bug node, as shown in Fig­ure 3, via trig­ger and JSON nodes. Con­fig­ure the SenseHAT node to gen­er­ate only en­vi­ron­ment events by de­s­e­lect­ing mo­tion and joy­stick events. By de­fault, this node emits the msg.pay­load for cer­tain pe­ri­odic in­ter­vals with prop­er­ties like tem­per­a­ture, hu­mid­ity and pres­sure; and mes­sage top­ics such as en­vi­ron­ment, as per the se­lec­tion. Since the data rate is very high, add a trig­ger node in be­tween, with the con­fig­u­ra­tion of Send noth­ing, then wait for 10 sec­onds and then send the orig­i­nal msg.pay­load.

You may add a JSON node from the func­tion cat­e­gory af­ter the trig­ger node to pub­lish data in the JSON for­mat in­stead of the JavaScript ob­ject.

Con­fig­ure MQTT out­put node with the fol­low­ing pa­ram­e­ters. Un­der the Con­nec­tion tab, fill mqtt.opensen­sors. io as the server host name, 1883 as the port num­ber, and the de­vice ID gen­er­ated ear­lier as the client ID. Un­der the Se­cu­rity tab, fill in the user name and the pass­word gen­er­ated on de­vice creation, and fill the topic name as /users/xxxx/ rpisense/weather by re­plac­ing xxxx with your user name.

Sim­u­la­tor node works well with the same steps, ex­cept that you keep chang­ing the sen­sor val­ues man­u­ally un­der the UI tab, i.e., lo­cal­host:1880/sensehat-sim­u­la­tor.

Hit the de­ploy op­tion and mon­i­tor the sen­sor val­ues lo­cally un­der the De­bug tab, as well as pub­lished mes­sages in the Top­ics sec­tion of the OpenSen­sors por­tal. You can also mon­i­tor these mes­sages with MQTT sub­scribe from other de­vices with a sim­i­lar con­fig­u­ra­tion and topic, as this plat­form sup­ports only one

MQTT con­nec­tion per de­vice.

Lo­cal dash­board

You can also build a sim­ple dash­board for lo­cal ac­cess, apart from pub­lish­ing to the cloud. For this, connect the SenseHAT node to a func­tion node, in­crease the num­ber of out­puts to three and fill the fol­low­ing code:

var msg1={}, msg2={}, msg3={}; msg1.pay­load=msg.pay­load.tem­per­a­ture; msg2.pay­load=msg.pay­load.hu­mid­ity; msg3.pay­load=msg.pay­load.pres­sure; re­turn [ msg1,msg2,msg3 ];

Now, each out­put con­nec­tor of this node can be con­nected to suit­able dash­board nodes like gauge, chart, text, etc. Con­fig­ure the dash­board nodes with Rasp­ber­ryPi as the group name, SenseHAT as the tab name and give a suit­able la­bel and value range. Hit De­ploy and open the link host­name:1880/ui to see the sen­sor val­ues lo­cally, as shown in Fig­ure 4.

Writ­ing custom nodes

It’s time to prove that you can also con­trib­ute to Node-RED with your own ad­di­tions. Any Node.js code can be easily wrapped into a custom node with a HTML fron­tend. For a sim­ple ex­am­ple, let’s try to make a node for re­triev­ing data pub­lished to the topic in the above sec­tion. For this, real-time APIs are of­fered, which open a Server Side Events (SSE) stream for a given topic, and an API key is needed (if the topic is not pub­lic), which is gen­er­ated in the Pro­file sec­tion. Given be­low is the con­sole based Node.js code to watch a pub­lic topic with SSE client sup­port.

var Even­tSource = re­quire(‘even­tsource’); var url=re­quire(‘url’); var topic=”/users/xxxx/rpisense/weather”; var ur­lopts=url.parse(“https://re­al­time.opensen­sors.io/v1/

pub­lic/events/top­ics/”+topic); var even­tUrl = new Even­tSource(ur­lopts); even­tUrl.on­mes­sage = func­tion(event) {



Let’s fol­low the steps given be­low to wrap the above code into a Node-RED node.

1. Cre­ate a new di­rec­tory node-red-con­trib-os­fy­demo un­der NODE_PATH, i.e., <topdir-of-nodejs>/lib/node_­mod­ules.

2. From NODE_PATH/node-red/nodes, copy 99-sam­ple. js.demo, 99-sam­ple.html.demo as opensen­sors. js, opensen­sors.html in a sub-di­rec­tory with name opensen­sors un­der node-red-con­trib-os­fy­demo

3. Cre­ate the man­i­fest file based on the pack­age.json as per the guide­lines in nodered.org/docs/cre­at­ing-nodes/ pack­ag­ing.html. Then add even­tsource as a de­pen­dency and re­place “sam­ple”: “sam­ple/sam­ple.js” with “opensen­sors” : “opensen­sors/opensen­sors.js”.

4. Edit opensen­sors.html as fol­lows. Un­der RED.nodes. reg­is­terType, make zero in­puts and one out­put, as this node doesn’t re­ceive any in­puts and only gen­er­ates pay­loads when­ever server side events are trig­gered.

in­puts: 0, out­put:1

We’ll con­sider the pre-ex­ist­ing topic field as the opensen­sors topic, and the name is the de­fault field kept for all nodes.

5. Edit opensen­sors.js as fol­lows. Add the fol­low­ing lines in

the be­gin­ning, in place of the foo en­try:

var Even­tSource = re­quire(“even­tsource”); var myurl=re­quire(‘url’);

Af­ter re­spond to in­puts .... re­move the this.on (‘in­put’, ..... ); and add the fol­low­ing code:

var url=myurl.parse(“https://re­al­time.opensen­sors.io/ v1/pub­lic/events/top­ics/”+this.topic); var even­tUrl = new Even­tSource(url); var msg={}; even­tUrl.on(‘mes­sage’,func­tion(event) { msg.pay­load=event.data; node.send(msg);

}); //or use any call­back style of node.js

Re­name Sam­pleNode as OpenSen­sorsNode and re­place all oc­cur­rences of sam­ple with opensen­sors in both JavaScript and HTML files.

That’s it—the custom node is ready to use now. Res­tart Node-RED and re­fresh the browser page. You can find the new node un­der the in­put cat­e­gory, drag it and connect it to the de­bug node. Fill a suit­able topic name in the Edit panel and de­ploy the flow. You will ob­serve that all the pub­lished mes­sages on this topic are cap­tured by this node. If you are not plan­ning to cre­ate an ac­count ini­tially, try the same with /orgs/os­fy­demo/rpisense/ weather, a pub­lic topic cre­ated for this ex­am­ple.

<div class=”form-row”>

<la­bel for=”node-in­put-is­pub­lic”>

<in­put id=”node-in­put-is­pub­lic” type=”check­box”>

pub­lic topic </la­bel> </div>

Add suit­able ini­tial val­ues un­der de­faults sec­tion in html file

apikey: { value:”xxxx”}, is­pub­lic : {value:true, re­quired:true}

Note that a comma must be added at the end of the first line of the above code. Now, in js, type the fol­low­ing code in the lo­cal copy sec­tion:

this.apikey = n.apikey; this.is­pub­lic = n.is­pub­lic;

Next, form the url as fol­lows:

if(this.is­pub­lic) url=myurl.parse(“https://re­al­time.opensen­sors.io


else url=myurl.parse(“https://re­al­time.opensen­sors.io/v1/events


You can com­pare your code with the ex­am­ple code node-red-con­trib-os­fy­demo un­der github.com/ra­jesh­sola/ node-red-ad­dons. I am plan­ning to add a few more nodes for the his­tor­i­cal data of opensen­sors.io, which can be found in the above repos­i­tory.

A few tips be­fore you start to write custom nodes

1. Do a sim­ple search in flows.nodered.org to avoid over­lap­ping or du­pli­ca­tion. You can merge your changes into ex­ist­ing nodes in­stead of adding new ones, un­less your node has some dis­tin­guish­ing fea­tures or cod­ing com­pared to the ex­ist­ing ones.

2. If not many con­fig­u­ra­tion changes are re­quired com­pared to avail­able nodes, you can cre­ate ex­am­ple flows or sub­flows, e.g., most of the REST APIs and MQTT op­er­a­tions are pos­si­ble with ex­ist­ing http-re­quest and MQTT nodes (in­stead of adding new ones), or a sim­ple func­tion node may be good enough in be­tween.

3. Once the new node is pub­lished to npm, new nodes will be pulled pe­ri­od­i­cally and will be vis­i­ble in the above library. An­nounce your con­tri­bu­tion in the Node-RED mail­ing list for sug­ges­tions and feed­back from the com­mu­nity.


[1] nodered.org/docs/, flows.nodered.org [2] nodered.org/docs/cre­at­ing-nodes/ [3] opensen­sors.io


Fig­ure 1: SenseHAT hard­ware and sim­u­la­tor UI

Fig­ure 2: OpenSen­sors.io por­tal – de­vices and top­ics

Fig­ure 4: Lo­cal dash­board

Fig­ure 3: Con­nected nodes and MQTT con­fig­u­ra­tion

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.