All Products
Search
Document Center

IoT Platform:Example

Last Updated:Sep 11, 2023

This topic describes how to call the API operations of Link SDK for C to implement the broadcast communication feature for a device. In this example, a sample code file named ./demos/mqtt_broadcast_demo.c is used.

Background information

  • For more information about the broadcast communication feature, see Overview.

  • The broadcast communication feature is available only for devices that are connected to IoT Platform over Message Queuing Telemetry Transport (MQTT). For more information about the MQTT connection-related code for configuring the feature, see Overview.

Procedure

  1. Initialize a client.

    For more information, see Step 1: Initialize a client.

  2. Configure required features.

    For more information, see Step 2: Configure required features.

  3. Establish a connection.

    For more information, see Step 3: Establish a connection.

  4. Optional. If you want to send a broadcast message to all devices of a product, the devices must call the aiot_mqtt_sub function to subscribe to a specified topic.

    • Sample code:

          {
              char *sub_topic = "/broadcast/a18wP******/test";
      
              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;
              }
          }
    • Parameters:

      Parameter

      Example

      Description

      sub_topic

      /broadcast/a18wP******/test

      The topic to which the broadcast message is sent. Format: /broadcast/${productKey}/<custom field>. Parameters in this example:

      • a18wP******is the ProductKey of the device.

      • test indicates a custom field.

      For more information about topics, see Topics.

  5. Receive the broadcast message.

    1. After the device is connected to IoT Platform, you can call the PubBroadcast operation of IoT Platform to send a broadcast message to the device.

      Important

      We recommend that you send at most one broadcast message per minute to prevent throttling.

      For more information, see Broadcast messages.

    2. The device calls the aiot_mqtt_recv function to receive the broadcast message. Then, a callback function is called to perform the required operations.

      In this example, a callback function named demo_mqtt_default_recv_handler is used.

    3. Define a callback function to process the broadcast message.

      Check whether the message is a broadcast message based on the topic format.

      • If you want to send the broadcast message to all online devices of a product, the devices do not need to subscribe to a broadcast topic. After each device receives the broadcast message, you can check whether the message is a broadcast message based on the broadcast topic format. The format of a broadcast topic is /sys/${productKey}/${deviceName}/broadcast/request/${MessageId}.

      • If you want to send the broadcast message to all devices that subscribe to a custom broadcast topic, the devices must subscribe to the topic. The format of a custom broadcast topic is /broadcast/${productKey}/<custom field>.

      You can specify the processing logic of the callback based on your business requirements. In this example, the broadcast message is printed.

      void demo_mqtt_default_recv_handler(void *handle, const aiot_mqtt_recv_t *packet, void *userdata)
      {
          switch (packet->type) {
              ……
              ……
              case AIOT_MQTTRECV_PUB: {
                  printf("pub, qos: %d, topic: %.*s\n", packet->data.pub.qos, packet->data.pub.topic_len, packet->data.pub.topic);
                  printf("pub, payload: %.*s\n", packet->data.pub.payload_len, packet->data.pub.payload);
                  /* TODO: Print the broadcast message */           
              }
              break;
      
              ……
              ……       
      
              default: {
      
              }
          }
      }
  6. End the connection.

  7. Exit the program.

    For more information, see Step 9: Exit the program.

What to do next

  • After you configure the sample code file, compile the file to generate an executable file. In this example, the demos/mqtt-broadcast-demo executable file is generated.

    For more information, see Compilation and running.

  • For more information about the execution result, see View logs.