This article describes how to port Link SDK for C to connect a device that integrates a microcontroller unit (MCU) and cellular module to IoT Platform.
Background information
A device that cannot be directly connected to the Internet can integrate an MCU and cellular. The MCU controls the cellular module by using AT commands. This way, the device can be connected to the Internet. You can connect the device to IoT Platform by porting Link SDK for C.
This article describes how to port Link SDK for C on the MCU development board and connect the device to IoT Platform. In this example, the device integrates a common MCU and cellular module. After you connect the device to IoT Platform, you can manage the device and analyze device data by using IoT Platform.
Before you begin
Before you port Link SDK, you must prepare the following components.
- Hardware:
Hardware type Description MCU development board - Name: STM32 Nucleo board
- Model: STM32L476RG
- System: FreeRTOS
- Reference: NUCLEO-L476RG
Cellular communication board - Name: Hezhou Air724UG
- References: Product introduction and User guide.
SIM card - Interface model: Micro
- Signal: 4G
- Development tools:
Tool Description STM32CubeMX The development tool that is used to generate initialization code for MCU peripherals. Version: 6.1.2. MDK-Arm The development tool that is used to integrate the development environment and debugging code. Version: 5.26.2.0. Serial port debugger The tool that is used to connect and debug devices. Baud rate: 115,200. - Link SDK for C:
Log on to the IoT Platform console . On the SDK customization page, obtain the SDK.
NoteYou can download the sample project file to view a porting example. Then, you can port Link SDK for C to the device by referring to the configurations.
- Device authentication information:
Before you port Link SDK, you must create a product and device in the IoT Platform console and obtain the device authentication information. In this example, the unique-certificate-per-device authentication method is used. For more information, see Obtain information for unique-certificate-per-device authentication.
Procedure
- Install the hardware.
- Port the project.
- Compile and run the code.
- Click to compile the project file.
- Click the icon to download the executable sample code file to the MUC development board.
- Click to run the executable sample code file on the MUC development board.
Running result
- You can view logs on the device.
- The following log data indicates that the device is connected to IoT Platform.
linkkit_init[2.222][LK-0313] MQTT user calls aiot_mqtt_connect api, connect [2.266][LK-0317] stm32l4_mbedtls_cat1&a18wP****** [2.277][LK-0318] B4C45425D73E24B2935D73C1E98B6079A630FBE03F61E2A2031CEE7867****** unknown option, 2 unknown option, 5 [2.377][LK-1000] establish mbedtls connection with server(host='a18wP******.iot-as-mqtt.cn-shanghai.aliyuncs.com', port=[443]) [7.311][LK-1000] success to establish mbedtls connection,(cost 18686 bytes in total, max used 21294 bytes) [7.500][LK-0313] MQTT connect success in 5286 ms
- The following log data indicates that the device time is synchronized with the server
time. For more information, see NTP service.
AIOT_MQTTEVT_CONNECT [7.544][LK-0309] pub: /ext/ntp/a18wP******/stm32l4_mbedtls_cat1/request [LK-030A] > 7B 22 64 65 76 69 63 65 53 65 6E 64 54 69 6D 65 | {"deviceSendTime [LK-030A] > 22 3A 22 37 35 33 39 22 7D | ":"7539"} [7.688][LK-0309] pub: /ext/ntp/a18wP******/stm32l4_mbedtls_cat1/response local time: 1620915828805, 2021/05/13-22:23:48:805 heartbeat response
- The following log data indicates that the device submits Thing Specification Language
(TSL) data to IoT Platform. For more information, see What is a TSL model?.
[1620915829.788][LK-0309] pub: /sys/a18wP******/stm32l4_mbedtls_cat1/thing/config/log/get [LK-030A] > 7B 22 69 64 22 3A 22 31 22 2C 22 76 65 72 73 69 | {"id":"1","versi [LK-030A] > 6F 6E 22 3A 22 31 2E 30 22 2C 22 70 61 72 61 6D | on":"1.0","param [LK-030A] > 73 22 3A 7B 22 67 65 74 54 79 70 65 22 3A 22 63 | s":{"getType":"c [LK-030A] > 6F 6E 74 65 6E 74 22 2C 22 63 6F 6E 66 69 67 53 | ontent","configS [LK-030A] > 63 6F 70 65 22 3A 22 64 65 76 69 63 65 22 7D 7D | cope":"device"}} [1620915830.011][LK-0309] pub: /sys/a18wP******/stm32l4_mbedtls_cat1/thing/config/log/get_reply [1620915830.033][LK-1507] LOGPOST user log config arrived log switch state is: 1 [1620915835.066][LK-0309] pub: /sys/a18wP******/stm32l4_mbedtls_cat1/thing/log/post [LK-030A] > 7B 22 69 64 22 3A 22 32 22 2C 22 76 65 72 73 69 | {"id":"2","versi [LK-030A] > 6F 6E 22 3A 22 31 2E 30 22 2C 22 70 61 72 61 6D | on":"1.0","param [LK-030A] > 73 22 3A 5B 7B 22 75 74 63 54 69 6D 65 22 3A 22 | s":[{"utcTime":" [LK-030A] > 32 30 32 31 2F 35 2F 31 33 20 32 32 3A 32 33 3A | 2021/5/13 22:23: [LK-030A] > 35 35 22 2C 22 6C 6F 67 4C 65 76 65 6C 22 3A 22 | 55","logLevel":" [LK-030A] > 44 45 42 55 47 22 2C 22 6D 6F 64 75 6C 65 22 3A | DEBUG","module": [LK-030A] > 22 41 50 50 22 2C 22 63 6F 64 65 22 3A 22 32 30 | "APP","code":"20 [LK-030A] > 30 22 2C 22 74 72 61 63 65 43 6F 6E 74 65 78 74 | 0","traceContext [LK-030A] > 22 3A 22 30 22 2C 22 6C 6F 67 43 6F 6E 74 65 6E | ":"0","logConten [LK-030A] > 74 22 3A 22 6C 6F 67 20 69 6E 20 77 68 69 6C 65 | t":"log in while [LK-030A] > 28 31 29 22 7D 5D 7D | (1)"}]} [1620915836.855][LK-0309] pub: /sys/a18wP******/stm32l4_mbedtls_cat1/thing/event/property/post [LK-030A] > 7B 22 69 64 22 3A 22 33 22 2C 22 76 65 72 73 69 | {"id":"3","versi [LK-030A] > 6F 6E 22 3A 22 31 2E 30 22 2C 22 70 61 72 61 6D | on":"1.0","param [LK-030A] > 73 22 3A 7B 22 4C 69 67 68 74 53 77 69 74 63 68 | s":{"LightSwitch [LK-030A] > 22 3A 20 30 7D 2C 22 73 79 73 22 3A 7B 22 61 63 | ": 0},"sys":{"ac [LK-030A] > 6B 22 3A 31 7D 7D | k":1}} [1620915837.099][LK-0309] pub: /sys/a18wP******/stm32l4_mbedtls_cat1/thing/event/Error/post [1620915837.122][LK-0309] pub: /sys/a18wP******/stm32l4_mbedtls_cat1/thing/event/property/post_reply [1620915837.177][LK-0A08] DM recv generic reply demo_dm_recv_handler, type = 0 msg_id = 3, code = 200, data = {}, message = success [1620915837.311][LK-0309] pub: /sys/a18wP******/stm32l4_mbedtls_cat1/thing/event/Error/post_reply [1620915837.344][LK-0A08] DM recv generic reply demo_dm_recv_handler, type = 0 msg_id = 4, code = 200, data = {}, message = success
- The following log data indicates that the device is connected to IoT Platform.
-
You can log on to the IoT Platform console to view the device status and operation logs.
- In the left-side navigation pane, choose Online in the State column indicates that the device is connected to IoT Platform. . Then, find the device and view the online status. The value
- In the left-side navigation pane, choose . Select a product to view device logs.