?
Air780EP是合宙推出的一款[低功耗4G全網(wǎng)通模組],兼容模組行業(yè)1618經(jīng)典封裝,支持OpenCPU開(kāi)發(fā)及全功能數(shù)傳AT開(kāi)發(fā),可廣泛應(yīng)用于多樣化的物聯(lián)網(wǎng)終端。
針對(duì)客戶(hù)朋友需求反饋,本期特別推出基于Air780EP模組AT開(kāi)發(fā)的阿里云應(yīng)用指南。
**一、相關(guān)準(zhǔn)備工作 **
1.1 硬件準(zhǔn)備
1.2 軟件準(zhǔn)備
--設(shè)備證書(shū)
{
"ProductKey": "k1hipglLdjU",
"DeviceName": "868488076506128",
"DeviceSecret": "fef32adf6dad46c2c7cbf335d3a7b021"
}
--產(chǎn)品證書(shū)
{
"ProductSecret": "8LQwOYSZMwp3DFBd",
"ProductKey": "k1hipglLdjU"
}
特別提醒:
上方證書(shū)信息僅為本教程使用,用戶(hù)需要使用自己的證書(shū)信息。
**二、阿里云操作 **
打開(kāi)阿里云平臺(tái)(https://www.aliyun.com/)找到物聯(lián)網(wǎng)平臺(tái),開(kāi)通業(yè)務(wù)后進(jìn)入控制臺(tái)。
2.1 產(chǎn)品操作
點(diǎn)開(kāi)設(shè)備管理的產(chǎn)品頁(yè)面,點(diǎn)擊 新建產(chǎn)品, 根據(jù)需求和圖示說(shuō)明創(chuàng)建產(chǎn)品。
?
2.2 設(shè)備操作
創(chuàng)建產(chǎn)品完成后就可以進(jìn)入設(shè)備頁(yè)面添加設(shè)備,在對(duì)應(yīng)產(chǎn)品頁(yè)面進(jìn)入設(shè)備管理,按照提示添加設(shè)備。在做正式產(chǎn)品時(shí)建議使用imei為devicename,方便后期維護(hù)。
阿里云設(shè)備創(chuàng)建:
https://help.aliyun.com/document_detail/73729.html?spm=a2c4g.11186623.6.573.55977b7bAjX04B
?
**三、連接阿里云 **
使用AT方式連接阿里云分為:一機(jī)一密和一型一密兩種方式,其中一機(jī)一密又包括HTTP鑒權(quán)連接和MQTT直連。
下文將根據(jù)不同連接方式分別講解:
3.1 一機(jī)一密AT HTTP鑒權(quán)連接
3.1.1 首先需要使用HmacMD5計(jì)算哈希值用于簽名
在線加/解密工具:
http://encode.chahuo.com/
明文:
clientId868488076506128deviceName868488076506128prod
uctKeyk1hipglLdjU
秘鑰:
fef32adf6dad46c2c7cbf335d3a7b021
結(jié)果:
0f655b12adf519b9a7a2b0a990f398b6
3.1.2 拼接DOWNLOAD參數(shù)
productKey=k1hipglLdjU&sign=0f655b12adf519b9a7a2b0a9
90f398b6&clientId=868488076506128&deviceName=8684880
76506128
3.1.3 AT交互流程如下所示:
01)激活網(wǎng)絡(luò)
[03:09:17.105]發(fā)→◇AT+CGATT?
[03:09:17.106]收←◆AT+CGATT?
+CGATT: 1
OK
[03:09:24.575]發(fā)→◇AT+SAPBR=3,1,"CONTYPE","GPRS"
[03:09:24.579]收←◆AT+SAPBR=3,1,"CONTYPE","GPRS"
OK
[03:09:32.363]發(fā)→◇AT+SAPBR=3,1,"APN","CMIOT"
[03:09:32.365]收←◆AT+SAPBR=3,1,"APN","CMIOT"
OK
[03:09:38.899]發(fā)→◇AT+SAPBR=1,1
[03:09:38.901]收←◆AT+SAPBR=1,1
OK
02)初始化HTTP
[03:09:43.901]發(fā)→◇AT+HTTPINIT
[03:09:43.903]收←◆AT+HTTPINIT
OK
03)寫(xiě)入網(wǎng)址URL
[03:09:50.783]發(fā)→◇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
04)設(shè)置本次請(qǐng)求的hearder
注意:"Content-type: " 后面有一個(gè)空格。
[03:09:57.667]發(fā)→◇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
05)配置寫(xiě)入DOWNLOAD參數(shù)指令
注意: 這個(gè)112是DOWNLOAD參數(shù)的數(shù)據(jù)長(zhǎng)度(以字節(jié)為單位),實(shí)際使用時(shí),**要根據(jù)自己的數(shù)據(jù)長(zhǎng)度進(jìn)行修改,切記!??! **
待出現(xiàn)DOWNLOAD后發(fā)送通過(guò)前文計(jì)算拼接出來(lái)的DOWNLOAD參數(shù)。
[03:10:05.246]發(fā)→◇AT+HTTPDATA=112,20000
[03:10:05.249]收←◆AT+HTTPDATA=112,20000
DOWNLOAD
06)寫(xiě)入DOWNLOAD參數(shù)
注意:最好自己復(fù)制這串?dāng)?shù)據(jù),然后修改具體的值, 不要全部手動(dòng)輸入, 否則很容易出問(wèn)題,切記?。。?/p>
[03:10:10.735]發(fā)→◇productKey=k1hipglLdjU&sign=0f655b12adf519b9a7a2b
0a990f398b6&clientId=868488076506128&deviceName=8684
88076506128
[03:10:10.740]收←◆
OK
07)HTTPACTION的信息上報(bào)
[03:10:16.378]發(fā)→◇AT+HTTPACTION=1
[03:10:16.381]收←◆AT+HTTPACTION=1
OK
08)收到信息上報(bào)
正?;貜?fù)狀態(tài)碼200,收到返回?cái)?shù)據(jù)長(zhǎng)度為124:
[03:10:18.025]收←◆
+HTTPACTION: 1,200,124
09)讀取請(qǐng)求結(jié)果
注意:HTTPREAD返回的數(shù)據(jù)中有iotId和iotToken,其中iotId的值為MQTT client的用戶(hù)名,iotToken的值為MQTT client的密碼。
[03:10:51.867]發(fā)→◇AT+HTTPREAD
[03:10:51.870]收←◆AT+HTTPREAD
+HTTPREAD: 124
{"code":200,"data":{"iotId":"2BGmgWFvoXlbi1Er9sk4k1hip0","iotToken":"13 7eb0759ca248b79383a60c0b0e0f80"}, "message":"success"}
OK
10)配置連接阿里云
注意:第一個(gè)值為clientId,第二個(gè)值為iotId,第三個(gè)值為iotToken。
[03:13:32.942]發(fā) →◇AT+MCONFIG="868488076506128","2BGmgWFvoXlbi1Er9sk 4k1hip0","137eb0759ca248b79383a60c0b0e0f80"
[03:13:32.945]收 ←◆AT+MCONFIG="868488076506128","2BGmgWFvoXlbi1Er9sk 4k1hip0","137eb0759ca248b79383a60c0b0e0f80"
OK
11)建立連接
注意:收到CONNECT OK后立刻發(fā)送指令建立會(huì)話,不然會(huì)被踢,一定注意?。?!
[03:13:51.493]發(fā)→◇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]發(fā)→◇AT+MCONNECT=1,120
[03:13:53.185]收←◆AT+MCONNECT=1,120
OK
[03:13:53.434]收←◆
CONNACK OK
3.1.4 連接成功
?
3.2 一機(jī)一密AT直連MQTT
3.2.1 注冊(cè)參數(shù)
當(dāng)設(shè)備屬于公共實(shí)例,使用一型一密預(yù)注冊(cè)認(rèn)證方式時(shí),動(dòng)態(tài)注冊(cè)參數(shù)如下:
mqttClientId: clientId+"|securemode=3,signmethod=hmacsha1,timestam p=132323232|"
mqttUsername:
deviceName+"&"+productKey
mqttPassword:
sign_hmac(deviceSecret,content)
?
3.2.2 AT交互流程如下所示:
01)配置MQTT參數(shù)
[03:51:26.001]發(fā)
→◇AT+MCONFIG="868488076506128|securemode=3,signmeth
od=hmacmd5|","868488076506128&k1hipglLdjU","0f655b12
adf519b9a7a2b0a990f398b6"
[03:51:26.004]收
←◆AT+MCONFIG="868488076506128|securemode=3,signmeth
od=hmacmd5|","868488076506128&k1hipglLdjU","0f655b12
adf519b9a7a2b0a990f398b6"
OK
02)建立連接
注意:第一個(gè)參數(shù)需要根據(jù)實(shí)際項(xiàng)目productKey修改。
[03:51:43.546]發(fā)→◇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
03)建立會(huì)話
注意:收到 CONNECT OK 后立刻發(fā)送指令建立會(huì)話,不然會(huì)被踢,一定注意?。。?/strong>
[03:51:45.836]收←◆
CONNECT OK
[03:51:48.961]發(fā)→◇AT+MCONNECT=1,120
[03:51:48.964]收←◆AT+MCONNECT=1,120
OK
[03:51:49.179]收←◆
CONNACK OK
3.3 一型一密AT方式連接
一型一密與一機(jī)一密的主要區(qū)別在于,第一次連接的時(shí)候需要根據(jù)協(xié)議請(qǐng)求秘鑰。
3.3.1 首先需要在阿里云打開(kāi)動(dòng)態(tài)注冊(cè)開(kāi)關(guān):
?
3.3.2 使用到的參數(shù)如下所示:
{
"ProductSecret": "8LQwOYSZMwp3DFBd",
"ProductKey": "k1hipglLdjU"
"DeviceName" "868488076506128"
}
明文:deviceName868488076506128productKeyk1hipglLdjUrandom
123456
秘鑰:
8LQwOYSZMwp3DFBdHmacMD5
計(jì)算結(jié)果
:20558baadb2a0b04847767980f0ee67b
DOWNLOAD參數(shù)
--random 為隨機(jī)數(shù),長(zhǎng)度不定
productKey=k1hipglLdjU&deviceName=868488076506128&ra
ndom=123456&sign=20558baadb2a0b04847767980f0ee67b&si
gnMethod=HmacMD5
3.3.3 注冊(cè)參數(shù)
當(dāng)設(shè)備屬于公共實(shí)例,使用一型一密預(yù)注冊(cè)認(rèn)證方式時(shí),動(dòng)態(tài)注冊(cè)參數(shù)如下:
mqttClientId:
clientId+"|securemode=2,authType=xxxx,random=xxxx,si
gnmethod=xxxx|"
mqttUserName:
deviceName+"&"+productKey
mqttPassword:
sign_hmac(productSecret,content)
3.3.4 AT交互流程如下所示:
01)激活網(wǎng)絡(luò)
注意:先發(fā)送AT+CGATT?查詢(xún)是否附著上GPRS數(shù)據(jù)網(wǎng)絡(luò),返回+CGATT: 1后,才能執(zhí)行下面的三個(gè)AT指令。
[04:07:13.901]發(fā)→◇AT+CGATT?
[04:07:13.902]收←◆AT+CGATT?
+CGATT: 1
OK
[04:07:23.900]發(fā)→◇AT+SAPBR=3,1,"CONTYPE","GPRS"
[04:07:23.902]收←◆AT+SAPBR=3,1,"CONTYPE","GPRS"
OK
[04:07:31.245]發(fā)→◇AT+SAPBR=3,1,"APN","CMIOT"
[04:07:31.248]收←◆AT+SAPBR=3,1,"APN","CMIOT"
OK
[04:07:38.659]發(fā)→◇AT+SAPBR=1,1
[04:07:38.661]收←◆AT+SAPBR=1,1
OK
02)初始化HTTP
[04:08:35.329]發(fā)→◇AT+HTTPINIT
[04:08:35.332]收←◆AT+HTTPINIT
OK
03)寫(xiě)入網(wǎng)址URL
[04:08:44.056]發(fā)→◇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
04)設(shè)置本次請(qǐng)求的hearder
注意:"Content-type: " 后面有一個(gè)空格。
[04:08:54.671]發(fā)
→◇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
05)配置寫(xiě)入DOWNLOAD參數(shù)指令
注意:這個(gè)120是DOWNLOAD參數(shù)的數(shù)據(jù)長(zhǎng)度(以字節(jié)為單位),實(shí)際使用時(shí),要根據(jù)自己的數(shù)據(jù)長(zhǎng)度進(jìn)行修改,切記?。。〈霈F(xiàn)DOWNLOAD后發(fā)送通過(guò)前文計(jì)算拼接出來(lái)的DOWNLOAD參數(shù)。
[04:09:11.437]發(fā)→◇AT+HTTPDATA=120,20000
[04:09:11.439]收←◆AT+HTTPDATA=120,20000
DOWNLOAD
06)寫(xiě)入DOWNLOAD參數(shù)
注意:最好自己復(fù)制這串?dāng)?shù)據(jù),然后修改具體的值, 不要全部手動(dòng)輸入, 否則很容易出問(wèn)題,切記!??!
[04:09:16.944]發(fā)
→◇productKey=k1hipglLdjU&deviceName=868488076506128
&random=123456&sign=20558baadb2a0b04847767980f0ee67b
&signMethod=HmacMD5
[04:09:16.947]收←◆
OK
07)HTTPACTION的信息上報(bào)
[04:09:26.653]發(fā)→◇AT+HTTPACTION=1
[04:09:26.656]收←◆AT+HTTPACTION=1
OK
08)收到信息上報(bào)
正常回復(fù)狀態(tài)碼200,收到返回?cái)?shù)據(jù)長(zhǎng)度為149:
[04:09:28.828]收←◆
+HTTPACTION: 1,200,149
09)讀取請(qǐng)求結(jié)果
[04:09:43.709]發(fā)→◇AT+HTTPREAD
[04:09:43.711]收←◆AT+HTTPREAD
+HTTPREAD: 149
{"code":200,"data":
{"deviceName":"868488076506128","deviceSecret":"fef3
2adf6dad46c2c7cbf335d3a7b021","productKey":"k1hipglL
djU"},"message":"success"
}
OK
**四、發(fā)布與訂閱消息 **
4.1 發(fā)布消息
4.1.1 發(fā)布Qos0消息:
[04:58:06.798]發(fā)
→◇AT+MPUB="/k1hipglLdjU/868488076506128/user/update
",0,0,"HelloWorld"
[04:58:06.802]收
←◆AT+MPUB="/k1hipglLdjU/868488076506128/user/update
",0,0,"HelloWorld"
OK
4.1.2 發(fā)布Qos1消息:
注意:Qos1消息必須等到PUBACK返回,才能發(fā)下一條消息。
[04:58:43.408]發(fā)
→◇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
4.1.3 查看日志信息:
通過(guò)物聯(lián)網(wǎng)平臺(tái)-監(jiān)控運(yùn)維-日志服務(wù),可以直接查看日志信息。
?
4.2 訂閱消息
4.2.1 訂閱主題:
[05:01:58.568]發(fā)
→◇AT+MSUB="/k1hipglLdjU/868488076506128/user/get",0
[05:01:58.572]收
←◆AT+MSUB="/k1hipglLdjU/868488076506128/user/get",0
OK
[05:01:58.887]收←◆
SUBACK
4.2.2 設(shè)置收到數(shù)據(jù)時(shí)的打印方式:
配置為0時(shí)主動(dòng)上報(bào)到串口,上報(bào)的URC為+MSUB:,, 配置為1時(shí)為緩存模式。
有新訂閱消息時(shí),上報(bào)的URC為 +MSUB:。
[05:02:05.102]發(fā)→◇AT+MQTTMSGSET=0
[05:02:05.105]收←◆AT+MQTTMSGSET=0
OK
4.2.3 通過(guò)阿里云平臺(tái)發(fā)布消息:
?
審核編輯 黃宇
?
-
4G
+關(guān)注
關(guān)注
15文章
5491瀏覽量
118702 -
低功耗
+關(guān)注
關(guān)注
10文章
2349瀏覽量
103530 -
阿里云
+關(guān)注
關(guān)注
3文章
932瀏覽量
42919
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論