Because of the async communication mode of the MQTT protocol based on the publish/subscribe pattern, after the server controls a device, it cannot synchronously get the result returned by the device. To solve this problem, IoT Hub uses the Revert RPC (RRPC) technology to implement a sync communication mechanism.
$rrpc/rxd/${productID}/${deviceName}/+
is used to subscribe to RRPC request messages sent by the cloud (downstream).$rrpc/rxd/${productID}/${deviceName}/${processID}
is used for the cloud to publish (downstream) RRPC request messages.$rrpc/txd/${productID}/${deviceName}/${processID}
is used to publish (upstream) RRPC response messages.Note:
${productID}
: product ID${deviceName}
: device name${processID}
: unique message ID generated by the server to identify different RRPC messages. The corresponding RRPC request message can be found through theprocessID
carried in the RRPC response message.
processID
distributed by the cloud in the request message topic, sets it as the processID
of the response message topic, and publishes a return message of the device to the response message topic.processID
and sends the return message to the server.Note:RRPC requests time out in 4s, that is, if the device doesn't respond within 4s, the request will be considered to have timed out.
The flowchart is as follows:
The sample completes device connection through the device-side C-SDK on Linux and calls APIs together with Tencent Cloud API Explorer. The specific steps are as follows:
Create an air conditioner product and an airConditioner1
device as instructed in Device Interconnection.
Modify CMakeLists.txt
and make sure that the following options exist:
set(BUILD_TYPE "release")
set(COMPILE_TOOLS "gcc")
set(PLATFORM "linux")
set(FEATURE_MQTT_COMM_ENABLED ON)
set(FEATURE_RRPC_ENABLED ON)
set(FEATURE_AUTH_MODE "KEY")
set(FEATURE_AUTH_WITH_NOTLS OFF)
set(FEATURE_DEBUG_DEV_INFO_USED OFF)
Run the following script for compilation:
./cmake_build.sh
The demo output rrpc_sample
is in the output/release/bin
folder.
Enter the information of the airConditioner1
device created above in the JSON file aircond_device_info1.json
.
{
"auth_mode":"KEY",
"productId":"KL4J2****8",
"deviceName":"airConditioner1",
"key_deviceinfo":{
"deviceSecret":"zOZXUaycuwleP****78dBA=="
}
}
rrpc_sample
demoYou can see that the airConditioner1
device has subscribed to the RRPC message and then entered the waiting status.
./rrpc_sample -c ./aircond_device_info1.json -l 1000
INF|2020-08-03 23:57:55|qcloud_iot_device.c|iot_device_info_set(50): SDK_Ver: 3.2.0, Product_ID: KL4J2****8, Device_Name: airConditioner1
DBG|2020-08-03 23:57:55|HAL_TLS_mbedtls.c|HAL_TLS_Connect(200): Setting up the SSL/TLS structure...
DBG|2020-08-03 23:57:55|HAL_TLS_mbedtls.c|HAL_TLS_Connect(242): Performing the SSL/TLS handshake...
DBG|2020-08-03 23:57:55|HAL_TLS_mbedtls.c|HAL_TLS_Connect(243): Connecting to /KL4J2****8.iotcloud.tencentdevices.com/8883...
INF|2020-08-03 23:57:55|HAL_TLS_mbedtls.c|HAL_TLS_Connect(265): connected with /KL4J2****8.iotcloud.tencentdevices.com/8883...
INF|2020-08-03 23:57:56|mqtt_client.c|IOT_MQTT_Construct(113): mqtt connect with id: 2**** success
INF|2020-08-03 23:57:56|rrpc_sample.c|main(206): Cloud Device Construct Success
DBG|2020-08-03 23:57:56|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(142): topicName=$rrpc/rxd/KL4J2****8/airConditioner1/+|packet_id=****
INF|2020-08-03 23:57:56|rrpc_sample.c|_mqtt_event_handler(49): subscribe success, packet-id=*****
DBG|2020-08-03 23:57:56|rrpc_client.c|_rrpc_event_callback(104): rrpc topic subscribe success
PublishRRPCMessage
to send an RRPC request messageGo to API Explorer, enter the personal key and device parameter information, select Online Call, and send the request.
Observe the printout of the airConditioner1
device, and you can see that the RRPC request message has been received, and the processID
is ***.
DBG|2020-08-04 00:07:36|rrpc_client.c|_rrpc_message_cb(85): topic=$rrpc/rxd/KL4J2****8/airConditioner1/***
INF|2020-08-04 00:07:36|rrpc_client.c|_rrpc_message_cb(86): len=6, topic_msg=closed
INF|2020-08-04 00:07:36|rrpc_client.c|_rrpc_get_process_id(76): len=3, process id=***
INF|2020-08-04 00:07:36|rrpc_sample.c|_rrpc_message_handler(137): rrpc message=closed
Observe the printout of the airConditioner1
device, and you can see that the RRPC request message has been processed, the RRPC response message has been replied, and the processID
is ***.
DBG|2020-08-04 00:07:36|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$rrpc/txd/KL4J2****8/airConditioner1/***|payload=ok
Observe the response result of the server, and you can see that the RRPC response message has been received. MessageId
is ***, and Payload
is **** after being Base64-encoded
, which is the same as the actual response message of the client after being Base64-encoded
. At this point, it can be confirmed that the response message has been received.
Was this page helpful?