Connect a device to IoT Platform

更新时间:
复制 MD 格式

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 by a regular user. If you encounter an operation that requires administrative permission, run 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.

      Under Advanced Capabilities, select Thing Specification Language model, keep other defaults, and click Start Generation to download the C SDK ZIP file.Download SDK from public instance

  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:

    Successful execution

  7. In the IoT Platform console, go to the Public Instance page. Verify the device status.

    • Choose Device Management > Devices. If the device status is Online, the device is connected to IoT Platform.Device online status

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

      TSL data - street lamp

What to do next

You can now subscribe to device messages from the server.