|
使用MQTT連接阿里云IoT 注意, 在進(jìn)行MQTT CONNECT協(xié)議設(shè)置的時(shí)候 - Connect指令中的KeepAlive有效范圍[60秒,300秒],否則會(huì)拒絕連接。
- 如果同一個(gè)設(shè)備多個(gè)連接可能會(huì)導(dǎo)致客戶端互相上下線,MQTT默認(rèn)開源SDK會(huì)自動(dòng)重連,您可以通過(guò)日志服務(wù)看到設(shè)備行為。
MQTT 連接域名: 使用域名:${productKey}.iot-as-mqtt.cn-shanghai.aliyuncs.com:1883${productKey}請(qǐng)?zhí)鎿Q為您的產(chǎn)品key,mqtt的Connect報(bào)文參數(shù)如下:mqtt的Connect報(bào)文參數(shù)如下:mqttClientId: clientId+"|securemode=3,signmethod=hmacsha1,timestamp=132323232|"mqttUsername: deviceName+"&"+productKeymqttPassword: sign_hmac(deviceSecret,content)sign簽名需要把以下參數(shù)按字典序排序后,再根據(jù)signmethod加簽。content=提交給服務(wù)器的參數(shù)(productKey,deviceName,timestamp,clientId), 按照字母順序排序, 然后將參數(shù)值依次拼接其中clientId是客戶端自表示id,建議mac或sn,64字符內(nèi);timestamp當(dāng)前時(shí)間毫秒值,僅做混淆,可以不傳遞;mqttClientId格式中||內(nèi)為擴(kuò)展參數(shù);signmethod代表簽名算法類型,可支持hmacSha1,hmacSha256,hmacMd5;securemode代表目前安全模式,可選值有2 (TLS直連模式)、3(TCP直連模式;實(shí)例 如果clientId = 12345,deviceName = device, productKey = pk, timestamp = 789,signmethod=hmacsha1,deviceSecret=secret,那么使用tcp方式提交給mqtt參數(shù)分別如下:mqttclientId=12345|securemode=3,signmethod=hmacsha1,timestamp=789|username=device&pkpassword=hmacsha1("secret","clientId12345deviceNamedeviceproductKeypktimestamp789").toHexString(); //最后是二進(jìn)制轉(zhuǎn)16制字符串,大小寫不敏感。 這個(gè)例子結(jié)果為 FAFD82A3D602B37FB0FA8B7892F24A477F851A14注意上面3個(gè)參數(shù)分別是mqtt Connect登錄報(bào)文的mqttClientId,mqttUsername,mqttPasswrod傳輸數(shù)據(jù)形式透?jìng)?/strong>設(shè)備端直接通過(guò)上發(fā)自定義二進(jìn)制數(shù)據(jù), 云端對(duì)上傳上來(lái)的二進(jìn)制數(shù)據(jù)直接進(jìn)行解析(通過(guò)云端轉(zhuǎn)換腳本, 需自行開發(fā)), 并轉(zhuǎn)換為標(biāo)準(zhǔn)Alink協(xié)議數(shù)據(jù)的格式。而在下行時(shí), 云端同樣會(huì)通過(guò)經(jīng)過(guò)解析, 把標(biāo)準(zhǔn)的Alink協(xié)議數(shù)據(jù)轉(zhuǎn)換成二進(jìn)制數(shù)據(jù), 并發(fā)送到設(shè)備端。 透?jìng)飨嚓P(guān)的Topic定義如下,傳輸數(shù)據(jù)為二進(jìn)制數(shù)據(jù)。 下行 Topic
設(shè)備端訂閱Topic:/sys/{productKey}/{deviceName}/thing/model/down_raw
設(shè)備端回復(fù)Topic:/sys/{productKey}/{deviceName}/thing/model/down_raw_reply 上行 Topic
設(shè)備端發(fā)布Topic:/sys/{productKey}/{deviceName}/thing/model/up_raw
服務(wù)端回復(fù)Topic(需設(shè)備端訂閱):/sys/{productKey}/{deviceName}/thing/model/up_raw_reply 非透?jìng)鳎ˋlink格式)設(shè)備直接構(gòu)造對(duì)應(yīng)Topic以及對(duì)應(yīng)的json格式數(shù)據(jù), 云端直接進(jìn)行解析。 設(shè)備屬性上報(bào)設(shè)備端發(fā)布Topic: /sys/{productKey}/{deviceName}/thing/event/property/postpayload:{ "id" : "123", "version":"1.0", "params" : { "Power" : "on", "WF" : "2" }, "method":"thing.event.property.post"}服務(wù)端回復(fù)Topic(需設(shè)備端訂閱): /sys/{productKey}/{deviceName}/thing/event/property/post_replypayload:{ "id":"123", "code":200, "data":{}}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
設(shè)備事件上報(bào)設(shè)備端發(fā)布Topic: /sys/{productKey}/{deviceName}/thing/event/{tsl.event.identifer}/postpayload:{ "id" : "123", "version":"1.0", "params" : { "Power" : "on", "WF" : "2" }, "method":"thing.event.{tsl.event.identifier}.post"}服務(wù)端回復(fù)Topic(需設(shè)備端訂閱):/sys/{productKey}/{deviceName}/thing/event/{tsl.event.identifer}/post_replypayload:{ "id" : "123", "code":200, "data" : {}}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
服務(wù)器獲取設(shè)備屬性設(shè)備端訂閱Topic: /sys/{productKey}/{deviceName}/thing/service/property/get注: params中為Json數(shù)據(jù), 參數(shù)為開發(fā)者設(shè)置的屬性名字, 詳情參考TSL描述。payload:{ "id" : "123", "version":"1.0", "params" : [ "power" , "temp" ], "method":"thing.service.property.get"}注:服務(wù)端不會(huì)對(duì)get_reply數(shù)據(jù)做處理,目前獲取到的屬性值要求設(shè)備通過(guò)post方式上報(bào)。設(shè)備端回復(fù)Topic:/sys/{productKey}/{deviceName}/thing/event/property/postpayload:{ "id":"123", "code":200, "data":{ "power":"on", "temp":"23" }}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
服務(wù)器設(shè)置設(shè)備屬性設(shè)備端訂閱Topic: /sys/{productKey}/{deviceName}/thing/service/property/setpayload:{ "id" : "123", "version":"1.0", "params" : { "Power" : "on", "WF" : "2" }, "method":"thing.service.property.set"}設(shè)備端回復(fù)Topic: /sys/{productKey}/{deviceName}/thing/service/property/set_replypayload:{ "id":"123", "code":200, "data":{}}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
服務(wù)器觸發(fā)指定服務(wù)異步模式下: 設(shè)備端訂閱Topic: /sys/{productKey}/{deviceName}/thing/service/{tsl.service.identifer}payload:{ "id" : "123", "version":"1.0", "params" : { "Power" : "on", "WF" : "2" }, "method":"thing.service.{tsl.service.identifer}"}設(shè)備端回復(fù)Topic:/sys/{productKey}/{deviceName}/thing/service/{tsl.service.identifer}_replypayload:{ "id":"123", "code":200, "data":{}}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
同步模式下:
注: messageId請(qǐng)參考【同步調(diào)用】中具體描述 設(shè)備端訂閱Topic: /sys/{productKey}/{deviceName}/rrpc/request/+payload:{ "id" : "123", "version":"1.0", "params" : { "Power" : "on", "WF" : "2" }, "method":"thing.service.{tsl.service.identifer}"}設(shè)備端回復(fù)Topic:/sys/{productKey}/{deviceName}/rrpc/response/{messageId}payload:{ "id":"123", "code":200, "data":{}}
|
|