Communicate using custom MQTT topics
The software development kit (SDK) provides MQTT API operations for direct communication with Alibaba Cloud IoT Platform. You can publish messages to custom topics to send data to the platform and subscribe to custom topics to receive data from the platform.
Communication flowchart
The following flowchart shows how to use an MQTT topic to communicate with IoT Platform.
Call the
IOT_MQTT_Construct()function to establish a persistent connection between the device and the cloud platform.This function sends a connection request to the cloud platform. The required signature information is obtained from the
IOT_Sign_MQTT()function in thedev_signfeature.If the connection is successful, the function returns a
handleparameter. This handle can be used as an input parameter for all subsequent MQTT network API operations.
Call the
IOT_MQTT_Subscribe()function to specify the topics from which the device will receive messages.Enter the main loop.
Call
IOT_MQTT_Publish()orIOT_MQTT_Publish_Simple()to publish messages to the platform.The
IOT_MQTT_Yield()function receives messages from the platform and calls the callback function specified inIOT_MQTT_Subscribe()to process the data.
Feature API operations
Function name | Description |
After an MQTT connection is established, call this function to check if the persistent connection is normal. | |
The constructor for an MQTT instance. The input parameter is the | |
The function to destroy an MQTT instance. The input parameter is the handle created by | |
During an MQTT session, this function constructs a complete | |
During an MQTT session, this function constructs a complete | |
During an MQTT session, this function constructs a complete | |
During an MQTT session, this function constructs a complete | |
During an MQTT session, this function constructs a complete | |
During an MQTT session, this is the main MQTT loop function. It maintains the heartbeat and receives downlink messages from the server. |
Required HAL functions
These functions are optional. You must implement them if you want the SDK to provide the MQTT channel feature.
Function name | Description |
Returns the number of milliseconds that have elapsed since the device was powered on. | |
Puts the device to sleep for the number of milliseconds specified by the input parameter. | |
Destroys a TLS connection. This is used for the MQTT feature with an encrypted connection. | |
Establishes a TLS connection. This is used for the MQTT feature with an encrypted connection. | |
Reads data from a TLS connection. This is used for the MQTT feature with an encrypted connection. | |
Writes data to a TLS connection. This is used for the MQTT feature with an encrypted connection. | |
Terminates the TLS connection for the MQTT feature. | |
Establishes a TCP connection. This includes domain name resolution and TCP connection establishment. | |
Reads stream data from a TCP connection within a specified time and returns the number of bytes read. | |
Sends stream data to a TCP connection within a specified time and returns the number of bytes sent. |
The user API operations provided by the SDK are listed in src/mqtt/mqtt_api.h. The HAL functions that you may need to implement are listed in src/mqtt/mqtt_wrapper.h. For more information, see the code.