Huawei Cloud Platform provides powerful cloud services, including support for the MQTT protocol(Related article:Which version of the MQTT protocol is better to use?). MQTT is a lightweight communication protocol widely used in the Internet of Things (IoT) field to achieve reliable data transmission and communication between devices. This tutorial will step by step introduce how to use the MQTT service provided by Huawei Cloud, including key steps such as creating an MQTT instance, device access, message publishing, and subscription.
Popular Terms About : Communication protocol
1. Huawei Cloud activates MQTT service
First, complete registration and perform real-name authentication. Next, we enter the [Product] → [Internet of Things IoT] → [Device Access] page, select [Management Console], and then enter the Huawei Cloud IoT platform.
After entering the console, select the service package according to the actual situation under [IoTDA Instance]. Select [Open Free Unit] in the tutorial.
After the creation is successful, refresh the page and you will see that you can start creating devices under the [Device Access] column.
First, under [Overview], you can view [Access Information] and click to obtain the access address and port number for MQTT communication.
In [Related Documents] under [Access Information], you can directly and quickly access the relevant manuals of Huawei Cloud.
If you have any questions, you can check through [Related Documents].
Next, return to the device [Access Page], enter the [Product] page, click [Create Product] in the upper right corner to start creating information about the device to be accessed.
Here, select the resource space you just created as [Owning Resource Space]. Fill in the [product name] according to the actual situation, and you can set it arbitrarily. [Protocol Type] Select [MQTT]. Select [JSON] for [Data Format]. [Manufacturer name] and [Equipment type] can be filled in according to the actual situation.
Once completed, click Confirm.
Next, go to [Devices] → [All Devices] and click Register Device.
Select the option you just created in [Official Resource Space] and [Official Product]. Fill in any value in [Product Identification Code], just make sure it is not repeated within a product. In [Device Authentication Type], you can choose [Key] or [Certificate], choose according to the actual situation. In the tutorial, [Key] authentication was selected. After confirming that other information is correct, click OK. A successful creation prompt will appear next.
Please note that if it is an automatically generated key, be sure to save it. The key can only be viewed once.
After confirming that the key is saved successfully, click [Save and Close], and you will see that the product we just created has been successfully added to the device list. Click Details to enter the product details page.
On this page you can get product-related information.
At this point, we have successfully activated the MQTT service of Huawei Cloud and created the device.
2. Obtain MQTT communication elements
Before starting to simulate MQTT communication, you need to obtain several parameters required for communication. First, let's go to the product page we just created.
Enter the ToPic management page.
Here, we can find some [System Preset Topics] preset by Huawei Cloud. Just select the corresponding Topic according to your needs.
Next, we need to get the "Client ID", "Username" and "Password". A tool can be used to generate these parameters directly. In [Overview] → [Access Information] → [Related Documents/Resources], click [Pan-Protocol Access] to enter the help document.
In the document, select any instance to which the device is connected.
Find an online tool under [Connection Authentication] and just click on it. Fill in the [Device ID] and [Key] of the device we registered in the tool to generate the information required for the connection (ClientId, Username, Password).
At this point, we have obtained all the parameters required for communication.
3. Product access uses MQTT to report data
In this tutorial, we use the serial server NB144 device to simulate actual communication scenarios. You can choose equipment according to the actual situation or use software to simulate. For how to use the product, please refer to the relevant video or consult the relevant manual. The required tools can be found in the product's associated software.
Now, we start to simulate the real communication process and open the host computer software of NB144.
First, change the [IP address type] in the product's network parameters to [Dynamic IP].
Enter the port 1 settings, change the working mode to [MQTT Client] mode, and select the standard [Huawei Cloud] as the platform.
Then fill in [Target IP], [Target Port], [Client ID], [Device name] and [Device secret] in order. Subscription and publishing are filled in based on actual conditions. In the tutorial, we select a system publishing topic for data transfer and leave the subscription topic blank.
After saving and restarting, wait for a while, and you will be able to see the device successfully connected in the background of the Huawei Cloud Platform.
At this point, we have successfully connected the product to Huawei Cloud through MQTT. Next, use any serial port debugging tool to simulate the device data reporting function.
In this tutorial, we use the XCOM tool, open the tool, confirm whether the serial port parameters are correct, and then [Open the serial port].
For rules on reporting data, please refer to the relevant manual. Due to space limitations, no further explanation will be given. Send the data report directly, and then under the [Device Shadow] page of the product, you can see the data just reported.
At this point, we have successfully connected to Huawei Cloud's MQTT platform and implemented the data reporting function.
We are a national high-tech enterprise specializing in the research
and development of wireless modules and industrial IoT terminals.
Independently developed and produced products include LoRa/WiFi Wireless Module/Bluetooth/ZigBee wireless modules, Ethernet modules, NB-IoT data transmission terminals, industrial IoT.
FREE TRIAL AT CDEBYTE
Explore Our Free trial and Evaluate More Discounts