[Anxin Ke's first 4G module CA-01 is directly connected to Alibaba Internet of things platform ③] connected by one type and one secret authentication

I preface

Under the one type one secret security authentication mode, all devices under the same product can burn the same firmware (that is, burn ProductKey and ProductSecret). When the device sends an activation request, the IOT platform confirms the identity, passes the authentication, and issues the DeviceSecret corresponding to the device.

2, Activate data network

**AT+CGATT?**
+CGATT: 1  
OK
**AT+SAPBR=3,1,"CONTYPE","GPRS"** 
OK
**AT+SAPBR=3,1,"APN","CMIOT"** 
OK
**AT+SAPBR=1,1** 
OK

AT+CGATT? Query whether the GPRS data network is attached. The following three AT commands can be executed only after +CGATT: 1 is returned.

3, Connect to alicloud authentication server and dynamically register devices

The authentication scheme adopts one type and one secret. When Alibaba cloud creates a product, it must turn on the dynamic registration switch, and then add devices. When the terminal dynamically registers devices, it will use the three tuples of ProductKey, ProductSecret and DeviceName: the following examples are used. The values of the three tuples are:
ProductKey = b1KCi45LcCP
ProductSecret = VWll9fiYWKiwraBk (this ProductSecret is a product key, not a device key, please note the difference) DeviceName = 862991419835241
Where at+httpdata=12020000
Note that this 120 is the data length (in bytes) under DOWNLOAD. In actual use, it should be modified according to its own data length. Remember!!!
DOWNLOAD
productKey=b1KCi45LcCP&deviceName=862991419835241&random=123456&sign=1770d3 8260cc980dbc4337ed7fe1e8de&signMethod=HmacMD5
Note that it is best to copy this string of data by yourself, and then modify the specific values. Do not enter all the data manually, otherwise it is easy to cause problems. Remember!!!
This AT command uses the triple value sign as the hash value calculated by the HmacMD5 algorithm,
Use web address: http://encode.chahuo.com
Plaintext:
deviceName862991419835241productKeyb1KCi45LcCPrandom123456
The key is:
VWll9fiYWKiwraBk
The calculated hash value is:
1770d38260cc980dbc4337ed7fe1e8de
Random is a random number with variable length

AT interaction process is as follows:

**AT+HTTPINIT OKAT+HTTPPARA="URL","https://iot-auth.cn-shanghai.aliyuncs.com/auth/register/device"** 
OK
**AT+HTTPPARA="USER_DEFINED","Content-Type: application/x-www-form-urlencoded"** 
OK

Note that content type: is followed by a space!!!

**AT+HTTPDATA=120,20000** 

Note that this 120 is the data length (in bytes) under DOWNLOAD. In actual use, it should be modified according to its own data length. Remember!!!
DOWNLOAD

**productKey=b1KCi45LcCP&deviceName=862991419835241&random=123456&sign=1770d3 8260cc980dbc4337ed7fe1e8de&signMethod=HmacMD5** 
OK

Note that it is best to copy this string of data by yourself, and then modify the specific values. Do not enter all the data manually, otherwise it is easy to cause problems. Remember!!!

**AT+HTTPACTION=1**
OK
 +HTTPACTION: 1,200,149 
  **AT+HTTPREAD**
+HTTPREAD: 149
{"code":200,"data":{"deviceName":"862991419835241","deviceSecret":"mRMyB50qafv74A 5FofvhxZ2h9iTL9wX7","productKey":"b1KCi45LcCP"},"message":"success"} 
 OK
 **AT+HTTPTERM** 
 OK

deviceSecret in the data returned by HTTPREAD is the device key, which will be used in step 4

4, Connect to the alicloud authentication server and obtain the user name and password of the access server

The authentication scheme adopts one type and one secret. After the device is dynamically registered in the third step, the cloud platform will return the DeviceSecret of the device. In this step, connecting to the authentication server requires a triple: {productkey, devicename, DeviceSecret}. The following examples are used. The values of the triple are:
ProductKey = b1KCi45LcCP
DeviceName = 862991419835241
DeviceSecret = mRMyB50qafv74A 5FofvhxZ2h9iTL9wX7 (this DeviceSecret is the device key returned in the third step HTTPREAD, not the device key in the triplet)
Where at+httpdata=11220000 note that this 112 is the data length under DOWNLOAD (in bytes). In actual use, it should be modified according to its own data length. Remember!!!
DOWNLOAD
productKey=b1KCi45LcCP&sign=70e7bb641d7566ad26a305dd20e8e261&clientId=86299141983 5241&deviceName=862991419835241
Note that it is best to copy this string of data, and then modify the specific values. Do not manually enter all the values, otherwise it is easy to cause problems. For example, once a customer manually entered the I in the Id of clientId as l, resulting in an error, remember!!!
This AT command will use the triple value, where sign is the hash value calculated by the HmacMD5 algorithm
Plaintext:
clientId862991419835241deviceName862991419835241productKeyb1KCi45LcCP
The key is:
mRMyB50qafv74A5FofvhxZ2h9iTL9wX7
The calculated hash value is:
70e7bb641d7566ad26a305dd20e8e261
AT interaction process is as follows:

**AT+HTTPINIT**
OK
**AT+HTTPPARA="URL","https://iot-auth.cn-shanghai.aliyuncs.com/auth/devicename"** 
OK
**AT+HTTPPARA="USER_DEFINED","Content-Type: application/x-www-form-urlencoded"** 
//Note that content type: is followed by a space!!! 
OK
**AT+HTTPDATA=112,20000** 

Note that this 112 is the data length (in bytes) under DOWNLOAD. In actual use, it should be modified according to its own data length. Remember!!!
DOWNLOAD

**productKey=b1KCi45LcCP&sign=70e7bb641d7566ad26a305dd20e8e261&clientId=862991419835241&deviceName=862991419835241**
OK

Note that it is best to copy this string of data, and then modify the specific values. Do not manually enter all the values, otherwise it is easy to cause problems. For example, once a customer manually entered the I in the Id of clientId as l, resulting in an error, remember!!!

**AT+HTTPACTION=1** 
OK
 +HTTPACTION: 1,200,128 AT+HTTPREAD +HTTPREAD: 128 {"code":200,"data":{"iotId":"PbUqGh2URAj1AtNyyd7X00107ceb00","iotToken":"425656f625b645 60a4bff2a0f16946a3"},"message":"success"}
 OK
 **AT+HTTPTERM**
OK

The data returned by HTTPREAD includes iotId and iotToken. The value of iotId is the user name of MQTT client in step 5, and the value of iotToken is the password of MQTT client in step 5

5, Connect to alicloud access server

**AT+MCONFIG="862991419835241","PbUqGh2URAj1AtNyyd7X00107ceb00","425656f625b64560 a4bff2a0f16946a3"**
 OK
 **AT+SSLMIPSTART="b1KCi45LcCP.iot-as-mqtt.cn-shanghai.aliyuncs.com",1883** 
 OK 
 CONNECT OK 

Establish a session. Send instructions to establish a session immediately after receiving CONNECT OK. Otherwise, you will be kicked. Be careful!!!

**AT+MCONNECT=1,300** 
OK 
CONNACK OK

You have successfully connected to alicloud here

Vi Send and receive application data

Subscribe to topics

**AT+MSUB="/b1KCi45LcCP/862991419835241/user/get",0** 
OK 
SUBACK 

Set the printing method when data is received

**AT+MQTTMSGSET=0** 
OK

After the topic subscription is successful, the device can receive data from the cloud

Publish message
Publish Qos0 message

**AT+MPUB="/ b1KCi45LcCP/862991419835241/user/update",0,0,"HelloWorld"** 
OK

Publish Qos1 message

**AT+MPUB="/ b1KCi45LcCP/862991419835241/user/update",1,0,"HelloWorld"** 
OK
PUBACK 

Qos1 messages cannot be sent until PUBACK returns

  • Encourage each other! If you have any questions, please leave a message or email: support@aithinker.com

Posted by MartinGr on Tue, 31 May 2022 17:47:45 +0530