Tuesday, February 9, 2016

Node-RED - Messages and Events

Node-RED is so flexible that people can have different views on what the tool does and how it can be useful.

The first thing to keep in mind is that Node-RED is not a programming language. It is an application that uses pieces of code to accomplish what we need.

So, what is the flow that we create when we connect all those nodes together? We know that those nodes are, at the end of the day, small pieces of JavaScript code. What is the purpose of connecting them after all?

Node-RED is a smart tool to process messages and events. This is why it can be so useful in the IOT world.

An event can be anything that can be captured to be processed in the digital world or anything that can be produced and communicated in the digital world.

Some examples of events:

- A light is turned on.
-  An e-mail or twitter or file is sent to someone.
- A record is updated and written to a database.
- A cell is changed in a spreadsheet.
- The temperature sensor in an aquarium generates a new measurement.
- The output pin in a microprocessor changes from low to high.
- This list is endless....


In the IOT world, we want to be able to capture events, process them  and generate communication or control.

Node-RED offers us an easy way to design flows to process messages. Those messages are representations of events.


Let's have a look at the most basic nodes from Node-RED: the input node "inject" (here labelled as timestamp) and the output node "debug" (here labelled as msg).

What kind of message processing does the inject node?

It is simple. It creates in the flow a message with three pieces of information: topic, payload and _msgid.


"topic": "", 
"payload": 1455054618825,
"_msgid": "6a3a8c99.95c574" 
}

This message has a peculiar format that is well known and widely used: JSON format.

It is not a coincidence that the main pieces are named "topic" and "payload". This has an intimate relation with the protocol MQTT.

So, the purpose of this node is to generate a message anytime someone clicks on the left tab in the node icon. For this example, the node was configured to load the computer time inside the message (this is why it was renamed as a "timestamp").

Since I wired this node to the output node "debug" the whole message (three pieces) will be handed over to this second node. This very basic output node will simply display the whole message in the debug window of Node-RED.

As we develop our flow to accomplish a specific task or purpose the message that goes from node to node is changed, processed, communicated, etc.

The flow and the messaging processing will be as complex as your task requires (and hopefully not more complex than really needed).

For basic and simple applications most of the times we will be changing only the payload of the object message (msg).

In Javascript we call the message an object and the payload is one of its properties.  Normally to refer to the payload of the object msg , we use the dotted notation: msg.payload

This is why in the dialogue box of the debug node you have the option to display msg.payload (or eventually another property if you wish).




So, creating a Node-RED flow requires understanding the problem at hand, abstracting the problem to represent it in the form of objects (messages), define what to do with the message (maybe add information  or store or modify), select the appropriate nodes from the library (or create new ones or augment the existing ones) and how to wire them together and finally define the way to communicate the messages on the internet (protocol and application).






IOT course by FutureLearn

I have enrolled in an exciting online IOT course by FutureLearn (click here).

The focus is on the entrepreneurial side of IOT, which is great for me.  Technical material is provided as additional (optional) study material.