Node-RED is a visual programming tool that connects hardware devices, APIs, online services and more in innovative and interesting ways. It provides a browser-based editor that easily connects processes through an extensive palette of nodes that can be deployed to its runtime with just one click.
In addition to some basic network service application nodes such as HTTP and WebScoket, Node-RED also provides connection support for the MQTT protocol. At present, an MQTT subscription node and an MQTT publishing node are provided at the same time. The subscription node is used for data input, and the publishing node can be used for data output.
This article will describe the complete flow of connecting to an MQTT server using Node-RED, filtering and processing MQTT data before sending it to the MQTT server. Readers can quickly learn how to use Node-RED for simple stream processing of MQTT data.
Node-RED installation
Node-RED can be quickly installed and used whether it is on your local computer, a device such as a Raspberry Pi, or a cloud server. The following two common installation methods will be used:
Use npm
for a global install:
1npm install -g --unsafe-perm node-red
Use Docker
to install:
1docker run -it -p 1880:1880 --name mynodered nodered/node-red
run
If you are using npm for global installation, you only need to run the node-red
command globally to start Node-RED immediately after prompting that the installation is successful.
Whether using Docker
or npm
After the startup is successful, we only need to open the browser, enter the current address and the port number 1880 to open the browser editor page of Node-RED , for example, if running locally, open the browser and enter http://127.0.0.1:1880 , when you see the page as shown in the figure below, it means that Node-RED has been successfully started:
Using MQTT with Node-RED
This article will use the free public MQTT server provided by EMQ, which is based on the MQTT IoT cloud platform - EMQX Cloud. The server access information is as follows:
- Broker: broker-cn.emqx.io
- TCP Port: 1883
- WebSocket Port: 8083
In the following function demonstration, we will provide a node-RED to process the received JSON data containing temperature and humidity information, and then make a rule judgment on the temperature value. When the temperature changes, the current temperature that has changed will be used. Simple use case for a value to be sent again over MQTT.
Connect to MQTT server
We first drag an MQTT in node to the page in the left menu bar. After double-clicking the node, a configuration page for editing the MQTT node appears on the right. We create a new connection information according to the content prompts, and then fill in the MQTT After adding other connection information, click the Done button to save the node information.
Process MQTT data
Access data: We drag and drop a JSON node to the page, and configure an Action in the configuration page of the JSON node. We set it to Always convert to JavasScript Object
, because we cannot determine that the data sent is in a JSON format The data is still a JSON string, so the first step is to perform a JSON conversion on the received message. After the configuration is complete, we connect the node with the MQTT in node.
filter data
After we configure and format the sent message data, we can drag and drop a filter node to the page. After double-clicking the node, configure the rules in the configuration page. We first select a Mode, which we set to blcok unless value changes
, the filter rule is that the value of the currently received data needs to be changed, because the current data is in JSON format, we are judging a certain value in the JSON data, so we need to set the value in the Property here msg.payload.temperature
After the configuration is completed, we click the Done button to save the configuration of the data filtering node, and finally connect the node to the JSON node after the configuration in the previous step is completed.
Use templates
After filtering the data, drag and drop a template node to the page, double-click the node, and then configure the template content, so that the filtered data can be output through the template. Of course, this step is not required, and the filtered data can be output directly.
Send processed MQTT data
After completing the above data processing and filtering, finally we will send the processed data using MQTT, drag and drop an MQTT out node to the page, fill in the same connection information as the MQTT in node, and configure a The user receives the topic of the data, and after saving it, connects it with the template node, and clicks the Deploy button in the upper right corner to deploy the current rule application online.
function test
After completing the functional orchestration of the entire stream data processing, we use the MQTT 5.0 client tool - MQTT X to test and verify the availability of the function. We create a new connection, connect to the MQTT cloud service address just configured in Node-RED, and then enter the Topic in the MQTT in node to send a message, so that Node-RED can receive the MQTT data we sent.
Then we subscribe a Topic configured in the MQTT out node in MQTT X to receive the processed message data. After sending a message data containing temperature and humidity, we can receive a message sent according to the message template we set, but cannot receive it again.
Because the temperature value has not changed at this time, when we modify the temperature value again, we will find that we have received another message containing a reminder that the temperature value has changed.
Summarize
So far, we have completed the installation and use Node-RED to connect to the MQTT cloud service, filter and process the MQTT message data, and finally send the processed data message to the MQTT server.
The interaction and use of Node-RED, that is, describing general business logic in UI, can reduce the threshold for non-professional developers to get started, and use a visual tool to quickly create the required complex execution tasks, which can be constructed through simple Node connections. Complex tasks, especially for some IoT application scenarios, are helpful.
Copyright statement: This article is original by EMQ, please indicate the source when reprinting.
Original link: https://www.emqx.com/zh/blog/using-node-red-to-process-mqtt-data
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。