Node-RED
Node-RED is a powerful tool for building Internet of Things (IoT) applications with a focus on simplifying the ‘wiring together’ of code blocks to carry out tasks. It uses a visual programming approach that allows developers to connect predefined code blocks, known as ‘nodes’, together to perform a task. The connected nodes, usually a combination of input nodes, processing nodes and output nodes, when wired together, make up ‘flows’. (Link)
Node-RED provides a web browser- based flow editor, which can be used to create JavaScript functions. Elements of applications can be saved or shared for re-use. The runtime is built on Node.js. The flows created in Node-RED are stored using JSON which can be easily imported and exported for sharing with others. By understanding Node-Red, IoT development can be accelerated without unnecessary coding.
Node.js
Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser. Node.js allows developers use JavaScript to write command line tools and to run scripts server-side to produce dynamic web page content before the page is sent to the user's web browser. NPM is a package manager for Node.js packages, or modules. The NPM program is installed on your computer when you install Node.js. A package in Node.js contains all the files you need for a module. Modules are JavaScript libraries you can include in your project. Since the light-weight runtime of Node-Red is built on Node.js, it is taking full advantage of its event-driven, non-blocking model.
Node-RED and Internet of Things
As a tool for rapid application development for the IoT, Node-RED is both powerful and flexible thanks to two factors:
Node-RED is an example of a flow-based programming model – messages representing events flow between nodes, triggering processing that results in output. The flow-based programming model maps well to typical IoT applications which are characterised by real-world events that trigger some sort of processing which in turn results in real-world actions.
Node-RED provides a set of built-in input and output nodes, each of which hides much of the complexity of interacting with the real world. Node-RED offers developers powerful building blocks to allow them to quickly put together flows that accomplish a lot, without having to worry about the programming details.
Installation of Node-RED
In order to able to work with Node-RED, you should install working node.js. It is recommended the use of Node.js LTS 8.x or 10.x. Node-RED no longer supports Node.js 6.x or earlier. (latest version is recommended). You can download the latest version of node.js from website below choosing which operating system you are using. https://nodejs.org/en/download/
Linux / OSx
Once installed, node.js, open the terminal window and run the following commands. To check your version of Node.js:
The easiest way to install Node-RED is to use the node package manager, npm, that comes with Node.js. Installing as a global module adds the command node-red to your system path:
Windows
Run the downloaded MSI file. Installing Node.js requires local administrator rights; if you are not a local administrator, you will be prompted for an administrator password on install. Accept the defaults when installing. After installation completes, close any open command prompts and re-open to ensure new environment variables are picked up.
Once installed, open a command prompt and run the following command to ensure Node.js and npm are installed correctly.
You should receive back output that looks similar to:
Installing Node-RED as a global module adds the command node-red to your system path. Execute the following at the command prompt:
Running Node-RED
If you have installed Node-RED as a global npm package, you can launch node-red in the command prompt (Windows):
or in the terminal (Linux):
This will output the Node-RED log to the terminal. You must keep the terminal or command prompt open in order to keep Node-RED running. Note that running Node-RED will create a new folder in your %HOMEPATH% folder called .node-red. This is your userDir folder, think of it as the home folder for Node-RED configuration for the current user.
You can then access the Node-RED editor by pointing your browser at http://localhost:1880
Example: Hello World
Perhaps the best way to understand what Node-RED is to see it. Below is Node-RED's representation of a "Hello World" program, commonly used to introduce someone to a new technology or programming language. It is usually a very simple routine that prints the message 'Hello World' on the screen. Node-RED provides a GUI where users drag-and-drop blocks that represent components of a larger system, in Node-RED's case usually the devices, software platforms and web services that are to be connected. Further blocks can be placed in between these components to represent software functions that wrangle and transform the data in transit. Here you can see Node-RED's graphical user interface and how the platform breaks systems down into their constituent parts.
Each of the rounded blocks you can see on the screen is a node, which is a visual representation of a block of JavaScript code designed to carry out a specific task. To build the 'Hello World' program the user drags into the central window an 'inject node', a node designed to output a message to other nodes. This inject node is edited to output the string 'Hello'. Next a function node is dragged on and edited to define a JavaScript function that appends the string ' World' to any message it receives. These two nodes are then wired together. Most nodes have a grey circle on their left edge, which represents their input port, and on their right edge, which represents their output port. Left clicking and dragging the output port of the 'Hello' node to the input port of the ' World' node connects the two together. The final stage is to add a Debug node that prints the message it receives in the Debug window. This Debug node is then wired to the output of the ' World' node. The process is now ready to run and once deployed will display the string 'Hello World' in the Debug box, shown on the right of the screen. This finished program is an example of what is called a flow in Node-RED.
Node-RED can do much more than spit out a simple message, e.g. it can also glue together web services and hardware, and that's where it starts getting powerful.
References
Most of the contents in this document can be found on the Node-Red website: https://nodered.org/
Installation instructions can be found at: https://nodered.org/docs/getting-started/installation
Node RED Programming Guide: http://noderedguide.com/
Last updated