This article describes how to call the API operations of Link SDK for C to to achieve broadcast communication. In this example, the ./demos/mqtt_broadcast_demo.c sample code file is used.
Background information
- For more information about broadcast communication, see Overview.
- You must establish an MQTT connection. For more information, see Overview.
Procedure
- Initialize a client.
- Configure required features.
- Establish a connection.
- Optional:If you want to send a broadcast message to all devices under a product, the devices
must call the aiot_mqtt_sub operation to subscribe to the 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 .
a1oGs****** indicates the ProductKey of the device.
test indicates a custom field.
For more information about topics, see What is a topic?.
|
- Receive the broadcast message.
- After the device is connected to IoT Platform, you can call the PubBroadcast operation to send a broadcast message.
Notice
We recommend that you send a maximum of one broadcast message per minute to prevent
throttling.
For more information, see Broadcast messages.
- The device calls the aiot_mqtt_recv operation to receive the message. Then, the callback is called to perform the required operations.
In this example, the callback is demo_mqtt_default_recv_handler
.
- Define the callback to process the broadcast message.
Check whether the message is a broadcast message based on the topic format.
- If you need to send the broadcast message to all online devices under a product, the
devices do not need to subscribe to a broadcast topic first. After each device receives
the message, you can check whether the message is a broadcast message based on the
topic format. The format of a broadcast topic is
/sys/${productKey}/${deviceName}/broadcast/request/${MessageId}
.
- If you need to send the broadcast message to all devices that subscribe to a custom
topic, the devices must subscribe to the topic first. 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 needs.
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: {
}
}
}
- End the connection.
- 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 running results, see View logs.