MQTT connection issues

Updated at:

This topic describes common issues and solutions for devices that use C Link SDK 3.x to connect to IoT Platform over the Message Queuing Telemetry Transport (MQTT) protocol.

What are the IoT Platform access domain name and port number?

Access domain name:

  • For new public instances and Enterprise instances, go to the Instance Overview page in the IoT Platform console. Click an instance to view its access domain name on the Instance Details page.

  • For public instances, the access domain name is ${YourProductKey}.iot-as-mqtt.${YourRegionId}.aliyuncs.com. The placeholders are defined as follows:

    • ${YourProductKey}: Replace this with the ProductKey of the product to which the device belongs. You can find the ProductKey on the Device Details page in the IoT Platform console.

    • ${YourRegionId}: Replace this with your region ID. For more information, see Supported regions.

Port number: 1883.

For more information, see View instance endpoints.

When connecting over MQTT, can different devices use the same clientID to connect to the server?

No. The clientID must be globally unique. If multiple devices use the same clientID to connect to IoT Platform at the same time, the device that connected first is forcibly disconnected.

What is the MQTT protocol version?

The MQTT version is set in the MQTT connect packet. The current software development kit (SDK) (V2.02) uses MQTT 3.1.1.

You can modify the value of src\mqtt\mqtt_client.h IOTX_MC_MQTT_VERSION in the SDK code to change the supported version. A value of 3 indicates version 3.1 and a value of 4 indicates version 3.1.1.

The server returns a "400" error during MQTT device authentication

A 400 error indicates that authentication failed. Check whether the device certificate information, such as ProductKey, DeviceName, and DeviceSecret, is correct.

Does the C SDK support MQTT connections from iOS?

The C SDK can be ported to any system that supports the C language. For iOS systems, we recommend that you find an open source Objective-C implementation.

The mqtt-example device goes offline immediately after it connects. How can I modify mqtt-example to keep the device online?

The mqtt-example program automatically exits after it sends one message. To keep the device online, you can use one of the following methods.

  • When you run mqtt-example, use the command line ./mqtt-example loop. The device then remains online.

  • Modify the demo code. The mqtt-example code calls IOT_MQTT_Destroy at the end, which causes the device to go offline. To prevent this, you can modify the code to remove the IOT_MQTT_Unregister and IOT_MQTT_Destroy calls.

    while(1)      
    {       
        IOT_MQTT_Yield(pclient, 200);   
        HAL_SleepMs(100);  
    }

How do I set the heartbeat interval?

You can set the value of keepalive_interval_ms in IOT_MQTT_Construct. IoT Platform uses this value as the heartbeat interval. The value of keepalive_interval_ms must be 30 seconds to 1,200 seconds.

What is the device-side reconnection mechanism?

The device sends a ping request at the interval specified by keepalive_interval_ms and then waits for a ping response.

If the device does not receive a ping response within the keepalive_interval_ms period, or if an error occurs during a send or receive operation, the SDK considers the network disconnected and initiates a reconnection.

The reconnection mechanism is triggered internally by the SDK and requires no user intervention. During reconnection, the device is re-authenticated. If authentication is successful, the device attempts to establish an MQTT connection again. The reconnection process continues until the connection is successful.

How does IoT Platform detect that a device is offline?

IoT Platform waits for a ping request based on the keepalive setting in the MQTT CONNECT packet. If a ping request is not received within the specified time, the device is considered offline.

The maximum latency that IoT Platform can accept is 5 seconds.

Does the device SDK support automatic reconnection for the MQTT and CCP protocols?

Yes, it does. For example, in a test scenario, a development board is connected to a router over Wi-Fi. If you unplug the network cable, both the MQTT and CCP protocols automatically attempt to reconnect to the server. The retry intervals increase exponentially, such as 1 s, 2 s, 4 s, and 8 s, up to a default maximum of 60 s. If the network remains disconnected after the maximum interval is reached, the device attempts to reconnect every 60 s. You can set the maximum interval.

When I publish QoS 1 data, the error MQTT_PUSH_TO_LIST_ERROR(-42) sometimes occurs. How can I resolve this?

Packets that require an ACK are stored until the ACK is received. This storage has a limit. When the number of waiting packets reaches the limit, the MQTT_PUSH_TO_LIST_ERROR(-42) error is triggered.

This error may occur due to poor network conditions or a high sending frequency. If you rule out these two issues and the sending frequency is as expected, you can adjust the values of IOTX_MC_REPUB_NUM_MAX, IOTX_MC_SUB_REQUEST_NUM_MAX, and IOTX_MC_SUB_NUM_MAX as needed.

If your business requirements permit, you can also change the publish Quality of Service (QoS) level to 0.

What is the purpose of IOT_MQTT_Yield?

IOT_MQTT_Yield attempts to receive data. Therefore, you must actively call this function when you need to receive data, such as after you subscribe or unsubscribe, after you publish a QoS 1 message, or when you expect to receive published data.

What does the timeout parameter in IOT_MQTT_Yield mean?

IOT_MQTT_Yield attempts to receive data and exits only after the timeout period expires.

What is the difference between IOT_MQTT_Yield and HAL_SleepMs?

Both IOT_MQTT_Yield and HAL_SleepMs block execution for a period of time. However, IOT_MQTT_Yield actively reads data from the network socket, whereas HAL_SleepMs simply pauses the thread until the timeout period elapses.

How do I receive messages in a loop?

You can call IOT_MQTT_Yield in a loop. The function automatically maintains the heartbeat and receives data.

If I subscribe to multiple topics, can a single call to IOT_MQTT_Yield receive messages from all of them?

Yes. First, confirm that the topic has both publish and subscribe permissions. If it does, a single call to IOT_MQTT_Yield can receive multiple packets.

For MQTT connections, is polling with IOT_MQTT_Yield the only way to get data?

If you use a TCP/IP protocol stack that can actively notify the application layer when data arrives, you can modify the implementation to trigger IOT_MQTT_Yield using an event-driven approach. However, this requires significant changes. We recommend that you evaluate whether this modification is necessary.

The modification procedure is as follows:

Adjust the socket API in utils_net.c to a callback-based API that is invoked when TCP data arrives.

When the TCP layer notifies the upper layer of new data, the MQTT server is triggered to internally execute `IOT_MQTT_Yield`. Therefore, you do not need to externally call `IOT_MQTT_Yield` to read the data.

If TCP cannot actively report data but the OS supports multi-threading, you can start another thread in mqtt-example. In this thread, you can use the following code to receive data. When data is received, it can trigger the main thread to process the data. This allows the main thread to handle other logic most of the time.

while(1)
 {      
    IOT_MQTT_Yiled(pclient, 200);    
    HAL_SleepMs(200);   
 }

If the system that you are using does not support multi-threading, you can only reduce the timeout interval of IOT_MQTT_Yield and increase its call frequency. You can perform other operations in the interval between calls to minimize the blocking of other operations.

Under what circumstances does a subscribe timeout occur?

If the system does not receive a SUBACK packet within a period of 2 × request_timeout_ms, a subscribe timeout occurs. A timeout notification is then sent through the event_handle function.

After you subscribe, immediately execute IOT_MQTT_Yield to try to read the SUBACK. Do not use HAL_SleepMs.

IOTX_MQTT_EVENT_SUBCRIBE_NACK is returned when subscribing

Check whether the operation permission for the topic is set to Subscribe.

If a "no authorization" error occurs when you publish a message, confirm that you have Publish permission.

MQTT published message body size limit

The MQTT protocol packet is limited by the size of the write_buf and read_buf parameters in IOT_MQTT_Construct.

The MQTT protocol packet size cannot exceed 256 KB. Messages that exceed this limit are discarded.

What is the format of an MQTT pub message payload?

IoT Platform does not specify the fields for the pub message payload. You can define your own protocol based on your application scenario and then send the data to the server in JSON format within the pub message payload.

"mqtt read buffer is too short" error occurs during an ota_mqtt update

This error occurs because the MQTT buffer is set to a size that is too small. Specifically, the allocated sizes for pread_buf and pwrite_buf in mqtt_param are insufficient. You can modify the size of OTA_MQTT_MSGLEN as needed.

Can I use a direct MQTT connection for OTA updates?

Yes, you can. You can use MQTT for firmware downloads and over-the-air (OTA) updates. However, the update package size is limited to a maximum of 16 MB. This method is suitable for resource-constrained devices.

When MQTT over TLS is enabled, the MQTT connection fails at runtime and returns error code 0x2700

If MQTT over TLS is disabled, messages can be subscribed to and published successfully. If MQTT over TLS is enabled, the connection fails. First, confirm that mbedtls has not been modified. This component handles encryption between the transport and application layers and must not be changed. If mbedtls is unmodified, check whether the system time is correct. An incorrect system time can also cause certificate validation to fail.

Is root.crt certificate validation required for an MQTT connection?

If you connect to MQTT over TLS, you must download the root certificate.

If you develop using the demo that is provided by IoT Platform, you do not need to download the root certificate because it is already included in the demo.

Which QoS levels does IoT Platform support?

IoT Platform supports QoS 0 and QoS 1 for both the MQTT and CCP protocols. QoS 2 is not supported.