RT thread studio Usage Note: connect to alicloud through 4G module

summary

This article takes you to connect to Alibaba cloud platform based on the 4G module in the previous article

Portal:

RT thread studio notes (VI): use of 4G module SIM7600

https://blog.csdn.net/tigerots/article/details/108132814

reference material:

https://help.aliyun.com/document_detail/89301.html?spm=a2c4g.11186623.6.706.bec8429dZDvMvG

 

Alibaba cloud creates products

Alibaba cloud Internet of things platform address: https://iot.console.aliyun.com/studio?spm=a2c56.12526802.1304866.1.743b107bMaky1X

 

About properties and events

 

Ali has special chapters on device attributes and event descriptions. We generally use Alink json format

The data in Alink format is as follows:

Uplink (Alink JSON):

  • Topic for device request: / sys/{productKey}/{deviceName}/thing/event/property/post

Request Topic used by this routine

"/sys/a1ZkpqGqexK/DTU_4G_002/thing/event/property/post"

  • The payload data used for equipment request shall include several important options such as method, ID and parameter

 {
      "method":  "thing.event.property.post",
      "id":  "20",
      "params": {
             "RGBColor": {
                     "Red": 20,
                     "Blue": 21,
                     "Green": 22
               }
      },
      "version": "1.0.0"
 }

 

  • Cloud response device Topic: / sys/{productKey}/{deviceName}/thing/event/property/post_reply

/sys/a1ZkpqGqexK/DTU_4G_002//thing/event/property/post_reply

  • The data content of the cloud response device, code 200 indicates success, and the ID number is consistent with the request ID number

{
     "code": 200,
      "data": {
      },
      "id": "45",
      "message": "success",
      "method": "thing.event.property.post",
      "version": "1.0"
 }

Be sure to understand the topics your device should subscribe to or publish, otherwise you can't write embedded programs.

 

RT thread configuration

1. Enable Alibaba cloud's mqtt software package. () if you need to have an in-depth understanding of the mqtt protocol, I suggest you watch Chaowei's video, bare metal analysis, and speak very thoroughly), and configure it according to your products on Alibaba cloud.

The above information comes from Alibaba cloud device management, as shown in the following figure:

2. After configuration, open the routine to configure the subscription and the topic to be published according to Alibaba's topic rules

uint32_t id_cnt = 0;
// Upload and modify attribute values
static int example_publish(void *handle)
{
    int             res = 0;
    const char     *fmt = "/sys/%s/%s/thing/event/property/post";
    char           *topic = NULL;
    int             topic_len = 0;

    char           *fmt2 = "{%s,%s,%s,%s}";
    char           *method = "\"method\": \"thing.event.property.post\"";// method
    char            id[16] = "";// Unique ID
    char            params[128] = "";// parameter
    char           *version = "\"version\":\"1.0.0\"";// Version, fixed

    char           *payload = NULL;
    int             payload_len = 0;


    // Parameters used to generate payload
    id_cnt ++;
    HAL_Snprintf(id, 16, "\"id\": \"%d\"", id_cnt);
    HAL_Snprintf(params, 128, "\"params\":{\"CurrentHumidity\":%d,\"VehSpeed\":%d,\"Atmosphere\":%d,\"CurrentTemperature\":%d}",
                (id_cnt%100), (id_cnt%1150), (id_cnt%10+990), (id_cnt%10+20));

    // Generate payload
    payload_len = strlen(fmt2) + strlen(method) + strlen(id) + strlen(params) + strlen(version) + 1;
    payload = HAL_Malloc(payload_len);
    if (payload == NULL) {
        EXAMPLE_TRACE("memory not enough");
        return -1;
    }
    memset(payload, 0, payload_len);
    HAL_Snprintf(payload, payload_len, fmt2, method, id, params, version);


    // Generate topic
    topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
    topic = HAL_Malloc(topic_len);
    if (topic == NULL) {
        EXAMPLE_TRACE("memory not enough");
        return -1;
    }
    memset(topic, 0, topic_len);
    HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);

    // Publish theme
    res = IOT_MQTT_Publish_Simple(0, topic, IOTX_MQTT_QOS0, payload, strlen(payload));
    if (res < 0) {
        EXAMPLE_TRACE("publish failed, res = %d", res);
        HAL_Free(topic);
        return -1;
    }

    HAL_Free(topic);
    return 0;
}
static int example_subscribe(void *handle)
{
    int res = 0;
    const char *fmt = "/%s/%s/user/get";
    char *topic = NULL;
    int topic_len = 0;

    topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
    topic = HAL_Malloc(topic_len);
    if (topic == NULL) {
        EXAMPLE_TRACE("memory not enough");
        return -1;
    }
    memset(topic, 0, topic_len);
    HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);

    res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS0, example_message_arrive, NULL);
    if (res < 0) {
        EXAMPLE_TRACE("subscribe failed");
        HAL_Free(topic);
        return -1;
    }

    HAL_Free(topic);
    return 0;
}

 

After compiling and running, the interrupt always prompts to save, and the exception occurs because (based on the previous blog)< RT thread studio notes (VI): use of 4G module SIM7600 >)The length of the host address array in is not enough. The Alibaba cloud address data parsed here is relatively long. The default is 32 bytes, which will lead to memory overflow

 

After modification, you can successfully connect to Alibaba cloud through the console, and publish and subscribe to topics.

 

Sorry, everyone, this blog has a lot of omissions and is not detailed enough. Originally, I planned to write code and blog at the same time, but because there are many development tasks of intermediary companies, I didn't complete the blog in time. Now I want to make up, but I don't have a board on hand, so I can't intercept the pictures of relevant operations. If you need to communicate with a little partner on this issue, please write to me privately.

Tags: Embedded system IoT rt-thread

Posted by asterinex on Tue, 10 May 2022 04:24:35 +0300