All Products
Search
Document Center

IoT Platform:Example

Last Updated:Jun 16, 2026

Use the sample code file ./demos/mqtt_broadcast_demo.c in Link SDK for C to implement broadcast communication for a device.

Background information

  • For more information about broadcast communication, see Overview.

  • The broadcast communication feature is available only for devices connected to IoT Platform over MQTT. For more information about the MQTT connection-related code, see Overview.

Procedure

  1. Initialize a client.

    For more information, see Step 1: Initialization.

  2. Configure required features.

    For more information, see Step 2: Configure features.

  3. Establish a connection.

    For more information, see Step 3: Request 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 What is a topic?.

  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, the callback function 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 callback processing logic 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. Close the connection.

    For more information, see Step 8: Disconnect.

  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 it to generate an executable file. In this example, the demos/mqtt-broadcast-demo executable file is generated.

    For more information, see Compile and run.

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