赞
踩
使用AT方式连接阿里云分为一机一密和一型一密两种方式,其中一机一密又包括HTTP认证二次连接和MQTT直连两种方式
PC电脑,串口工具
准备设备证书及产品证书
注意:下方证书信息仅为本教程使用,用户需要使用自己的证书信息
--设备证书
{
"ProductKey": "k1hipglLdjU",
"DeviceName": "868488076506128",
"DeviceSecret": "fef32adf6dad46c2c7cbf335d3a7b021"
}
--产品证书
{
"ProductSecret": "8LQwOYSZMwp3DFBd",
"ProductKey": "k1hipglLdjU"
}
打开阿里云平台找到物联网平台,开通业务后进入控制台。
点开设备管理的产品页面,点击新建产品。根据需求和图示说明创建产品。
创建产品完成后就可以进入设备页面添加设备,在对应产品页面进入设备管理,按照提示添加设备
(在做正式产品时建议使用imei为devicename,方便后期维护)
明文: clientId868488076506128deviceName868488076506128productKeyk1hipglLdjU
秘钥: fef32adf6dad46c2c7cbf335d3a7b021
结果: 0f655b12adf519b9a7a2b0a990f398b6
productKey=k1hipglLdjU&sign=0f655b12adf519b9a7a2b0a990f398b6&clientId=868488076506128&deviceName=868488076506128
[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
[03:09:43.901]发→◇AT+HTTPINIT
□
[03:09:43.903]收←◆AT+HTTPINIT
OK
[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
[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
[03:10:05.246]发→◇AT+HTTPDATA=112,20000
□
[03:10:05.249]收←◆AT+HTTPDATA=112,20000
DOWNLOAD
[03:10:10.735]发→◇productKey=k1hipglLdjU&sign=0f655b12adf519b9a7a2b0a990f398b6&clientId=868488076506128&deviceName=868488076506128
□
[03:10:10.740]收←◆
OK
[03:10:16.378]发→◇AT+HTTPACTION=1
□
[03:10:16.381]收←◆AT+HTTPACTION=1
OK
[03:10:18.025]收←◆
+HTTPACTION: 1,200,124
[03:10:51.867]发→◇AT+HTTPREAD
□
[03:10:51.870]收←◆AT+HTTPREAD
+HTTPREAD: 124
{"code":200,"data":{"iotId":"2BGmgWFvoXlbi1Er9sk4k1hip0","iotToken":"137eb0759ca248b79383a60c0b0e0f80"},"message":"success"}
OK
[03:13:32.942]发→◇AT+MCONFIG="868488076506128","2BGmgWFvoXlbi1Er9sk4k1hip0","137eb0759ca248b79383a60c0b0e0f80"
□
[03:13:32.945]收←◆AT+MCONFIG="868488076506128","2BGmgWFvoXlbi1Er9sk4k1hip0","137eb0759ca248b79383a60c0b0e0f80"
OK
[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
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 | 前文计算的签名值 |
[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
[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
[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
{
"ProductSecret": "8LQwOYSZMwp3DFBd",
"ProductKey": "k1hipglLdjU"
"DeviceName" "868488076506128"
}
明文:deviceName868488076506128productKeyk1hipglLdjUrandom123456
秘钥:8LQwOYSZMwp3DFBd
HmacMD5计算结果:20558baadb2a0b04847767980f0ee67b
DOWNLOAD参数
--random 为随机数,长度不定
productKey=k1hipglLdjU&deviceName=868488076506128&random=123456&sign=20558baadb2a0b04847767980f0ee67b&signMethod=HmacMD5
mqttClientId: clientId+"|securemode=2,authType=xxxx,random=xxxx,signmethod=xxxx|"
mqttUserName: deviceName+"&"+productKey
mqttPassword: sign_hmac(productSecret,content)
[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
[04:08:35.329]发→◇AT+HTTPINIT
□
[04:08:35.332]收←◆AT+HTTPINIT
OK
[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
[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
[04:09:11.437]发→◇AT+HTTPDATA=120,20000
□
[04:09:11.439]收←◆AT+HTTPDATA=120,20000
DOWNLOAD
[04:09:16.944]发→◇productKey=k1hipglLdjU&deviceName=868488076506128&random=123456&sign=20558baadb2a0b04847767980f0ee67b&signMethod=HmacMD5
□
[04:09:16.947]收←◆
OK
[04:09:26.653]发→◇AT+HTTPACTION=1
□
[04:09:26.656]收←◆AT+HTTPACTION=1
OK
[04:09:28.828]收←◆
+HTTPACTION: 1,200,149
[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
[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
[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
+MSUB:<topic>,<len>,<message>
+MSUB:<store_addr>
[05:02:05.102]发→◇AT+MQTTMSGSET=0
□
[05:02:05.105]收←◆AT+MQTTMSGSET=0
OK
外链图片转存中…(img-YxMoe4AE-1721627349846)]
[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
+MSUB:<topic>,<len>,<message>
+MSUB:<store_addr>
[05:02:05.102]发→◇AT+MQTTMSGSET=0
□
[05:02:05.105]收←◆AT+MQTTMSGSET=0
OK
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。