MQTT protocol - use CONNECT message to connect to Alibaba Cloud

Use the network debugging assistant to send a CONNECT message to connect to Alibaba Cloud

Reference: https://blog.csdn.net/daniaoxp/article/details/103039296

In the previous article, I introduced how to assemble the CONNECT message and how to calculate the remaining length

CONNECT message: https://blog.csdn.net/weixin_46251230/article/details/129392102

Calculate the remaining length: https://blog.csdn.net/weixin_46251230/article/details/129394172

In this experiment, the network debugging assistant sends a CONNECT message to connect to the Alibaba Cloud platform

1. Create an Alibaba Cloud device and obtain MQTT connection parameters

Create Aliyun device: https://blog.csdn.net/weixin_46251230/article/details/128993864

insert image description here

2. Convert MQTT connection parameters

Convert clientId, username, and passwd to hexadecimal, and you can use the network debugging assistant to convert

Select UDP as the protocol, select the address of your own host, port 8888, and select your own host address on the remote host, select ASCII mode, paste the parameters in this mode, first paste the clientId, and remove the extra spaces

insert image description here

Then click HEX, the ASCII code in the input box will be automatically converted to hexadecimal, copy these hexadecimals to the text document

insert image description here

Click Reset Count in the lower right corner, then click Send, you can see that 80 characters have been sent

insert image description here

Convert 80 to hexadecimal 0x50 and put it in front of the hexadecimal data of the text document. It must conform to UTF-8 encoding, so add 00 in front

insert image description here

Similarly, convert both username and passwd to hexadecimal format

insert image description here

3. Assemble the CONNETC message

The payload is to combine the hexadecimal data of clientId, username, and passwd

insert image description here

Fixed header + variable header, the remaining length can be replaced with ??, and left blank

insert image description here

Combine data in order of fixed header + variable header + payload

insert image description here

4. Calculate the remaining length

Copy all the characters after ?? to the network debugging assistant, reset the count, click send, and you can see that a total of 180 bytes have been sent

insert image description here

According to the steps of calculating the remaining length, 180 is greater than 128 and less than 16383, so two bytes are used to represent

insert image description here

According to the formula 180=128*a+b, it can be calculated that a = 1, b = 52, a is in the upper 8 bits, b is in the lower 8 bits, the lower bits come first, and the higher bits follow, so the writing order is ba

b = 0011 0100 = 1011 0100 (the highest bit indicates that there are bytes after the remaining length, so it must be actively set to 1)

b = B4

a = 0000 0001 = 01

Paste B4 01 into combined data

insert image description here

5. Connect to Alibaba Cloud platform

Aliyun server address (East China 2): *.iot-as-mqtt.cn-shanghai.aliyuncs.com

*Replace with the ProductKey of your own device

Such as: i6deo513xT1.iot-as-mqtt.cn-shanghai.aliyuncs.com

The port number is 1883

insert image description here

Change the network debugging assistant to TCP Client, fill in the remote host address as i6deo513xT1.iot-as-mqtt.cn-shanghai.aliyuncs.com, port number 1883, click HEX, copy and paste the above combined CONNECT message into the input box , click Connect, then click Send

insert image description here

When receiving, remember to set it as HEX reception. If the last byte in the information returned by Alibaba Cloud is 00, it means that the connection is successful. If it is not 00, the connection fails. The subscript is the possible value of the last byte, only 0x00 Yes means the connection is accepted

insert image description here

At this point, you can see that the devices on the Alibaba Cloud platform have been displayed online

insert image description here

Guess you like

Origin blog.csdn.net/weixin_46251230/article/details/129394306
Recommended