China Mobile 4G module-ML302-OpenCpu development-(MQTT connects to Alibaba Cloud-RRPC communication)

Station B: https://space.bilibili.com/309103931

China Mobile 4G module-ML302 column: https://blog.csdn.net/qq_33259323/category_10453372.html

China Mobile 4G Module-ML302 Collection: https://www.bilibili.com/read/readlist/rl328642

1. China Mobile 4G module-ML302-OpenCpu development-(firmware compilation and burning)

https://blog.csdn.net/qq_33259323/article/details/108586847

https://www.bilibili.com/read/cv7876504

2. China Mobile 4G module-ML302-OpenCpu development-(MQTT connects to Alibaba Cloud)

https://blog.csdn.net/qq_33259323/article/details/108638945

https://www.bilibili.com/read/cv7876527

2.1 China Mobile 4G module-ML302-OpenCpu development-(MQTT connection to Alibaba Cloud-subscribe topic)

https://blog.csdn.net/qq_33259323/article/details/108960540

https://www.bilibili.com/read/cv7879954

2.2 China Mobile 4G module-ML302-OpenCpu development-(MQTT connection to Alibaba Cloud-receiving and sending data)

https://blog.csdn.net/qq_33259323/article/details/108964810

https://www.bilibili.com/read/cv7886836

2.3 China Mobile 4G Module-ML302-OpenCpu Development-(MQTT connects to Alibaba Cloud-RRPC communication)

https://blog.csdn.net/qq_33259323/article/details/108965071

https://www.bilibili.com/read/cv7888259

3. China Mobile 4G module-ML302-OpenCpu development-serial port development

https://blog.csdn.net/qq_33259323/article/details/108974888

https://www.bilibili.com/read/cv7888865

4. China Mobile 4G module-ML302-OpenCpu development-51 MCU serial port to I2C

https://blog.csdn.net/qq_33259323/article/details/109020642

https://www.bilibili.com/read/cv7922942

5. China Mobile 4G module-ML302-OpenCpu development-MCP23017 input/output

https://blog.csdn.net/qq_33259323/article/details/109109136

https://www.bilibili.com/read/cv7969395

7. Mid-shift 4G module-ML302-OpenCpu development-PCF8591 measurement voltage

https://blog.csdn.net/qq_33259323/article/details/109109266

https://www.bilibili.com/read/cv7969365

8. China Mobile 4G module-ML302-OpenCpu development-GPIO

https://blog.csdn.net/qq_33259323/article/details/108960947

https://www.bilibili.com/read/cv7877192

9. China Mobile 4G Module-ML302-OpenCpu Development-ADC

https://blog.csdn.net/qq_33259323/article/details/109020864

https://www.bilibili.com/read/cv7922958

10. China Mobile 4G Module-ML302-OpenCpu Development-CJSON

https://blog.csdn.net/qq_33259323/article/details/109020898

https://www.bilibili.com/read/cv7923020

11. China Mobile 4G Module-ML302-OpenCpu Development-HTTP

https://blog.csdn.net/qq_33259323/article/details/109020904

https://www.bilibili.com/read/cv7923054

China Mobile 4G module-ML302-OpenCpu development-(MQTT connects to Alibaba Cloud-RRPC communication)

What is RRPC communication

The MQTT protocol is an asynchronous communication mode based on PUB/SUB, and is not suitable for scenarios where the server side synchronously controls the device side to return results. The Internet of Things platform has developed a set of request and response synchronization mechanism based on the MQTT protocol. Synchronous communication can be realized without changing the MQTT protocol. The IoT platform provides APIs to the server. The device only needs to reply to the PUB message in a fixed format. The server uses the API to synchronize the response results from the device.

Glossary

  • RRPC: Revert-RPC. RPC (Remote Procedure Call) adopts a client/server model, and users can request services remotely without knowing the underlying technical protocol. RRPC can realize the function of requesting the device from the server and enabling the device to respond.
  • RRPC request message: a message sent from the cloud to the device.
  • RRPC response message: the message that the device replies to the cloud.
  • RRPC message ID: The unique message ID generated by the cloud for each RRPC call.
  • RRPC Subscription Topic: The Topic passed when the device subscribes to RRPC messages contains wildcards.

RRPC principle

RRPC

The specific process is as follows:

  1. The IoT platform receives the RRPC call from the user server and sends an RRPC request message to the device. The message body is the data passed in by the user. Topic is the Topic defined by the IoT platform, which contains the unique RRPC message ID.
  2. After receiving the downlink message, the device replies an RRPC response message to the cloud according to the specified topic format (including the unique RRPC message ID issued by the cloud before). The cloud extracts the message ID in the topic and matches it with the previous RRPC request message. Then reply to the user server.
  3. If the device is not online when the call is made, the cloud will return an error that the device is offline to the user server; if the device does not reply to the RRPC response message within the timeout period (within 8 seconds), the cloud will return a timeout error to the user server.

Topics related to RRPC communication

Topic formats related to RRPC communication are as follows:

  • RRPC request message Topic: /sys/${YourProductKey}/${YourDeviceName}/rrpc/request/${messageId}
  • RRPC response message Topic: /sys/${YourProductKey}/${YourDeviceName}/rrpc/response/${messageId}
  • RRPC订阅Topic:/sys/${YourProductKey}/${YourDeviceName}/rrpc/request/+

The above content is from https://help.aliyun.com/document_detail/90567.html

It can be seen from the above that the messageId needs to be obtained in the topic, and then sent back

 

If your ML302 module is not connected to Alibaba Cloud, you can take a look at this. China Mobile 4G Module-ML302-OpenCpu Development-(MQTT connects to Alibaba Cloud)

https://blog.csdn.net/qq_33259323/article/details/108638945

https://www.bilibili.com/read/cv7876527

1. Subscribe to the RRPC example_subscribe_rrpc function

int example_subscribe_rrpc(void *handle){
    int res = 0;
    const char *fmt = "/sys/%s/%s/rrpc/request/+";
    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) {
        cm_printf("[ALIYUN]: memory not enough\n");
        return -1;
    }
    memset(topic, 0, topic_len);
    HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);

    cm_printf("topic:%s \r\n",topic);

    res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS0, example_message_arrive_rrpc, NULL);
    if (res < 0) {
        cm_printf("[ALIYUN]: subscribe failed\n");
        HAL_Free(topic);
        return -1;
    }

    HAL_Free(topic);
    return 0;
}

  2. Receive data example_message_arrive_rrpc function

char DEMO_RRPC_SessionId[19];

void example_message_arrive_rrpc(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg){
    iotx_mqtt_topic_info_t     *topic_info = (iotx_mqtt_topic_info_pt) msg->msg;
    char * sendMessage = NULL;
    const char * sendMessage_fmt = "%d";
    int sendMessage_len = 20;
    int cm_test_read_gpio = 0;

    cm_printf("example_message_arrive_rrpc \n");

    switch (msg->event_type) {
        case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
            /* print topic name and topic message */
            cm_printf("[ALIYUN]: Message Arrived:");
            cm_printf("Topic  : %.*s", topic_info->topic_len, topic_info->ptopic);
            cm_printf("Payload: %.*s", topic_info->payload_len, topic_info->payload);
            cm_printf("\n");

            // 提取sessionId
            strncpy(DEMO_RRPC_SessionId,topic_info->ptopic+46,19);
            cm_printf("[ALIYUN]: sessionId: %s",DEMO_RRPC_SessionId);


            //接收到的数据:topic_info->payload
            //干啥干啥干啥

            // 发送
            example_publish_rrpc(sendMessage);

            break;
        default:
            break;
    }
}

3. Send data example_publish_rrpc function

int example_publish_rrpc(unsigned char * payload)
{
    int             res = 0;
    const char     *fmt = "/sys/%s/%s/rrpc/response/%s";
    char           *topic = NULL;
    int             topic_len = 0;
    //char           *payload = "{\"message\":\"hello!\"}";]

    cm_printf("MQTT发送信息:%s",payload);

    topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1 +strlen(DEMO_RRPC_SessionId);
    topic = HAL_Malloc(topic_len);
    if (topic == NULL) {
        cm_printf("[ALIYUN]: memory not enough\n");  
        return -1;
    }
    memset(topic, 0, topic_len);
    HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME,DEMO_RRPC_SessionId);

    cm_printf("[ALIYUN]: example_publish_rrpc: %s",topic);


    res = IOT_MQTT_Publish_Simple(0, topic, IOTX_MQTT_QOS0, payload, strlen(payload));
    if (res < 0) {
        cm_printf("[ALIYUN]: publish failed, res = %d\n", res);
        HAL_Free(topic);
        return -1;
    }

    HAL_Free(topic);
    return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_33259323/article/details/108965071