All Products
Search
Document Center

IoT Platform:Connect a device to IoT Platform

Last Updated:Dec 07, 2023

Alibaba Cloud IoT Platform provides Link SDKs that allow devices to connect to IoT Platform. This topic describes how to enable communication between a device and IoT Platform by using the data_model_basic_demo sample code. In this example, a street lamp is developed.

Usage notes

In this topic, the permissions of a common 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 topic, Link SDK for C is used on the Linux operating system. To compile the SDK, we recommend that you use 64-bit Ubuntu 16.04.

  • 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

Procedure

  1. Obtain Link SDK for C from C SDK 4.x.

  2. Decompress the downloaded Link SDK for C and modify the device certificate in the SDK.

    Click data_model_basic_demo.c to download the data_model_basic_demo.c file, replace the data_model_basic_demo.c file in the path \LinkSDK\demos with the file that you downloaded, and configure 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

    • Public instance of the new version: iot-cn-***.mqtt.iothub.aliyuncs.com

    • Public instance of the previous version: a2***.iot-as-mqtt.cn-shanghai.aliyuncs.com

    The Message Queuing Telemetry Transport (MQTT) endpoint.

    • Endpoint of a public instance of the new version: On the Instance Details page, click View Development Configurations in the upper-right corner. In the Development Configurations panel, view the endpoint.

    • Endpoint of a public instance of the previous version: The endpoint is in the format of ${YourProductKey}.iot-as-mqtt.${YourRegionId}.aliyuncs.com.

    For more information about instances, see View the endpoint of an instance.

    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. For more information, see Obtain device authentication information.

    device_name

    device1

    device_secret

    8c684ef***

  3. Log on to the Linux virtual machine and run the following command to install the required software:

    sudo apt-get install -y build-essential make gcc
  4. Upload the LinkSDK file that is modified in Step 2 to the development environment.

  5. Go to the /LinkSDK SDK root directory and run the make command to compile the sample code:

    make clean
    make

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

  6. Run the sample code.

    ./output/data-model-basic-demo

    The following figure shows a sample success response:

    运行成功

  7. Log on to the IoT Platform console and go to the Public Instance page to view the status of your device.

    • In the left-side navigation pane, choose Devices > Devices. Then, find the device and view its online status. The value Online in the State/Enabled column indicates that the device is connected to IoT Platform.充电宝机柜设备在线

    • Click View in the Actions column to go to the Device Details page. Click the TSL Data tab, and then click Status to view the status of the device.

      In this example, the Current section in the following figure displays the operating current that is submitted to IoT Platform by using the sample code in the data_model_basic_demo.c file.

      路灯-物模型数据

What to do next

Subscribe to device messages from IoT Platform.