Home >Product dynamics>Product dynamics
Docker containers realize edge collection

Node-RED environment construction

Node-RED is a powerful tool for building Internet of Things (IoT) applications that focuses on simplifying the "connection" of blocks of code to perform tasks. It uses a visual programming approach that allows developers to connect predefined blocks of code (called "Nodes") to perform tasks. Connected nodes, usually a combination of input nodes, processing nodes, and output nodes, form a "flow" when connected together.


Node-RED.png

There are many tutorials on the Internet about how to deploy the docker environment. I will not explain it here. It is recommended to refer to the official installation instructions. Address: https://docs.docker.com/engine/install/centos/ (access in China may be slow), or you can Check out Alibaba Cloud's Documentation Center to see how to deploy a docker environment.

Project image installation

It is very simple to install the project file with docker. It only requires two instructions to deploy the Node-RED development environment. The first one is to pass "docker pull repository name" to get the project image, or you can pass "docker --help" Query instruction description.



docker --help.png


Starting from Node-RED 1.0, the repository on Docker Hub has been renamed to "nodered/node-red", use "docker pull nodered/node-red" to pull the project image.
To run it in Docker in its simplest form, just run:
 docker run -it -p 1880:1880 -v node_red_data:/data --name mynodered nodered/node-red
Let’s unpack the instructions:
docker run: run the container;
-it: Print the installation session, which can be omitted;
-p: Configure local port and external mapped port;
-v node_red_data:/data: Mount a docker named "node_red_data" to the container/data directory;
--name mynodered: named "mynodered";
nodered/node-red: mirror address;
It is also possible to just use "Docker run" without pulling the project image first. This command will query the local image. If there is no local image, it will automatically download the project image from the Docker Hub repository;
For other custom installations, it is recommended to carefully read the Node-RED official documentation (https://nodered.org/docs/) before proceeding. I will only introduce one usage of this tool and will not use the custom installation function;

Install plugin

Edge collection of RTU devices cannot be achieved through the default installation environment of Node-RED. It also needs to be installed by installing different functional plug-ins. Install the Modbus functional plug-in (node-red-contrib-modbus) to realize Modbus RTU/Modbus TCP protocol conversion and edge Collection, install the database plug-in (node-red-node-mysql) to realize the storage of edge collection data. In addition to installing the plug-in, a small amount of "javascript" scripts are also required for format conversion and data reporting.

Fortunately, the process of installing the plug-in can be realized through the visual web interface, and there is no need to understand complex installation instructions. This is also one of the advantages of Node-RED. Enter the IP address and port where the Node-RED environment is deployed in the address bar. In my case, it is 192.168. .88.5:1880.



Install plugin.png


Open the menu on the upper right foot, select node management, click "Install", and enter the Modbus function plug-in (node-red-contrib-modbus) and database plug-in (node-red-node-mysql) in the search box.

Install plugin-1.png

After successfully installing the node, add the following nodes in the toolbar:

Install plugin-3.png

The author of the "node-red-contrib-modbus" node has produced examples of different usages. You can test and understand the functions of different nodes through the import function. It is strongly recommended to understand more of these examples to facilitate subsequent use.

Gateway implementation

Gateway implementation.png


Add a "Modbus read" node to periodically query the Modbus device status. First, you need to configure the address of the Modbus device. I use the "Modbus Slave" software to simulate it. The address is 192.168.88.100:8890:
Gateway implementation-1.png


On the left side of the figure, double-click to open the "Modbus read" configuration interface. On the right side of the figure, a new Modbus device connection address is added. In the figure, the register parameters are configured. Here, the holding register with address 0x0000 is used as an example to configure the "Poll Rate" query cycle.
Use the "function" node to extract the data queried periodically by the "Modbus read" node, double-click to open the function node, and edit the following script:
msg.payload=msg.payload.data;
msg.payload=msg.payload.slice(0,1);
return msg;
Use "template" to package the results processed by the "function" node. Here I package the data as JSON data:

Gateway implementation-2.png

Finally, the data is transmitted to the specified server through TCP connection:

Gateway implementation-3.png

"6" is the "DEBUG" node for debugging. Through the above configuration, the data collected by the Modbus TCP command can be converted into JSON data and reported to the designated server:

Gateway implementation-4.png

Data can also be transmitted to the server through the MQTT connection node provided by Node-RED, and the collected data can also be transmitted to the database through sql statements through the "mysql" node.

Examples of collection and reporting database and MQTT server processes

Gateway implementation-5.png

At this point, a simple edge collection gateway is built.