您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“MQTT如何連接RRPC通訊”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“MQTT如何連接RRPC通訊”吧!
什么是RRPC通信
MQTT協(xié)議是基于PUB/SUB的異步通信模式,不適用于服務(wù)端同步控制設(shè)備端返回結(jié)果的場景。物聯(lián)網(wǎng)平臺基于MQTT協(xié)議制定了一套請求和響應(yīng)的同步機(jī)制,無需改動MQTT協(xié)議即可實現(xiàn)同步通信。物聯(lián)網(wǎng)平臺提供API給服務(wù)端,設(shè)備端只需要按照固定的格式回復(fù)PUB消息,服務(wù)端使用API,即可同步獲取設(shè)備端的響應(yīng)結(jié)果。
RRPC:Revert-RPC。RPC(Remote Procedure Call)采用客戶機(jī)/服務(wù)器模式,用戶不需要了解底層技術(shù)協(xié)議,即可遠(yuǎn)程請求服務(wù)。RRPC則可以實現(xiàn)由服務(wù)端請求設(shè)備端并能夠使設(shè)備端響應(yīng)的功能。
RRPC 請求消息:云端下發(fā)給設(shè)備端的消息。
RRPC 響應(yīng)消息:設(shè)備端回復(fù)給云端的消息。
RRPC 消息ID:云端為每次RRPC調(diào)用生成的唯一消息ID。
RRPC 訂閱Topic:設(shè)備端訂閱RRPC消息時傳遞的Topic,含有通配符。
具體流程如下:
物聯(lián)網(wǎng)平臺收到來自用戶服務(wù)器的RRPC調(diào)用,下發(fā)一條RRPC請求消息給設(shè)備。消息體為用戶傳入的數(shù)據(jù),Topic為物聯(lián)網(wǎng)平臺定義的Topic,其中含有唯一的RRPC消息ID。
設(shè)備收到下行消息后,按照指定Topic格式(包含之前云端下發(fā)的唯一的RRPC消息ID)回復(fù)一條RRPC響應(yīng)消息給云端,云端提取出Topic中的消息ID,和之前的RRPC請求消息匹配上,然后回復(fù)給用戶服務(wù)器。
如果調(diào)用時設(shè)備不在線,云端會給用戶服務(wù)器返回設(shè)備離線的錯誤;如果設(shè)備沒有在超時時間內(nèi)(8秒內(nèi))回復(fù)RRPC響應(yīng)消息,云端會給用戶服務(wù)器返回超時錯誤。
RRPC通信相關(guān)Topic格式如下:
RRPC請求消息Topic:/sys/${YourProductKey}/${YourDeviceName}/rrpc/request/${messageId}
RRPC響應(yīng)消息Topic:/sys/${YourProductKey}/${YourDeviceName}/rrpc/response/${messageId}
RRPC訂閱Topic:/sys/${YourProductKey}/${YourDeviceName}/rrpc/request/+
以上內(nèi)容來自https://help.aliyun.com/document_detail/90567.html
由上面可以看出需要在topic中獲得messageId,然后在發(fā)送回去
如果你的ML302模塊還沒有連上阿里云可以看一下這個,中移4G模塊-ML302-OpenCpu開發(fā)-(MQTT連接阿里云)
https://blog.csdn.net/qq_33259323/article/details/108638945
https://www.bilibili.com/read/cv7876527
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; }
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); //接收到的數(shù)據(jù):topic_info->payload //干啥干啥干啥 // 發(fā)送 example_publish_rrpc(sendMessage); break; default: break; } }
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發(fā)送信息:%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; }
到此,相信大家對“MQTT如何連接RRPC通訊”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。