中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-RRPC通讯)

原创
2020/10/19 23:16
阅读数 775

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

中移4G模块-ML302专栏:https://blog.csdn.net/qq_33259323/category_10453372.html

中移4G模块-ML302文集:https://www.bilibili.com/read/readlist/rl328642

1.中移4G模块-ML302-OpenCpu开发-(固件编译和烧录)

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

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

2.中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云)

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

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

2.1中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-订阅主题)

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

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

2.2中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-接收和发送数据)

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

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

2.3中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-RRPC通讯)

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

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

3.中移4G模块-ML302-OpenCpu开发-串口开发

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

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

4.中移4G模块-ML302-OpenCpu开发-51单片机串口转I2C

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

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

5.中移4G模块-ML302-OpenCpu开发-MCP23017输入/输出

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

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

7.中移4G模块-ML302-OpenCpu开发-PCF8591测量电压

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

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

8.中移4G模块-ML302-OpenCpu开发-GPIO

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

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

9.中移4G模块-ML302-OpenCpu开发-ADC

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

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

10.中移4G模块-ML302-OpenCpu开发-CJSON

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

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

11.中移4G模块-ML302-OpenCpu开发-HTTP

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

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

中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-RRPC通讯)

什么是RRPC通信

MQTT协议是基于PUB/SUB的异步通信模式,不适用于服务端同步控制设备端返回结果的场景。物联网平台基于MQTT协议制定了一套请求和响应的同步机制,无需改动MQTT协议即可实现同步通信。物联网平台提供API给服务端,设备端只需要按照固定的格式回复PUB消息,服务端使用API,即可同步获取设备端的响应结果。

名词解释

  • RRPC:Revert-RPC。RPC(Remote Procedure Call)采用客户机/服务器模式,用户不需要了解底层技术协议,即可远程请求服务。RRPC则可以实现由服务端请求设备端并能够使设备端响应的功能。
  • RRPC 请求消息:云端下发给设备端的消息。
  • RRPC 响应消息:设备端回复给云端的消息。
  • RRPC 消息ID:云端为每次RRPC调用生成的唯一消息ID。
  • RRPC 订阅Topic:设备端订阅RRPC消息时传递的Topic,含有通配符。

RRPC原理

RRPC

具体流程如下:

  1. 物联网平台收到来自用户服务器的RRPC调用,下发一条RRPC请求消息给设备。消息体为用户传入的数据,Topic为物联网平台定义的Topic,其中含有唯一的RRPC消息ID。
  2. 设备收到下行消息后,按照指定Topic格式(包含之前云端下发的唯一的RRPC消息ID)回复一条RRPC响应消息给云端,云端提取出Topic中的消息ID,和之前的RRPC请求消息匹配上,然后回复给用户服务器。
  3. 如果调用时设备不在线,云端会给用户服务器返回设备离线的错误;如果设备没有在超时时间内(8秒内)回复RRPC响应消息,云端会给用户服务器返回超时错误。

RRPC通信相关Topic

RRPC通信相关Topic格式如下:

  • RRPC请求消息Topic:/sys/${YourProductKey}/${YourDeviceName}/rrpc/request/${messageId}
  • RRPC响应消息Topic:/sys/${YourProductKey}/${YourDeviceName}/rrpc/response/${messageId}
  • RRPC订阅Topic:/sys/${YourProductKey}/${YourDeviceName}/rrpc/request/+

以上内容来自https://help.aliyun.com/document_detail/90567.html

由上面可以看出需要在topic中获得messageId,然后在发送回去

 

如果你的ML302模块还没有连上阿里云可以看一下这个,中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云)

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

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

1.订阅RRPC example_subscribe_rrpc函数

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.接收数据 example_message_arrive_rrpc函数

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.发送数据 example_publish_rrpc函数

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;
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部