All Products
Search
Document Center

IoT Platform:C Link SDK

Last Updated:Nov 16, 2023

This topic describes how to use Link SDK for C to connect a device to IoT Platform and enable communication.

Prerequisites

IoT Platform is activated.

Background information

You can configure Link SDK for C to connect a simulated device to IoT Platform over Message Queuing Telemetry Transport (MQTT). In this example, a device named LightSwitch is used. The device can submit data to IoT Platform by using custom topics.

For more information, see IoT Platform.

Create a product and a device

  1. Log on to the IoT Platform console. Then, click the card of the public instance.

  2. In the left-side navigation pane, choose Devices > Products. On the Products page, click Create Product.

  3. On the Create Product page, set the Product Name parameter to NightLightSwitch and the Category parameter to Custom Category. Use the default values for other parameters. Then, click OK.

    创建产品
  4. On the Create Product page, click Add in the Add Device section.

  5. On the Devices page, click Add Device.

  6. In the Add Device dialog box, enter a device name. Example: LightSwitch. Then, click OK.

    添加设备
  7. In the The devices have been added. dialog box, click Copy Device Certificate. Then, save the device certificate information to an on-premises server. For more information, see Device certificates.

Prepare the development environment

In this example, the development environment is Ubuntu 16.04, and the development tools are gcc and make. For more information, see Project configurations.

  1. Install Ubuntu 16.04.

    For more information, see Ubuntu.

  2. Run the following command to install gcc and make:

    sudo apt-get -y install gcc make

Configure the SDK

  1. Download a demo package from Demo file to the development environment and then decompress the package.

    IoT Platform allows you to develop custom features for Link SDKs. For more information, see Obtain Link SDK for C.

  2. Open the ./LinkSDK/demos/mqtt_basic_demo.c file and configure the relevant parameters.

    • Configure the parameters that are used to specify device connection information to connect the device to IoT Platform. The following table describes the parameters. Sample code:

      char *product_key       = "a18wP******";
      char *device_name       = "LightSwitch";
      char *device_secret     = "uwMTmVAMnGGHaAkqmeDY6cHxxB******";
      char  *mqtt_host = "a18wP******.iot-as-mqtt.cn-shanghai.aliyuncs.com";

      Parameter

      Example

      Description

      mqtt_host

      a18wP******.iot-as-mqtt.cn-shanghai.aliyuncs.com

      The endpoint to which you want to connect the device.

      • If you use a public instance of the previous version, the endpoint of the public instance is in the ${YourProductKey}.iot-as-mqtt.${YourRegionId}.aliyuncs.com format.

      product_key

      a18wP******

      The device certificate information. The device certificate is saved on your on-premises server after the device is created.

      You can view the device certificate on the Device Details page in the IoT Platform console. For more information, see Obtain device verification information.

      device_name

      LightSwitch

      device_secret

      uwMTmVAMnGGHaAkqmeDY6cHxxB******

    • Specify a topic to which you want the device to subscribe to receive messages from IoT Platform. Example: /a18wP******/LightSwitch/user/get. Sample code:

      Note

      If you want to use the feature, uncomment the code that is enclosed within a pair of forward slash and asterisk combinations (/**/).

          {
              char *sub_topic = "/a18wP******/LightSwitch/user/get";
      
              res = aiot_mqtt_sub(mqtt_handle, sub_topic, NULL, 1, NULL);
              if (res < 0) {
                  printf("aiot_mqtt_sub failed, res: -0x%04X\n", -res);
                  return -1;
              }
          }

      Parameter

      Example

      Description

      sub_topic

      /a18wP******/LightSwitch/user/get

      The topic on which the device has the Subscribe permission. The device can receive messages from IoT Platform by using this topic.

      You can view custom topics in the IoT Platform console. On the Product Details page, click the Topic Categories tab. On the tab that appears, click Topic Category. For more information, see Topics.

      In this example, the default custom topic is used. Parameter description:

      • a1oGs****** indicates the ProductKey of the device.

      • LightSwitch indicates the DeviceName of the device.

    • Specify a topic that can be used by the device to send messages. Example: /a18wP******/LightSwitch/user/update. Then, specify content for a message that you want to send. Sample code:

      Note

      If you want to use the feature, uncomment the code that is enclosed within a pair of forward slash and asterisk combinations (/**/).

           {
              char *pub_topic = "/a18wP******/LightSwitch/user/update";
              char *pub_payload = "{\"id\":\"1\",\"version\":\"1.0\",\"params\":{\"LightSwitch\":0}}";
      
              res = aiot_mqtt_pub(mqtt_handle, pub_topic, (uint8_t *)pub_payload, (uint32_t)strlen(pub_payload), 0);
              if (res < 0) {
                  printf("aiot_mqtt_sub failed, res: -0x%04X\n", -res);
                  return -1;
              }
          }

      Parameter

      Example

      Description

      pub_topic

      /a18wP******/LightSwitch/user/update

      The topic on which the device has the Publish permission. The device can use the topic to send messages to IoT Platform.

      You can view custom topics in the IoT Platform console. On the Product Details page, click the Topic Categories tab. On the tab that appears, click Topic Category. For more information, see Topics.

      In this example, the default custom topic is used. Parameter description:

      • a1oGs****** indicates the ProductKey of the device.

      • LightSwitch indicates the DeviceName of the device.

      pub_payload

      {\"id\":\"1\",\"version\":\"1.0\",\"params\":{\"LightSwitch\":0}}

      The message that is sent to IoT Platform.

      In this example, a custom topic is used. You can use custom message formats.

  3. In the SDK root directory, run the make command.

    The ./output/mqtt-basic-demo executable sample code file is generated.

Execution result

In the SDK root directory, run the following command to run the executable file:

./output/mqtt-basic-demo
  • You can view the execution result on the device.

    • The following log data indicates that the LightSwitch device is connected to IoT Platform.

      [1619768570.433][LK-0313] MQTT user calls aiot_mqtt_connect api, connect
      [1619768570.433][LK-0317] LightSwitch&a18wP******
      [1619768570.433][LK-0318] 933FE333F78F42E2AAD384F629C771277E06D459577942EA1F04ACFDFE******
      core_sysdep_network_establish host a18wP******.iot-as-mqtt.cn-shanghai.aliyuncs.com port 8883, type 0
      establish tcp connection with server(host='a18wP******.iot-as-mqtt.cn-shanghai.aliyuncs.com', port=[8883])
      success to establish tcp, fd=3
      local port: 49122
      [1619768570.599][LK-1000] establish mbedtls connection with server(host='a18wP******.iot-as-mqtt.cn-shanghai.aliyuncs.com', port=[8883])
      [1619768570.600][LK-1000] success to establish mbedtls connection, (cost 45134 bytes in total, max used 47870 bytes)
      [1619768570.644][LK-0313] MQTT connect success in 212 ms
      AIOT_MQTTEVT_CONNECT
    • The following log data indicates that the LightSwitch device subscribes to the /a18wP******/LightSwitch/user/get topic.

      [1620373302.144][LK-0309] sub: /a18wP******/LightSwitch/user/get

      After you subscribe to a topic, you can return to the IoT Platform console. On the Topics tab of the Device Details page, find the topic and click Publish Notification in the Actions column. In the Publish Notification dialog box, enter a message, such as This is a test message from Alibaba IoT Platform.. Then, send the message from IoT Platform to the device.发送消息

    • The following log data indicates that the LightSwitch device uses the /a18wP******/LightSwitch/user/update topic to submit a message to IoT Platform.

      [1619768570.644][LK-0309] pub: /a18wP******/LightSwitch/user/update
      
      [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 53 77 69 74 63 68 | s":{"LightSwitch
      [LK-030A] > 22 3A 30 7D 7D       
  • You can log on to the IoT Platform console to view the device status and operation logs.

    • In the left-side navigation pane, choose Devices > Devices. Then, find the device and view the device status. If the value in the State/Enabled column is Online, the device is connected to IoT Platform.

    • In the left-side navigation pane, choose Maintenance > Device Log. On the Device Log page, select the NightLightSwitch product. You can view logs that record when the devices of the product went online, subscribed to a topic, and submitted messages.

If an error occurs during debugging, fix the error as prompted. For more information, see Error codes for devices.

What to do next

  • After the device is connected to IoT Platform, you can manage and monitor the device. For more information about the features of IoT Platform, see Features.

  • For more information about how to use Link SDK for C to configure advanced features, see Overview.