# Node-RED

### **Menjalankan Node-RED**

You must first install Node.js and Node-RED on your computer/device. If you don't have Node.js and Node-RED, you can follow the tutorial on the page [https://nodejs.org/en/](https://nodejs.org/en) and then [https://nodered.org/docs/getting-started/installation](https://nodered.org/docs/getting-started/local).

Node-RED is a programming tool for connecting hardware devices, APIs, and online services in new and exciting ways. Node-RED provides a browser-based editor that makes it easy for users to connect flows with extensive use of nodes in a palette that can be directly applied with one click\[1]. Node-RED is shared with Antares, MQTT, and other platforms/protocols. Once installed, Node-RED can be run by typing "node-red" at the command prompt.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/QfqRHBKWIm6uyAECcPcF/image.png" alt=""><figcaption><p>Running Node-RED</p></figcaption></figure>

Users can type "localhost:1880" or "IPv4-address:1880" in the browser to access Node-RED.

### Node-RED Parts

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/RfboCg2KWiHedaXAWFFS/image.png" alt=""><figcaption><p>Parts of the RED Node</p></figcaption></figure>

In Node-RED, there are sections called nodes and flows. Nodes are the parts that are used to create the programme. There are several types of nodes: input, output, function, and others. The nodes can be downloaded in the "Manage palette" menu.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/TbVgXwnPyJ1Xu7VKZZbT/image.png" alt=""><figcaption><p>"Manage Palette" Menu</p></figcaption></figure>

A window will appear. The "nodes" menu shows nodes that are already installed, while the "install" menu shows nodes that can be downloaded and installed.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/GZVwaPMnuZv4rb2cGzde/image.png" alt=""><figcaption><p> "Nodes" and "Manage Palette" Menu</p></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/7nX00RC6AszXQ7ejugqT/image.png" alt=""><figcaption><p> "Install" menu in "Manage Palette"</p></figcaption></figure>

A flow is a place to attach nodes. In one Node-RED window, there can be more than one flow. Flows are independent of each other.

### Using MQTT&#x20;

#### ***A. Node*****&#x20;MQTT**

MQTT nodes are already installed during the installation of Node-RED. There are two types of MQTT nodes: MQTT input and MQTT output.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/W9eq7MqYfIhnD55KqX1Y/image.png" alt=""><figcaption><p>Gambar 6. Tipe <em>Node</em> MQTT</p></figcaption></figure>

#### **B.** MQTT Broker Setup&#x20;

An MQTT node looks like this:

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/X3TMMjFIIJrstOZmU2jL/image.png" alt=""><figcaption><p>MQTT Node Settings</p></figcaption></figure>

Adding a new broker can be done by selecting "add new mqtt-broker" and clicking on the right-hand icon. Setting up a broker can be done by simply clicking on the right-hand icon.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/hjgEMfce9R4uBbMDf0iV/image.png" alt=""><figcaption><p>New Broker Additions</p></figcaption></figure>

Users can name the new broker and set the server and port used.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/B61sdaWmxs9uSjhBXREd/image.png" alt=""><figcaption><p>New Broker Settings</p></figcaption></figure>

#### **C.** Transmitting and Receiving Data

Simulation of input data from the device can be done with the use of the "inject" node.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/cfSIZbgnvVfX9KQyqDdC/image.png" alt=""><figcaption><p>Send Data</p></figcaption></figure>

The "inject" node can be changed to provide another data type. In this case, the data type used is JSON.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/4h2C9PKpKpBqZAy5ze0H/image.png" alt=""><figcaption><p>Setting the "inject" Node</p></figcaption></figure>

Programmes can be written by clicking on the "..." icon.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/4kLF7MtiQVQaulXVPa7U/image.png" alt=""><figcaption><p>Programme Writing</p></figcaption></figure>

The "debug" node can be used to view the messages sent.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/L3A2Sw8bmtBI07kCnKJA/image.png" alt=""><figcaption><p>Receive Data</p></figcaption></figure>

Users can view the received messages in the "debug" panel.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/6IQVTXCUpfA1qPSTZSzA/image.png" alt=""><figcaption><p>Output in the "Debug" Panel</p></figcaption></figure>

## Reference

1. ["Node-RED: Flow-based Programming For The Internet"](https://nodered.org/). [Node-RED](https://nodered.org/). Accessed on 13 September 2018.
