All Products
Document Center

IoT Platform:Connect a device to IoT Platform and submit data

Last Updated:Jan 23, 2024

After you obtain a device certificate (ProductKey, DeviceName, and DeviceSecret), you can connect the device to an Enterprise Edition instance over Message Queuing Telemetry Transport (MQTT). This topic describes how to use Link SDK for C of IoT Platform to configure a simulated device and submit data.


A product and a device are created. For more information, see Create a product and device.

Before you begin

In this example, the permissions of a regular user are used to perform all operations. If you want to perform specific operations that require administrator permissions, run the sudo command.

Prepare a development environment

  • In this example, Link SDK for C is used in the Linux operating system. To compile the SDK, we recommend that you use 64-bit Ubuntu 20.04.

    In this example, an Elastic Compute Service (ECS) instance is purchased and used as a simulated device. The simulated device is connected to IoT Platform and used to submit data. For more information about how to purchase an ECS instance, see Creation methods.

  • The following software is required for SDK development and compilation:

    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

Develop a sample program for the device


In this example, a Thing Specification Language (TSL)-specific topic is used to submit data. You can also use custom topics to send data to devices. For more information, see Use custom topics.

  1. Log on to the ECS instance. For more information about how to log on to an ECS instance, see Connection method overview.

  2. Run the following command to install GCC and Make:

    sudo apt-get install -y build-essential make gcc
  3. Upload the file and decompress the file.

    1. Upload on-premises files to the ECS instance.

    2. Run the following commands to install unzip:

      apt update
      apt install unzip
    3. Go to the directory in which the file resides and run the following command to decompress the file:

  4. Go to the /LinkSDK/demos directory, open the data_model_basic_demo.c file, and then configure 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-******";






    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.







    The 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 the endpoint of an instance.

  5. 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}");
  6. 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);
  7. In the /LinkSDK root directory, run the following commands to compile the sample program:

    make clean

    The generated data-model-basic-demo file is stored in the ./output directory.

  8. Run the following command to execute the sample program:


    If 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-******
    [1695199326.066][LK-0317] user name: Device1&a2******
    establish tcp connection with server(host='iot-cn-******', port=[443])
    success to establish tcp, fd=3
    local port: 36030
    [1695199326.077][LK-1000] establish mbedtls connection with server(host='iot-cn-******', 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
    [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 |","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
  9. Log on to the IoT Platform console, find the Enterprise Edition instance that you want to manage, and then click the name of the instance to go to the Instance Details page. On the Instance Details page, view the device status.

    1. In the left-side navigation pane, choose Devices > Devices. 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.


    2. 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.

      In this example, the temperature data and the humidity data are submitted to IoT Platform by using the sample code in the data_model_basic_demo.cfile.


What to do next

Forward data to OTS