After you obtain a device certificate (ProductKey, DeviceName, and DeviceSecret), connect the device to an Enterprise Edition instance over MQTT. Use Link SDK for C to configure a simulated device and submit data.
Prerequisites
A product and a device are created. For more information, see Create a product and device.
Before you begin
All operations in this example use regular user permissions. To perform operations that require administrator permissions, run the sudo command.
Prepare a development environment
-
Link SDK for C is used on Linux in this example. We recommend 64-bit Ubuntu 20.04 for compiling the SDK.
An Elastic Compute Service (ECS) instance serves as the simulated device that connects to IoT Platform and submits data. For more information about how to purchase an ECS instance, see Create an instance.
-
The following software is required to develop and compile the SDK:
make (version 4.1 or later) and gcc (version 5.4.0 or later)
Run the following command to install the software:
sudo apt-get install -y build-essential make gcc
Obtain Link SDK for C
Download the LinK SDK for C V4.x compression package and change the name of the package to LinkSDK.zip.
Develop a sample program for the device
This example uses a Thing Specification Language (TSL)-specific topic to submit data. You can also use custom topics to send data to devices. For more information, see Use custom topics.
-
Log on to the ECS instance. For more information, see Remote connection methods for ECS instances.
-
Run the following command to install GCC and Make:
sudo apt-get install -y build-essential make gcc -
Upload the
LinkSDK.zipfile and decompress the file.-
Run the following commands to install unzip:
apt update apt install unzip -
Go to the directory in which the
LinkSDK.zipfile resides and run the following command to decompress the file:unzip LinkSDK.zip
-
Go to the
/LinkSDK/demosdirectory, open thedata_model_basic_demo.cfile, and configure the device verification parameters./* TODO: Replace the following variables with the information about your device certificate. */ char *product_key = "a2******"; char *device_name = "Device1"; char *device_secret = "8c684ef*************"; ...... char *mqtt_host = "iot-cn-******.mqtt.iothub.aliyuncs.com";Parameter
Example
Description
product_key
a2******The device certificate that is used for verification. This certificate is saved on your on-premises server after you add the device.
You can also view the device certificate on the Device Details page in the IoT Platform console.
device_name
Device1device_secret
8c684ef*************mqtt_host
iot-cn-******.mqtt.iothub.aliyuncs.comThe MQTT endpoint. In the upper-right corner of the Instance Details page, click View Development Configurations. In the Development Configurations panel, view the endpoint.
For more information about instances, see Manage instance endpoints.
-
Modify the following sample code to submit temperature and humidity data:
/* The main thread goes to sleep. */ while (1) { /* TODO: The following sample code shows how to submit properties and events. You can uncomment the sample code to view the result. */ demo_send_property_post(dm_handle, "{\"temperature\": 30,\"humidity\": 42}"); ...... sleep(5); } -
Modify the following sample code to subscribe to a custom topic. The custom topic is used to receive commands from IoT Platform.
aiot_mqtt_sub(mqtt_handle, "/a2******/Device1/user/get", NULL, 1, NULL); -
In the
/LinkSDKroot directory, run the following commands to compile the sample program:make clean makeThe generated
data-model-basic-demofile is stored in the./outputdirectory. -
Run the following command to execute the sample program:
./output/data-model-basic-demoIf the sample program runs as expected, the following logs are generated:
[1695199326.066][LK-0313] MQTT user calls aiot_mqtt_connect api, connect [1695199326.066][LK-032A] mqtt host: iot-cn-******.mqtt.iothub.aliyuncs.com [1695199326.066][LK-0317] user name: Device1&a2****** establish tcp connection with server(host='iot-cn-******.mqtt.iothub.aliyuncs.com', port=[443]) success to establish tcp, fd=3 local port: 36030 [1695199326.077][LK-1000] establish mbedtls connection with server(host='iot-cn-******.mqtt.iothub.aliyuncs.com', port=[443]) [1695199326.122][LK-1000] success to establish mbedtls connection, (cost 45382 bytes in total, max used 48350 bytes) [1695199326.166][LK-0313] MQTT connect success in 92 ms AIOT_MQTTEVT_CONNECT [1695199326.166][LK-0309] sub: /a2******/Device1/user/get [1695199326.166][LK-0309] pub: /sys/a2******/Device1/thing/event/property/post [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 74 65 6D 70 65 72 61 74 75 72 65 | s":{"temperature [LK-030A] > 22 3A 20 33 30 2C 22 68 75 6D 69 64 69 74 79 22 | ": 30,"humidity" [LK-030A] > 3A 20 34 32 7D 2C 22 73 79 73 22 3A 7B 22 61 63 | : 42},"sys":{"ac [LK-030A] > 6B 22 3A 31 7D 7D | k":1}} [1695199326.244][LK-0309] pub: /sys/a2******/Device1/thing/event/property/post_reply [LK-030A] < 7B 22 63 6F 64 65 22 3A 32 30 30 2C 22 64 61 74 | {"code":200,"dat [LK-030A] < 61 22 3A 7B 7D 2C 22 69 64 22 3A 22 31 22 2C 22 | a":{},"id":"1"," [LK-030A] < 6D 65 73 73 61 67 65 22 3A 22 73 75 63 63 65 73 | message":"succes [LK-030A] < 73 22 2C 22 6D 65 74 68 6F 64 22 3A 22 74 68 69 | s","method":"thi [LK-030A] < 6E 67 2E 65 76 65 6E 74 2E 70 72 6F 70 65 72 74 | ng.event.propert [LK-030A] < 79 2E 70 6F 73 74 22 2C 22 76 65 72 73 69 6F 6E | y.post","version [LK-030A] < 22 3A 22 31 2E 30 22 7D | ":"1.0"} [1695199326.244][LK-0A08] DM recv generic reply demo_dm_recv_handler, type = 0 demo_dm_recv_generic_reply msg_id = 1, code = 200, data = {}, message = success -
Log on to the IoT Platform console, find and click the target Enterprise Edition instance to go to the Instance Details page, and then view the device status.
-
In the left-side navigation pane, choose . On the Devices page, find Device1 and view the device status. The value Online in the Device Status column indicates that the device is connected to IoT Platform.

-
Click View in the Actions column. On the device details page, click TSL Data. On the TSL Data tab, click Status. On the Status tab, view the device status.
The temperature and humidity data are submitted to IoT Platform by using the sample code in the
data_model_basic_demo.cfile.
-