Abstract: This paper mainly describes the specific process of using MQTT to connect huawei cloud IoT platform.

The scheme used: the target board is STM32L431BearPI (with E53 expansion board); The TCPIP function is provided by ESP8266 of the development board. MQTT uses paho-client-embeded; The IoT interconnection interface uses the V5 version interface. Demo Select MQTTdemo of target.

The whole development is divided into two parts: cloud development and device side development. Cloud development includes creating products and devices; Device side development includes configuring functions, compiling, burning and debugging according to the scheme.

The cloud development

Create a product model

The product model is really an abstraction of the user device. In programming terms, the product model is a Class and the device is an object. For an introduction to product models and how to create them, see Creating a Product.

The final product model created in this paper is as follows:

Equipment attributes include: temperature, humidity, light intensity, LED state, Motor state. The device commands include two parts: control LED ON/OFF and control Motor ON/OFF.

Create a device

Once the product model is created, we can create devices based on that product model. Device –> Register a device. Enter the corresponding information according to the dialog box. The device id is demoTest and the secret key is 012345678. (The device has been deleted. Do not connect to it.) The results are as follows:

After successful registration:

At this point, we have registered our end-to-end devices and can see our newly registered devices in our devices -> All Devices.

Device side development

The device development mainly includes configuration engineering, compiling and burning, debugging several steps. The following sections describe each of them.

Project configuration

Since the SDK is a full-stack software that contains all the code, we need to tell the compiler which files to compile and how to compile them, which is mostly done by Menuconfig. For engineering configuration guidance refer to engineering configuration demonstration.

Go to the target/ stm32l431_bearpi directory and enter menuconfig(menuconfig.py for MacOS or Linux) to go to the configuration screen.

Confirm the configuration to enter the SDK.

Development board serial port and DEMO selection

Set AT serial port baud rate and module matching; Set the device ID and key as you set when creating the device on the IoT platform.

TCPIP configuration

Under Network, select the TCPIP function, set the ESP8266 mode, and set the SSID and PWD of the hotspot.

Configure SSL

The main reason for configuring MBEDTLS is that the IoT platform needs to use the HMAC algorithm when generating triples of MQTT. Enable TLS and select MbedTLS and set it to CERT mode.

Configure the MQTT protocol

Enable MQTT under the Network menu. The CONFIGURED MQTT is a standard MQTT protocol.

Configure the IoT MQTT access service

Under the OC menu, enable OC MQTT (optionally turn off OC COAP and OC LWM2M).

Here we configure the process of MQTT accessing IOT; Standard DEMO is not enabled because we are using DEMO under Target.

Type Q and save to exit, and you’ll see that our configuration is saved in the.config file.

Genconfig (genconfig.py) generates or updates the iot_config.h file. The Makefile selects which components to compile based on the. Config file; The compiler determines how to compile this component based on the iot_config.h file.

Our configuration of the project is now complete.

Compile and burn debugging

compile

Functional source code is organized by Makefile, theoretically you have the Make tool and the corresponding cross-compiler can be compiled. This article uses SW4ST, an Eclipse-based tool from AC6. Its use can refer to SW4ST

In SW4ST, check “Clean” under the menu Project–>Clean to compile and you can see the compiled result in the Console.

If FLASH overflow occurs, you can disable unnecessary components (stime shell) through configuration. Or change the optimization level from -o0 -g to -OS in the Makefile.

burn

Run–>Debug Configurations in the menu of SW4ST.

Create an Ac6 STM32Debugging(using stlink).

You only need to ensure that the MCU models are the same.

For BearPI, you also need to set the restart mode to soft mode.

Click Apply and debug, you can see the burning finished and enter the debugging interface.

debugging

After entering the debug page, we can step into/ Step over/ Step out/run like any other IDE.

If the baud rate is set correctly, the system output information is displayed. (wifi is connected and IP is obtained, and the topic is subscribed to IoT platform successfully).

Joint debugging

The device is online and reports information

When the connection OK is displayed on the end, we can see on the IoT platform that the corresponding device should be online and receive the corresponding data.

The IoT platform delivers the command

After you select the synchronous command, the device can receive the command.

At this point, we have opened the data interaction channel between the device side and the IoT platform.

Click to follow, the first time to learn about Huawei cloud fresh technology ~