MQTT protocol - publish message (server sends to client)
Publish message composition: https://blog.csdn.net/weixin_46251230/article/details/129414158
After understanding the PUBLISH message that publishes the information, you can analyze the message data sent by the Alibaba Cloud server to the local client
Before the experiment, you need to create products and equipment on Alibaba Cloud, and create a simple temperature and humidity object model: https://blog.csdn.net/weixin_46251230/article/details/128996719
1. Use the CONNECT message to connect to Alibaba Cloud
Check if the device is online
2. Subscribe to the subject of the object model
Select a Topic class with subscription permissions
After replacing ${deviceName} with your own device name, convert it to hexadecimal, and calculate the byte length. At the end of the byte, you need to add the service quality level Qos. Here, set it to 00. Don’t forget
Add fixed headers and variable headers to combine into subscription topic messages
Send a message and receive a response from Alibaba Cloud, indicating that the subscription is successful
3. Alibaba Cloud object model sends data
Select online debugging in the Alibaba Cloud platform, then set the temperature and humidity values, and finally click Settings, the server will send the data to the client
In the network debugging assistant, you can see the message sent by Alibaba Cloud, and copy the message to a text file for analysis
4. Analyze the message
The first byte of the message is 30, which conforms to the fixed header byte of the published message message, the next byte is AB, and the highest bit is 1, so there is another byte 01 to indicate the remaining length, the low bit is in front, and the high bit is in After that, the length can be calculated to be 171, so the fixed header can be obtained as 30 AB 01
The first two bytes of the variable header indicate the length of the header, so 00 34 indicates the length, and 0x34 is converted to 52 in decimal, so 52 bytes are counted from 34 as the variable header
Convert these 52 bytes to ASCII to get the Topic class of Alibaba Cloud Thing model subscription permission: /sys/i6deo513xT1/Device01/thing/service/property/set
It is consistent with the subscription topic message sent above, indicating that the analysis is correct
The remaining bytes are all the payload. The first two bytes of this part do not represent the length, but real data . Convert all of them to ASCII code to get the data in JSON format. You can see that the temperature value is 20, The humidity value is 66, which is the same as the one set on the Aliyun platform at the beginning, indicating that the data sent by the server to the client is correct.