How to use NodeRed to realize the communication between Raspberry Pi and Alibaba Cloud IoT platform?

Node-RED is a powerful tool for building Internet of Things (IoT) applications. It uses a visual programming method that allows developers to connect together predefined code blocks (called "nodes") to perform tasks.

When pre-defined code blocks are connected together, the connection nodes usually composed of input nodes, processing nodes and output nodes form a "flow".

The RevPi Core module has a built-in Node-RED operating environment. This article will introduce how to realize the communication between the RevPi Core module and the Alibaba Cloud IoT platform based on Node-RED.

01 Start Node-RED

The RevPi Core module has already built the Node-RED operating environment, and additionally includes nodes suitable for RevPi devices, without additional software installation, just start and run.

First connect to RevPi remotely via putty, enter the following command to start Node-RED: sudo node-red-start.

After waiting for Node-RED to start, enter the Node-RED interface by entering RevPi's IP: 1880 in the browser.
Insert picture description here
You can use the command sudo systemctl enable nodered.service to set Node-RED to start automatically.

02 Configure Alibaba Cloud IoT platform

In the IoT platform console, click Device Management -> Product -> Create Product:
Insert picture description here
Install the product as shown in the figure below:
Insert picture description here
Click Save, and go to Add Device:
Insert picture description here
Enter the device name, you can choose by yourself, here is RevPi:
Insert picture description here
Click to confirm , And go to view device information:
Insert picture description here
Don’t close this device information page for now, you will need information about this page later.

03 Create Node-RED flow

The MQTT communication method will be used here to communicate with the Alibaba Cloud IoT platform. First, establish the flow as shown in the figure below: the
Insert picture description here
timestamp (ie the inject node) only serves as a trigger here and does not need to be set.

The exec node can execute system commands. This article uses the exec node to collect RevPi's CPU temperature and upload it to the cloud platform. Configure the exec node as shown below.

In the command box, enter: /usr/bin/vcgencmd measure_temp.
Insert picture description here

04 mqtt out node parameter setting

For the configuration of the mqtt out node, please refer to the Alibaba Cloud Internet of Things platform document "Use MQTT.fx to access the Internet of Things platform". The document uses the MQTT.fx software, but the configuration parameters are basically the same.

Click to add a new mqtt-broker node, and set the node name by yourself. In this example, set it to AliIOT.
Insert picture description here
The parameters that need to be configured include connection and attributes. Let’s look at them in turn:
Insert picture description here
Insert picture description here
Server
According to the official documentation, the address format of Broker is:
${YourProductKey}.iot-as-mqtt. ${YourRegionId}.aliyuncs.com

The above ProductKey can be found in the device information, and the RegionId can be found in the URL: https://help.aliyun.com/document_detail/40654.html?spm=a2c4g.11186623.2.19.3d1b5800pRBtd2#concept-h4v-j5k-xdb
Complete example:
a1A96I6bXvh.iot-as-mqtt.cn-shanghai.aliyuncs.com,
select 1883 as the port.

Client
ID The format of the client ID is:
${clientId}|securemode=3,signmethod=hmacsha1|.

  • The clientId can be set by the user, and the length is within 64 characters. In this example, it is set to RevPiNodeRED.
  • Securemode is the secure mode, TCP direct connection mode is set to securemode=3, and TLS direct connection is set to securemode=2. In this example, select the security mode as 3.
  • Signmethod is an algorithm type, which supports hmacmd5 and hmacsha1. For this example, hmacsha1 is selected.
    Full example:
    RevPiNodeRED|securemode=3,signmethod=hmacsha1|

User name and password
The format of the user name is:
${YourDeviceName}& ${YourProductKey}.
Both DeviceName and ProductKey can be found in the device information, please fill in accordingly.
Complete example: The
RevPi&a1A96I6bXvh
password needs to be generated based on the encryption operation of the device information. You can generate a small tool with the help of password, download address:
https://files.alicdn.com/tpsservice/88413c66e471bec826257781969d1bc7.zip?spm=a2c4g.11186623.2.21.3d1b5800pRBtd2&file=88413c66e471bec826257781969d1bc7.zip to
download and decompress the file.
As shown in the figure below, fill in the relevant information truthfully, the timestamp can be omitted, and click generate to generate the password.
Insert picture description here
In addition, you can also manually generate a password. For details, please refer to the official documentation of the Alibaba Cloud IoT platform.

Topic
Fill in the above Broker related information and click Add:
Insert picture description here
Topic can be selected in Product->Test->Topic Class List->Custom Topic to select Topic with publishing permission, and change it according to the device name:
Insert picture description here
Of course, you can also Customize topics with publishing permissions. The subject used in this example is:
/a1A96I6bXvh/RevPi/user/update
mqtt out The QoS and retention option settings of the node can be selected by yourself. After the configuration is complete, click "Finish". Then click the "Deploy" button to deploy this process to the device and run it.

05 Communication test

After the deployment is completed, the mqtt out node should display "Connected" under normal circumstances. If it is "not connected" or has been "connecting", please check whether the related configuration is wrong.
Insert picture description here
Click the timestamp to trigger the process and upload the temperature information to the cloud platform.
You can view the received temperature information under the cloud operation log by selecting the Test product under the monitoring operation and maintenance -> log service of the Alibaba Cloud IoT platform console.
Insert picture description here
Insert picture description here
Of course, you can also send information from the cloud platform to RevPi through topics with subscription permissions. If you are interested in this, you can explore the implementation method by yourself according to the official manual.

Guess you like

Origin blog.csdn.net/Hongke_IIOT/article/details/108746327