跳到主要内容

MQTT 协议

网关连接和注册

通过 mqtt 连接:

mqttClientId: productKey+""+deviceName+""+model

mqttUserName: deviceName

mqttPassword: md5(产品密钥,mqttClientId)

密钥:*

地址: iotkit.cc:1883 或 120.76.96.206:1883

说明:

productKey: 在平台创建的产品 productKey

deviceName: 设备唯一标识,如 MAC

model: 设备型号

网关连接 mqtt 成功后,云端会对网关进行注册

连接成功后网关订阅 topic: /sys/{productKey}/{deviceName}/c/#

云端验证订阅成功后,会将网关置为在线。

子设备注册

由网关上报

请求

Topic: /sys/{网关 productKey}/{网关 deviceName}/s/register

payload 参数:

参数类型说明
idString消息 ID
methodStringthing.lifetime.register
paramsJSON内容固定为:{ "productKey":设备 PK, "deviceName":设 mac, "model":设备型号 }

响应

Topic: /sys/{网关 productKey}/{网关 deviceName}/c/register_reply

payload 参数:

参数类型说明
idString消息 ID
methodStringthing.lifetime.register_reply
codeint响应码,0:成功,x:其它错误
dataJSON注册成功的设备信息: { "productKey":设备 PK, "deviceName":设 mac, "model":设备型号 }

子设备注册成功后,订阅 topic: /sys/{productKey}/{deviceName}/c/# 设备将变为在线,反之取消订阅该 topic,设备将变为离线。

子设备注销

网关接收

请求

Topic: /sys/{网关 productKey}/{网关 deviceName}/c/deregister

payload 参数:

参数类型说明
idString消息 ID
methodStringthing.lifetime.deregister
paramsJSON内容固定为:`

{ "productKey":设备 PK, "deviceName":设 mac }`

响应

Topic: /sys/{网关 productKey}/{网关 deviceName}/s/deregister_reply

payload 参数:

参数类型说明
idString消息 ID
methodStringthing.lifetime.deregister_reply
codeint响应码,0:成功,x:其它错误

云端先对子设备解绑然后网关再注销,方能在其它网关注册。

服务调用

请求

Topic:/sys/{productKey}/{deviceName}/c/service/服务名

payload 参数:

参数类型说明
idString消息 ID
methodStringthing.service.服务名
paramsJSON服务输入参数,格式:{"参数 1":值 1}

具体见产品物模型中服务的定义

响应

Topic:/sys/{productKey}/{deviceName}/s/service/服务名_reply

payload 参数:

参数类型说明
idString消息 ID
methodStringthing.service.服务名_reply
codeint响应码,0:成功,x:其它错误
dataJSON服务调用结果数据

事件上报

请求

Topic: /sys/{productKey}/{deviceName}/s/event/事件名

payload 参数:

参数类型说明
idString消息 ID
methodStringthing.event.事件名
paramsJSON事件上报参数,格式:{"参数 1":值 1},具体见产品物模型中事件的定义

响应

Topic: /sys/{productKey}/{deviceName}/c/event/事件名_reply

payload 参数:

参数类型说明
idString消息 ID
methodStringthing.event.事件名_reply
codeint响应码,0:成功,x:其它错误

属性设置

请求

Topic: /sys/{productKey}/{deviceName}/c/service/property/set

payload 参数:

参数类型说明
idString消息 ID
methodStringthing.service.property.set
paramsJSON属性参数,格式:{"参数 1":值 1},具体见产品物模型中属性的定义

响应

Topic: /sys/{productKey}/{deviceName}/s/service/property/set_reply

payload 参数:

参数类型说明
idString消息 ID
methodStringthing.service.property.set_reply
codeint响应码,0:成功,x:其它错误

属性获取

请求

Topic:/sys/{productKey}/{deviceName}/c/service/property/get

payload 参数

参数类型说明
idString消息 ID
methodStringthing.service.property.get
paramsJSON数组,要获取的属性列表,如:["a","b","c"...]

响应

Topic:/sys/{productKey}/{deviceName}/s/service/property/get_reply

payload 参数:

参数类型说明
idString消息 ID
methodStringthing.service.property.get_reply
codeint响应码,0:成功,x:其它错误

属性上报

请求

Topic:/sys/{productKey}/{deviceName}/s/event/property/post

payload 参数:

参数类型说明
idString消息 ID
methodStringthing.event.property.post
paramsJSON属性参数,格式:{"参数 1":值 1},具体见产品物模型中属性的定义

响应

Topic:/sys/{productKey}/{deviceName}/c/event/property/post_reply

payload 参数:

参数类型说明
idString消息 ID
methodStringthing.event.property.post_reply
codeint响应码,0:成功,x:其它错误