当前位置:网站首页>05. Tencent cloud IOT device side learning -- mqtt protocol client implementation
05. Tencent cloud IOT device side learning -- mqtt protocol client implementation
2022-06-24 16:40:00 【fancyxu】
The main purpose of this series is to record the learning notes of Tencent cloud Internet of things device , And on the device side SDK Make a supplementary explanation .
Source Overview
For the relevant source code files, see https://github.com/tencentyun/qcloud-iot-explorer-sdk-embedded-c/tree/master/sdk_src/protocol/mqtt
mqtt The protocol client mainly implements the construction and sending of each control packet and the processing of the server reply , All downlink data ( except connack), adopt qcloud_iot_mqtt_yield Receive and process , subscribe 、 Unsubscribing and publishing are implemented by the user through providing an external interface .
The normal process is ( Under multithreading Yiled Usually a separate thread is used for , Please see the _mqtt_yield_thread):
Implementation details
- CONNECT:CONNECT The control package is built in _serialize_connect_packet in , It is mainly set according to the authentication method usrname and password, as well as clean session.
- CONNACK:CONNECT After the control packet is sent, it will pass wait_for_read wait for CONNACK, And then call _deserialize_connack_packet Judge the result
- SUBSCRIBE:SUBSCRIBE The control package is built on _serialize_subscribe_packet. For subscriptions , Would call push_sub_info_to Join the subscription queue list_sub_wait_ack in , And then in qcloud_iot_mqtt_yield Call in qcloud_iot_mqtt_sub_info_proc Determine the timeout of the subscription in the queue or receive SUBACK After treatment . One of the key things is SubTopicHandle Of on_message_handler( Handle the downlink message of the subscription topic ) and on_sub_event_handler( Handle events such as timeout ).
- SUBACK:SUBACK Will pass qcloud_iot_mqtt_yield Receive and process , Mainly judge whether the reply is normal according to the agreement
- UNSUBSCRIBE and UNSUBACK: and SUBSCRIBE Similar treatment , Also add to list_sub_wait_ack in , However, it is rarely used in actual scenarios , Generally, the device subscription relationship is determined at the time of design , There are few scenarios in which you need to cancel your subscription .
- PUBLISH( client -> The server )&PUBACK:PUBLISH The control package is built on _serialize_publish_packet, Usually we only specify QoS Grade and payload. about QoS1 The news of , Would call _mask_push_pubInfo_to Add to list_pub_wait_ack in , And then in qcloud_iot_mqtt_yield Call in qcloud_iot_mqtt_pub_info_proc The timeout judgment published in the queue or the receipt PUBACK To deal with . One of the key things is Qcloud_IoT_Client Medium event_handle Will tell you that the release timed out , So as to realize... In the application layer QoS1 Retransmission of . Practical application , Even using QoS1 There is no guarantee that the message will arrive , Because in most cases, you can't receive PUBACK All because the equipment is disconnected from the network , At this time, the application layer design strategy is needed to realize , For example, store messages locally .
- PUBLISH( The server -> client )&PUBACK: Sent by the server PUBLISH The news will be here qcloud_iot_mqtt_yield Call in _handle_publish_packet Handle , according to QoS reply PUBACK, Then call back the message to SubTopicHandle Of on_message_handler Message processing .
- PINGREQ&PING: Keep alive news in qcloud_iot_mqtt_yield call _mqtt_keep_alive Send , And then through _handle_pingresp_packet Process the reply .
- DISCONNECT: This message is usually not required , You can refer to qcloud_iot_mqtt_disconnect
Some of the parameters
On the client side MQTT There will be some parameters related to the actual application in the implementation , It needs to be adjusted to meet the needs of the business , As listed below :
- QCLOUD_IOT_MQTT_MAX_REMAIN_WAIT_MS : Maximum waiting time for header reception , Under the weak network, it is necessary to set a large
- QCLOUD_IOT_MQTT_COMMAND_TIMEOUT:MQTT Timeout for blocking calls , Including connection, etc , Large can be set under the weak network
- QCLOUD_IOT_MQTT_KEEP_ALIVE_INTERNAL: Keep alive by default , Generally speaking, through API Input parameters can be modified , This is just a demonstration
- QCLOUD_IOT_MQTT_TX_BUF_LEN and QCLOUD_IOT_MQTT_RX_BUF_LEN: Receive and send buffer sizes , Set according to business requirements , No more than 16K
- MAX_RECONNECT_WAIT_INTERVAL: Maximum wait time for reconnection , If a quick reconnection is required, the value should be reduced
- MQTT_RMDUP_MSG_ENABLED and MQTT_MAX_REPEAT_BUF_LEN: These two parameters are mainly used for message filtering , Because the platform is based on QOS1 Retransmission will be realized , However, due to the delay of messages in the link , So we need to filter the messages we have received , Avoid repeated reception processing . It is generally recommended to open ,BUF The length is set according to the actual application , The oldest messages are overwritten ID The strategy of
边栏推荐
- Introduction to website development for zero foundation Xiaobai
- Funny! Pictures and texts give you a comprehensive understanding of the effects of dynamics and mass
- [play with Tencent cloud] my operation strategy from domain name application to website filing in Tencent cloud
- During JMeter pressure measurement, time_ The number of requests does not go up due to many waits. The problem is solved
- Video intelligent analysis platform easycvr derivative video management platform menu bar small screen adaptive optimization
- Percona Toolkit series - Pt deadlock logger
- Scuffle on China's low code development platform -- make it clear that low code
- MySQL Advanced Series: locks - locks in InnoDB
- Bitwise Operators
- A set of very good H3C and Tianrongxin Internet cutover scheme templates, with word document download
猜你喜欢

Cognition and difference of service number, subscription number, applet and enterprise number (enterprise wechat)

A survey of training on graphs: taxonomy, methods, and Applications
MySQL Advanced Series: Locks - Locks in InnoDB

Some adventurer hybrid versions with potential safety hazards will be recalled

A survey on dynamic neural networks for natural language processing, University of California

There are potential safety hazards Land Rover recalls some hybrid vehicles
Advanced programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization

Ui- first lesson

Ps\ai and other design software pondering notes

C. K-th not divisible by n (Mathematics + thinking) codeforces round 640 (Div. 4)
随机推荐
06. Tencent cloud IOT device side learning - Introduction to basic functions
How to access tke cluster API interface with certificate or token
Mathematics in machine learning -- point estimation (IV): maximum posteriori probability (map)
What is the difference between a network card and a port
Annual contribution! Tencent cloud middleware product upgrade conference is in hot registration!
Script design for automatic login and command return
Activeindex selection and redirection in the menu bar on the right of easycvs
Pytorch transpose convolution
Nonholonomic constrained robot
Customized Tile Map cut - based on Tencent map
What is Ethernet
Prometheus deployment
There are potential safety hazards Land Rover recalls some hybrid vehicles
Leetcode notes of Google boss | necessary for school recruitment!
Istio FAQ: sidecar stop sequence
Regular expression learning artifact!
TRTC web end imitation Tencent conference microphone mute detection
How to pop up an alarm through the national standard gb28181 protocol video platform easygbs for mobile detection / perimeter intrusion detection video recording
[tke] whether to configure SNAT when the container accesses services outside the node
Enterprise service growth path (7): what key factors will affect SaaS' sales performance?