Universal ESP8266 connected to Alibaba Cloud IoT platform

I'm doing a graduation project recently. After all, I have been an undergraduate for 4 years, as a computer embedded student, and I have been studying in the embedded laboratory for three or four years. I don't want to fool myself, so I'm ready to make a good one. It is a general-purpose ESP8266, so I started working on Alibaba Cloud. After two days, I finally finished debugging. I will share the steps briefly. I hope it will help students who are new to Alibaba Cloud IoT platform. The premise is that you need to understand the basic AT of ESP8266. Instructions, understand the MQTT protocol, and the basic knowledge of Alibaba Cloud, otherwise you will be confused.

1. Register with Alibaba Cloud to create a device and obtain the device triplet.

1. You can register for Alibaba Cloud, DingTalk, and Alipay. After real-name authentication, you can enter the Internet of Things platform (find it yourself, and the location may change if you continue to upgrade)

 2. Click to enter the public instance

3. Create a new product (all defaults, ie: direct connection device, wifi, ICA)

4. Select the created product and click Device Management on the right

 

5. After entering, click Add Device, and create your own device. This is D001 (the current display is offline because I have already connected it, and it should be inactive just after creating it) and then click on the right to view the device properties

 

6. Click View on the device information interface to view and obtain the triplet of the device

This triple is important information for connecting to Alibaba Cloud, one-click copy for backup

2. Prepare to connect to Alibaba Cloud and generate packets

Only the connection message is demonstrated here, the message conversion is not complicated, just need to be careful not to make mistakes, even if there is a little error, it will not be connected.

1. Copy to Notepad for backup

 

2. First make clear the IP address of the device we created on Alibaba Cloud, which is the address we will connect to in a while (mine is East China 2)

*.iot-as-mqtt.cn-shanghai.aliyuncs.com Port number: 1883 "*" is the ProductKey value of our device

i.e. my device address is:

                        

3. To generate a connection message of the MQTT protocol, the first is a fixed header plus a variable header

Fixed header + variable header as follows: 10 ?? 00 04 4D 51 54 54 04 C2 00 64

4. Next is the message payload

load 1 >>>(DEVICENAME)|securemode=3,signmethod=hmacsha1|

Load 2 >>>(DEVICENAME)&(PRODUCTKEY) 

负载3>>>ClientId(DEVICENAME)deviceName(DEVICENAME)productKey(PRODUCTKEY)

Encryption key (DeviceSecret)

Replace the parentheses with their contents with our triples, being careful not to delete any letter by mistake.

4.1 Do hash encryption for 3: Enter payload 3 >>> and DeviceSecret and click HmacSHA1 to encrypt, and get a string of encrypted data

4.2 After load 1, load 2 is replaced and load 3 is replaced and encrypted

 

4.3 Convert the 3 strings of data obtained above

Convert to hexadecimal code, and add the length of the message after conversion to hexadecimal to get the new payload 1, 2, 3

The two bytes in the blue box are the corresponding length (in hexadecimal).

4.4 Get the final connection message

Add the newly obtained payload packets 1, 2, and 3 to the fixed and variable packets, and replace the second byte (??) of the packet with the total length of the packet (from ?? to the next byte). to the length of the last byte), my total length is 110 converted to hexadecimal as 6E

 

At this point, the message is completed, and you must be careful. If you miss a byte, you will not be able to connect.

Third, open the network assistant, connect the ESP8266 with the serial port module, and prepare for debugging

The ESP8266 purchased from Taobao for 5 pieces only needs to be connected to the five pins of TX, RX, VCC, GND, and EN. Connect it yourself here.

1. After the connection is completed, prepare for debugging, and load the AT command once on the right side

 

 

details as follows:

AT+RST

AT+CWMODE=1

AT+CWAUTOCONN=0

 AT+CWJAP="wifi name", "wifi password" Modify according to your own wifi

AT+CIPMODE=1

AT+CIPMUX=0

AT+CIPSTART="TCP","(ProductKey).iot-as-mqtt.cn-shanghai.aliyuncs.com",1883 

                         Note: Replace (ProductKey) with the corresponding value of your own device

AT+CIPSEND enters the transparent transmission mode, don't forget it, don't be in a hurry, if you forget it, you will fart.

10 6E 00 04 4D 51 54 54 04 C2**********************..... After entering the transparent transmission, it starts to send the connection message, pay attention to this It must be sent in HEX form.

As long as it does not return Close, it means success. Since it is currently displayed in the form of a string, you cannot see the reply 20 02 00 00 

Now change the display mode to HEX mode 

Send ping packet C0 00

Replying to D0 00 means that the connection is very OK.

Summarize

The mqtt protocol is not difficult, and it can be completed by knowing the message format. Be careful not to make mistakes in the process of manually generating the message. Even if there is one more space, it will be kicked out by Alibaba Cloud immediately. Be careful.

Most of the information found on the Internet is to use cellular connection, the device selects a 2G module, and connects to the cloud platform in this way. I specially tested it, using WIFI, not selecting the module, the same can be connected. (If you do not Knowing this can be ignored).

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/caojia12345678/article/details/121342147