Communicate using custom MQTT topics

更新时间:
复制 MD 格式

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.image

  1. 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 the dev_sign feature.

    • If the connection is successful, the function returns a handle parameter. This handle can be used as an input parameter for all subsequent MQTT network API operations.

  2. Call the IOT_MQTT_Subscribe() function to specify the topics from which the device will receive messages.

  3. Enter the main loop.

    • Call IOT_MQTT_Publish() or IOT_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 in IOT_MQTT_Subscribe() to process the data.

Feature API operations

Function name

Description

IOT_MQTT_CheckStateNormal

After an MQTT connection is established, call this function to check if the persistent connection is normal.

IOT_MQTT_Construct

The constructor for an MQTT instance. The input parameter is the iotx_mqtt_param_t struct. This function connects to the MQTT server and returns the handle of the created instance.

IOT_MQTT_Destroy

The function to destroy an MQTT instance. The input parameter is the handle created by IOT_MQTT_Construct.

IOT_MQTT_Publish

During an MQTT session, this function constructs a complete MQTT Publish message and sends the message to the server.

IOT_MQTT_Publish_Simple

During an MQTT session, this function constructs a complete MQTT Publish message and sends the message to the server. The parameters do not include complex data types such as structs.

IOT_MQTT_Subscribe

During an MQTT session, this function constructs a complete MQTT Subscribe message and sends a subscription request to the server.

IOT_MQTT_Subscribe_Sync

During an MQTT session, this function constructs a complete MQTT Subscribe message, sends a subscription request to the server, and waits for an acknowledgement.

IOT_MQTT_Unsubscribe

During an MQTT session, this function constructs a complete MQTT Unsubscribe message and sends an unsubscribe request to the server.

IOT_MQTT_Yield

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

HAL_UptimeMs

Returns the number of milliseconds that have elapsed since the device was powered on.

HAL_SleepMs

Puts the device to sleep for the number of milliseconds specified by the input parameter.

HAL_SSL_Destroy

Destroys a TLS connection. This is used for the MQTT feature with an encrypted connection.

HAL_SSL_Establish

Establishes a TLS connection. This is used for the MQTT feature with an encrypted connection.

HAL_SSL_Read

Reads data from a TLS connection. This is used for the MQTT feature with an encrypted connection.

HAL_SSL_Write

Writes data to a TLS connection. This is used for the MQTT feature with an encrypted connection.

HAL_TCP_Destroy

Terminates the TLS connection for the MQTT feature.

HAL_TCP_Establish

Establishes a TCP connection. This includes domain name resolution and TCP connection establishment.

HAL_TCP_Read

Reads stream data from a TCP connection within a specified time and returns the number of bytes read.

HAL_TCP_Write

Sends stream data to a TCP connection within a specified time and returns the number of bytes sent.

Note

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.