Connect a device to IoT Platform

Updated at:

Use the data_model_basic_demo sample program to simulate a street lamp device and connect it to IoT Platform through the C SDK.

Important notes

The steps in this topic are performed as a regular user. If an operation requires administrative permissions, prefix the command with sudo.

Prepare the development environment

  • The device-side C SDK runs on Linux. Compile it in a 64-bit Ubuntu 16.04 environment.

  • Required software:

    make 4.1 or later, gcc 5.4.0 or later.

    Install command:

    sudo apt-get install -y build-essential make gcc

Procedure

  1. Download the C SDK for devices.

    1. Log on to the IoT Platform console.

    2. In the upper-left corner of the console, select the region where your instance is located. On the Instance Overview tab, click Public Instance.

    3. In the navigation pane on the left, click Documents & Tools. In the Device SDK section, under Link SDK, click Customize SDK.

    4. Customize and download the SDK.

      On the SDK generation page, configure the following parameters:

      • SDK Version: Select v4.x

      • Device OS: Select Linux

      • Device Hardware: Select Single-board system

      • Protocol: Select MQTT, version 3.1.1

      • Data Encryption: Select TLS-CA

      • Device Authentication: Select Device Secret

      • Advanced Capabilities: Select Thing Specification Language model

      After you complete the settings, click Start Generation.

  2. Unzip the C SDK file and modify the device certificate information.

    Download data_model_basic_demo.c and use it to replace the file in \LinkSDK\demos. Update the following parameters:

    char *product_key       = "a2***";
    char *device_name       = "device1";
    char *device_secret     = "8c684ef***";
    ...
    ...
    char  *mqtt_host = "a2***.iot-as-mqtt.cn-shanghai.aliyuncs.com";

    Parameter

    Example

    Description

    mqtt_host

    • New public instances:iot-cn-***.mqtt.iothub.aliyuncs.com

    • Old public instances:a2***.iot-as-mqtt.cn-shanghai.aliyuncs.com

    The MQTT ingest endpoint for the device.

    • New public instances: On the Instance Details page, click View Development Configurations. The endpoint appears on the Development Configurations panel.

    • Old public instances: The endpoint format is${YourProductKey}.iot-as-mqtt.${YourRegionId}.aliyuncs.com.

    View instance endpoints.

    product_key

    a2***

    The device certificate saved when you added the device.

    You can also view the certificate on the Device Details page in the IoT Platform console. Obtain a device certificate.

    device_name

    device1

    device_secret

    8c684ef***

  3. On the Linux virtual machine, install the required software.

    sudo apt-get install -y build-essential make gcc
  4. Upload the modified LinkSDK directory to your Linux virtual machine.

  5. In the SDK root directory /LinkSDK, run the make command to compile the sample program.

    make clean
    make

    The compiled binary data-model-basic-demo is in ./output.

  6. Run the sample program.

    ./output/data-model-basic-demo

    Expected output:

    root@xxx:~/linkSDK# ./output/data-model-basic-demo
    [1637041632.700][LK-0313] MQTT user calls aiot_mqtt_connect api, connect
    [1637041632.700][LK-032A] mqtt host: axxx.iot-as-mqtt.cn-shanghai.aliyuncs.com
    [1637041632.700][LK-0317] user name: device1&axxx,0
    [1637041632.700][LK-0318] password: 86761xxx¥21D
    establish tcp connection with server(host='axxx.iot-as-mqtt.cn-shanghai.aliyuncs.com', port=[443])
    success to establish tcp, fd=3
    local port: 35604
    [1637041632.711][LK-1000] establish mbedtls connection with server(host='axxx.iot-as-mqtt.cn-shanghai.aliyuncs.com', port=[443])
    [1637041632.766][LK-1000] success to establish mbedtls connection, (cost 45252 bytes in total, max used 47988 bytes)
    [1637041632.811][LK-0313] MQTT connect success in 108 ms
    AIOT_MQTTEVT_CONNECT
    [1637041632.811][LK-0309] pub: /sys/axxx/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 4C 69 67  68 74 43 75 72 72 65 6E | s":{"LightCurren
    [LK-030A] > 74 22 3A 20 31 2E 35 7D  2C 22 73 79 73 22 3A 7B | t": 1.5},"sys":{
    [LK-030A] > 22 61 63 6B 22 3A 31 7D  7D                       | "ack":1}}
    
    [1637041632.833][LK-0309] pub: /sys/axxx/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"}
    
    [1637041632.833][LK-0A08] DM recv generic reply
    demo_dm_recv_handler, type = 0
    msg_id = 1, code = 200, data = {}, message = success
  7. In the IoT Platform console, go to the Public Instance page. Verify the device status.

    • Choose Device Management > Devices. On the Devices page, enter device1 in the DeviceName search box to find the target device. Verify that the device status is Online.

    • Click View in the Actions column. On the Device Details page, click the TSL Data tab and then Running Status.

      The data_model_basic_demo.c sample reports the operating current value.

      On the Running Status tab, the Operating Current property displays a value of 1.5 A, indicating that the device has successfully reported this property value.

What to do next

You can now subscribe to device messages from the server.