Skip to content

Contributed by wxlinus, curated by Ai-Thinker

CLion Dev (1): Porting CLinkSDK to the M61 Board for Alibaba Cloud IoT In the previous tutorial we covered how to port Alibaba Cloud's CLinkSDK. This time we'll talk about how to use the CLinkSDK to connect to the Alibaba Cloud IoT Platform; a successful connection proves the porting is fine.

  1. Alibaba Cloud IoT Platform configuration Create a product

Create a device

Copy the device triplet (will be configured on the device side later)

  1. Device-side development The WiFi connection is hardcoded for now, without provisioning. There are already many tutorials on the forum about connecting WiFi, so I won't cover it here.

Configure the Alibaba Cloud IoT Platform device triplet

Subscribe to topics

  1. Compile and flash to the device

  2. Verifying the result You can see the serial log prints connection success.

The device is online.

The Alibaba Cloud IoT Platform logs also show the device messages.

  1. Summary The related initialization and configuration functions can be referenced in the project code. They basically don't need modification after configuration and all have detailed comments.

Device topic subscription is mainly implemented by the following function:

c
uint8_t user_mqtt_sub(char *sub_topic, uint8_t qos) {
    int32_t res = STATE_SUCCESS;
    // MQTT topic subscription example; use it according to your business needs
    {
        // property set
        mqtt_properties_t *sub_props = aiot_mqtt_props_init();
        // user-defined property
        mqtt_property_t user_prop = {
            .id = MQTT_PROP_ID_USER_PROPERTY,
            .value.str_pair.key.len = strlen("demo_key"),
            .value.str_pair.key.value = (uint8_t *) "demo_key",
            .value.str_pair.value.len = strlen("demo_value"),
            .value.str_pair.value.value = (uint8_t *) "demo_value",
        };
        // add the user-defined property to the property set
        aiot_mqtt_props_add(sub_props, &user_prop);
        // subscription options
        sub_options_t opts = {
            .no_local = 1,
            .qos = qos,
            .retain_as_publish = 1,
            .retain_handling = 1,
        };
        res = aiot_mqtt_sub_v5(mqtt_handle, sub_topic, &opts, NULL, NULL, sub_props);
        aiot_mqtt_props_deinit(&sub_props);
        if (res < 0) {
            LOG_E("aiot_mqtt_sub failed, res: -0x%04X\n", -res);
            aiot_mqtt_deinit(&mqtt_handle);
            return -1;
        }
    }
}

Publishing is implemented by the following function:

c
uint8_t user_mqtt_pub(char *pub_topic, char *pub_payload, uint8_t qos) {
    int32_t res = STATE_SUCCESS;
    mqtt_properties_t *pub_props = aiot_mqtt_props_init();
    mqtt_property_t response_prop = {
        .id = MQTT_PROP_ID_RESPONSE_TOPIC,
        .value.str.len = strlen(pub_topic),
        .value.str.value = (uint8_t *) pub_topic,
    };
    // mqtt5 custom property feature
    //    char *demo_data_str = "1";
    //    mqtt_property_t correlation_prop = {
    //            .id = MQTT_PROP_ID_CORRELATION_DATA,
    //            .value.str.len = strlen(demo_data_str),
    //            .value.str.value = (uint8_t *) demo_data_str,
    //    };
    aiot_mqtt_props_add(pub_props, &response_prop);
    //    aiot_mqtt_props_add(pub_props, &correlation_prop);
    /* MQTT 5.0 topic alias feature. Within one connection, after a message has been sent to pub_topic,
     * sending to this topic again enables the topic alias feature, and the uplink message no longer carries the topic field.
     */
    res = aiot_mqtt_pub_v5(mqtt_handle, pub_topic, (uint8_t *) pub_payload,
                           (uint32_t) (strlen(pub_payload)), qos, 1, pub_props);
    if (res < 0) {
        LOG_E("aiot_mqtt pub failed, res: -0x%04X\n", -res);
        aiot_mqtt_deinit(&mqtt_handle);
        return -1;
    }
    aiot_mqtt_props_deinit(&pub_props);
}

Publishing and subscribing on the platform are both JSON-based; you can use the cJSON library to easily build and parse data. For example, building data to send:

c
char *get_payload(int led_state) {
    cJSON *root = cJSON_CreateObject();
    cJSON_AddNumberToObject(root, "id", 1);
    cJSON_AddStringToObject(root, "version", "1.0");
    cJSON *params = cJSON_CreateObject();
    cJSON_AddBoolToObject(params, "ledState", led_state);
    cJSON_AddItemReferenceToObject(root, "params", params);
    strcpy(payload, cJSON_PrintUnformatted(root));
    cJSON_Delete(root);
    cJSON_Delete(params);
    return payload;
}

The related code is already on Gitee: m61sdk: Ai-Thinker m61 module SDK porting project (gitee.com)

Have questions?

For other questions, please visit the unified discussion area: Ai-Thinker Discussions

Released under the MIT License. Build Time 2026-09-11 14:52:23