Use the sample code file ./demos/mqtt_broadcast_demo.c in Link SDK for C to implement broadcast communication for a device.
Background information
Procedure
Initialize a client.
For more information, see Step 1: Initialization.
Configure required features.
For more information, see Step 2: Configure features.
Establish a connection.
For more information, see Step 3: Request a connection.
-
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?.
-
-
-
Receive the broadcast message.
-
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.
ImportantWe recommend that you send at most one broadcast message per minute to prevent throttling.
For more information, see Broadcast messages.
-
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. -
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: { } } } -
-
Close the connection.
For more information, see Step 8: Disconnect.
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-demoexecutable file is generated.For more information, see Compile and run.
-
For more information about the execution result, see View logs.