簡介
> 使用AT方式連接阿里云分為一機一密和一型一密兩種方式,其中一機一密又包括HTTP認證二次連接和MQTT直連兩種方式
>
> - 關聯文檔和使用工具:
> - [AT固件獲取]
> - [在線加/解密工具]
> - [阿里云平臺]
準備工作
1. Air780EP_全IO開發板一套,包括天線SIM卡,USB線,并燒錄AT固件
2. PC電腦,串口工具
3. 準備設備證書及產品證書
**注意:下方證書信息僅為本教程使用,用戶需要使用自己的證書信息**
```lua
--設備證書
{
"ProductKey": "k1hipglLdjU",
"DeviceName": "868488076506128",
"DeviceSecret": "fef32adf6dad46c2c7cbf335d3a7b021"
}
--產品證書
{
"ProductSecret": "8LQwOYSZMwp3DFBd",
"ProductKey": "k1hipglLdjU"
}
```
阿里云操作
產品操作
打開[阿里云平臺]找到**物聯網平臺**,開通業務后進入**控制臺**。
點開**設備管理**的**產品頁面**,點擊**新建產品**。根據需求和圖示說明創建產品。
設備操作
創建產品完成后就可以進入設備頁面添加設備,在對應產品頁面進入設備管理,按照提示添加設備
**(在做正式產品時建議使用imei為devicename,方便后期維護)**
[阿里云設備創建](https://help.aliyun.com/document_detail/73729.html?spm=a2c4g.11186623.6.573.55977b7bAjX04B)
# 連接阿里云
## 一機一密AT HTTP鑒權連接
- 首先需要使用HmacMD5計算哈希值用于簽名,使用網址:[在線加/解密工具](http://encode.chahuo.com/)
```
明文: clientId868488076506128deviceName868488076506128productKeyk1hipglLdjU
秘鑰: fef32adf6dad46c2c7cbf335d3a7b021
結果: 0f655b12adf519b9a7a2b0a990f398b6
```
- 拼接DOWNLOAD參數
```
productKey=k1hipglLdjU&sign=0f655b12adf519b9a7a2b0a990f398b6&clientId=868488076506128&deviceName=868488076506128
```
- AT交互流程如下所示
1. 激活網絡
```
[03:09:17.105]發→◇AT+CGATT?
□
[03:09:17.106]收←◆AT+CGATT?
+CGATT: 1
OK
[03:09:24.575]發→◇AT+SAPBR=3,1,"CONTYPE","GPRS"
□
[03:09:24.579]收←◆AT+SAPBR=3,1,"CONTYPE","GPRS"
OK
[03:09:32.363]發→◇AT+SAPBR=3,1,"APN","CMIOT"
□
[03:09:32.365]收←◆AT+SAPBR=3,1,"APN","CMIOT"
OK
[03:09:38.899]發→◇AT+SAPBR=1,1
□
[03:09:38.901]收←◆AT+SAPBR=1,1
OK
```
2. 初始化HTTP
```
[03:09:43.901]發→◇AT+HTTPINIT
□
[03:09:43.903]收←◆AT+HTTPINIT
OK
```
3. 寫入網址URL
```
[03:09:50.783]發→◇AT+HTTPPARA="URL",https://iot-auth.cn-shanghai.aliyuncs.com/auth/devicename
□
[03:09:50.786]收←◆AT+HTTPPARA="URL",https://iot-auth.cn-shanghai.aliyuncs.com/auth/devicename
OK
```
4. 設置本次請求的hearder,**注意"Content-type: " 后面有一個空格**
```
[03:09:57.667]發→◇AT+HTTPPARA="USER_DEFINED","Content-type: application/x-www-form-urlencoded"
□
[03:09:57.671]收←◆AT+HTTPPARA="USER_DEFINED","Content-type: application/x-www-form-urlencoded"
OK
```
5. 配置寫入DOWNLOAD參數指令,**注意這個112是DOWNLOAD參數的數據長度(以字節為單位),實際使用時,要根據自己的數據長度進行修改,切記?。?! 待出現DOWNLOAD后發送通過前文計算拼接出來的DOWNLOAD參數**
```
[03:10:05.246]發→◇AT+HTTPDATA=112,20000
□
[03:10:05.249]收←◆AT+HTTPDATA=112,20000
DOWNLOAD
```
6. 寫入DOWNLOAD參數,**注意最好自己復制這串數據,然后修改具體的值,不要全部手動輸入,否則很容易出問題,切記?。?!**
```
[03:10:10.735]發→◇productKey=k1hipglLdjU&sign=0f655b12adf519b9a7a2b0a990f398b6&clientId=868488076506128&deviceName=868488076506128
□
[03:10:10.740]收←◆
OK
```
7. HTTPACTION的信息上報,正常的話會回復狀態碼200
```
[03:10:16.378]發→◇AT+HTTPACTION=1
□
[03:10:16.381]收←◆AT+HTTPACTION=1
OK
```
8. 收到信息上報正常回復狀態碼200,收到返回數據長度為124
```
[03:10:18.025]收←◆
+HTTPACTION: 1,200,124
```
9. 讀取請求結果,**注意,HTTPREAD返回的數據中有iotId和iotToken,其中iotId的值為MQTT client的用戶名,iotToken的值為MQTT client的密碼**
```
[03:10:51.867]發→◇AT+HTTPREAD
□
[03:10:51.870]收←◆AT+HTTPREAD
+HTTPREAD: 124
{"code":200,"data":{"iotId":"2BGmgWFvoXlbi1Er9sk4k1hip0","iotToken":"137eb0759ca248b79383a60c0b0e0f80"},"message":"success"}
OK
```
10. 配置連接阿里云,**注意,第一個值為clientId,第二個值為iotId,第三個值為iotToken**
```
[03:13:32.942]發→◇AT+MCONFIG="868488076506128","2BGmgWFvoXlbi1Er9sk4k1hip0","137eb0759ca248b79383a60c0b0e0f80"
□
[03:13:32.945]收←◆AT+MCONFIG="868488076506128","2BGmgWFvoXlbi1Er9sk4k1hip0","137eb0759ca248b79383a60c0b0e0f80"
OK
```
11. 建立連接,**注意,收到CONNECT OK后立刻發送指令建立會話,不然會被踢,一定注意?。。?*
```lua
[03:13:51.493]發→◇AT+SSLMIPSTART="a1qsHBbKtmc.iot-as-mqtt.cn-shanghai.aliyuncs.com",1883
□
[03:13:51.496]收←◆AT+SSLMIPSTART="a1qsHBbKtmc.iot-as-mqtt.cn-shanghai.aliyuncs.com",1883
OK
[03:13:52.911]收←◆
CONNECT OK
[03:13:53.182]發→◇AT+MCONNECT=1,120
□
[03:13:53.185]收←◆AT+MCONNECT=1,120
OK
[03:13:53.434]收←◆
CONNACK OK
```
- 連接成功
## 一機一密AT直連MQTT
- 當設備屬于公共實例,使用一型一密預注冊認證方式時,動態注冊參數如下:
```lua
mqttClientId: clientId+"|securemode=3,signmethod=hmacsha1,timestamp=132323232|"
mqttUsername: deviceName+"&"+productKey
mqttPassword: sign_hmac(deviceSecret,content)
```
| 參數 | 說明 |
| :----------- | :----------------------------------------------------------- |
| clientId | 客戶端ID:長度在64個字符內,建議使用IMEI或SN碼方便區分不同客戶端 |
| securemode | 安全模式: 2 (TLS直連模式),3 (TCP直連模式) |
| signmethod | 表示簽名算法類型。支持hmacmd5,hmacsha1,hmacsha256。默認為hmacmd5 |
| timestamp | 表示當前時間毫秒值,可以不傳遞 |
| mqttPassword | 前文計算的簽名值 |
- AT交互流程如下所示
1. 配置MQTT參數
```
[03:51:26.001]發→◇AT+MCONFIG="868488076506128|securemode=3,signmethod=hmacmd5|","868488076506128&k1hipglLdjU","0f655b12adf519b9a7a2b0a990f398b6"
□
[03:51:26.004]收←◆AT+MCONFIG="868488076506128|securemode=3,signmethod=hmacmd5|","868488076506128&k1hipglLdjU","0f655b12adf519b9a7a2b0a990f398b6"
OK
```
2. 建立連接,**注意,第一個參數需要根據實際項目productKey修改**
```
[03:51:43.546]發→◇AT+SSLMIPSTART="k1hipglLdjU.iot-as-mqtt.cn-shanghai.aliyuncs.com",1883
□
[03:51:43.549]收←◆AT+SSLMIPSTART="k1hipglLdjU.iot-as-mqtt.cn-shanghai.aliyuncs.com",1883
OK
```
3. 建立會話,**注意,收到 CONNECT OK 后立刻發送指令建立會話,不然會被踢,一定注意?。?!**
```
[03:51:45.836]收←◆
CONNECT OK
[03:51:48.961]發→◇AT+MCONNECT=1,120
□
[03:51:48.964]收←◆AT+MCONNECT=1,120
OK
[03:51:49.179]收←◆
CONNACK OK
```
## 一型一密AT方式連接
- 一型一密與一機一密的主要區別在于第一次連接的時候需要根據協議請求秘鑰,首先需要在阿里云打開動態注冊開關,如下圖所示
- 使用到的參數如下所示
```lua
{
"ProductSecret": "8LQwOYSZMwp3DFBd",
"ProductKey": "k1hipglLdjU"
"DeviceName" "868488076506128"
}
明文:deviceName868488076506128productKeyk1hipglLdjUrandom123456
秘鑰:8LQwOYSZMwp3DFBd
HmacMD5計算結果:20558baadb2a0b04847767980f0ee67b
DOWNLOAD參數
--random 為隨機數,長度不定
productKey=k1hipglLdjU&deviceName=868488076506128&random=123456&sign=20558baadb2a0b04847767980f0ee67b&signMethod=HmacMD5
```
- 當設備屬于公共實例,使用一型一密預注冊認證方式時,動態注冊參數如下:
```lua
mqttClientId: clientId+"|securemode=2,authType=xxxx,random=xxxx,signmethod=xxxx|"
mqttUserName: deviceName+"&"+productKey
mqttPassword: sign_hmac(productSecret,content)
```
- AT交互流程如下所示
1. 激活網絡,**注意,先發送AT+CGATT?查詢是否附著上GPRS數據網絡,返回+CGATT: 1后,才能執行下面的三個 AT指令。**
```
[04:07:13.901]發→◇AT+CGATT?
□
[04:07:13.902]收←◆AT+CGATT?
+CGATT: 1
OK
[04:07:23.900]發→◇AT+SAPBR=3,1,"CONTYPE","GPRS"
□
[04:07:23.902]收←◆AT+SAPBR=3,1,"CONTYPE","GPRS"
OK
[04:07:31.245]發→◇AT+SAPBR=3,1,"APN","CMIOT"
□
[04:07:31.248]收←◆AT+SAPBR=3,1,"APN","CMIOT"
OK
[04:07:38.659]發→◇AT+SAPBR=1,1
□
[04:07:38.661]收←◆AT+SAPBR=1,1
OK
```
2. 初始化HTTP
```
[04:08:35.329]發→◇AT+HTTPINIT
□
[04:08:35.332]收←◆AT+HTTPINIT
OK
```
3. 寫入網址URL
```
[04:08:44.056]發→◇AT+HTTPPARA="URL","https://iot-auth.cn-shanghai.aliyuncs.com/auth/register/device"
□
[04:08:44.061]收←◆AT+HTTPPARA="URL","https://iot-auth.cn-shanghai.aliyuncs.com/auth/register/device"
OK
```
4. 設置本次請求的hearder,**注意"Content-type: " 后面有一個空格**
```
[04:08:54.671]發→◇AT+HTTPPARA="USER_DEFINED","Content-Type: application/x-www-form-urlencoded"
□
[04:08:54.674]收←◆AT+HTTPPARA="USER_DEFINED","Content-Type: application/x-www-form-urlencoded"
OK
```
5. 配置寫入DOWNLOAD參數指令,**注意這個120是DOWNLOAD參數的數據長度(以字節為單位),實際使用時,要根據自己的數據長度進行修改,切記?。。?待出現DOWNLOAD后發送通過前文計算拼接出來的DOWNLOAD參數**
```
[04:09:11.437]發→◇AT+HTTPDATA=120,20000
□
[04:09:11.439]收←◆AT+HTTPDATA=120,20000
DOWNLOAD
```
6. 寫入DOWNLOAD參數,**注意最好自己復制這串數據,然后修改具體的值,不要全部手動輸入,否則很容易出問題,切記!!!**
```
[04:09:16.944]發→◇productKey=k1hipglLdjU&deviceName=868488076506128&random=123456&sign=20558baadb2a0b04847767980f0ee67b&signMethod=HmacMD5
□
[04:09:16.947]收←◆
OK
```
7. HTTPACTION的信息上報,正常的話會回復狀態碼200
```
[04:09:26.653]發→◇AT+HTTPACTION=1
□
[04:09:26.656]收←◆AT+HTTPACTION=1
OK
```
8. 收到信息上報正?;貜蜖顟B碼200,收到返回數據長度為149
```
[04:09:28.828]收←◆
+HTTPACTION: 1,200,149
```
9. 讀取請求結果
```
[04:09:43.709]發→◇AT+HTTPREAD
□
[04:09:43.711]收←◆AT+HTTPREAD
+HTTPREAD: 149
{"code":200,"data":{"deviceName":"868488076506128","deviceSecret":"fef32adf6dad46c2c7cbf335d3a7b021","productKey":"k1hipglLdjU"},"message":"success"}
OK
```
# 發布與訂閱消息
## 發布消息
- 發布Qos0消息
```
[04:58:06.798]發→◇AT+MPUB="/k1hipglLdjU/868488076506128/user/update",0,0,"HelloWorld"
□
[04:58:06.802]收←◆AT+MPUB="/k1hipglLdjU/868488076506128/user/update",0,0,"HelloWorld"
OK
```
- 發布Qos1消息,**注意,Qos1 消息必須等到PUBACK返回,才能發下一條消息**
```
[04:58:43.408]發→◇AT+MPUB="/k1hipglLdjU/868488076506128/user/update",1,0,"HelloWorld"
□
[04:58:43.412]收←◆AT+MPUB="/k1hipglLdjU/868488076506128/user/update",1,0,"HelloWorld"
OK
[04:58:43.709]收←◆
PUBACK
```
- 通過**物聯網平臺-監控運維-日志服務**可以直接查看日志信息
## 訂閱消息
- 訂閱主題
```
[05:01:58.568]發→◇AT+MSUB="/k1hipglLdjU/868488076506128/user/get",0
□
[05:01:58.572]收←◆AT+MSUB="/k1hipglLdjU/868488076506128/user/get",0
OK
[05:01:58.887]收←◆
SUBACK
```
- 設置收到數據時的打印方式
配置為0時主動上報到串口,上報的 URC 為`+MSUB:,,`
配置為1時為緩存模式。有新訂閱消息時,上報的URC為 `+MSUB:`
```
[05:02:05.102]發→◇AT+MQTTMSGSET=0
□
[05:02:05.105]收←◆AT+MQTTMSGSET=0
OK
```
- 通過阿里云平臺發布消息
-
開發板
+關注
關注
25文章
4771瀏覽量
96178 -
阿里云
+關注
關注
3文章
922瀏覽量
42780 -
MQTT
+關注
關注
5文章
629瀏覽量
22286 -
合宙通信
+關注
關注
0文章
146瀏覽量
1658
發布評論請先 登錄
相關推薦
評論