Skip to content

网关 MQTT 接入

网关设备通过 MQTT 接入 ZhiLian.Yun 后,可以代无法直接联网的子设备上报数据、接收平台下发消息,并维护子设备在线状态。

网关自身仍然使用设备证书连接平台,连接参数、接入点和身份认证方式与 设备 MQTT 接入 一致。区别在于:网关代子设备通信时,协议报文使用 gw 表示子设备数据,gw 的键通常是子设备地址。

TIP

直连设备报文使用 data;网关代子设备报文使用 gw。不要把子设备数据放到直连设备的 data 中。

子设备地址

子设备地址由网关侧定义,用于在同一个网关下唯一标识子设备。常见取值包括:

  • Modbus 从站地址。
  • RS485/LoRa 子设备地址。
  • 子设备 SN、MAC、IMEI 或业务编号。
  • 网关内部维护的唯一逻辑地址。

示例中使用 SUB_DEVICE_ADDR1SUB_DEVICE_ADDR2 表示子设备地址。

网关 MQTT 主题一览

以下主题中的 网关设备码 是网关设备在平台中的 deviceCode

发布主题

网关向平台发布消息。

消息类型主题
子设备上报属性值up/网关设备码/attributes/post
子设备获取属性值up/网关设备码/attributes/get
子设备获取期望属性up/网关设备码/attributes/desired_get
子设备清除期望属性up/网关设备码/attributes/desired_delete
子设备回复下发属性up/网关设备码/attributes/set_reply
子设备回复属性采集up/网关设备码/attributes/acquisition_reply
子设备上报事件up/网关设备码/event/post
子设备批量上报或历史上报up/网关设备码/batch/post
子设备回复命令up/网关设备码/command/send_reply
子设备已连接up/网关设备码/gateway/connect
子设备已断开up/网关设备码/gateway/disconnect

订阅主题

网关订阅平台下发给子设备的消息。

消息类型主题
子设备属性上报响应down/网关设备码/attributes/post_reply
子设备属性获取响应down/网关设备码/attributes/get_reply
子设备期望属性响应down/网关设备码/attributes/desired_get_reply
子设备清除期望属性响应down/网关设备码/attributes/desired_delete_reply
子设备接收下发属性down/网关设备码/attributes/set
子设备接收属性采集down/网关设备码/attributes/acquisition
子设备事件上报响应down/网关设备码/event/post_reply
子设备批量上报响应down/网关设备码/batch/post_reply
子设备接收下发命令down/网关设备码/command/send
子设备已连接响应down/网关设备码/gateway/connect_reply
子设备已断开响应down/网关设备码/gateway/disconnect_reply

子设备已连接

当网关确认子设备已连接时,向平台上报子设备连接状态。平台可根据 typeKeyproductKey 自动创建子设备。

发布主题:

text
up/网关设备码/gateway/connect

网关发送消息:

json
{
  "id": 101,
  "ack": 1,
  "time": 1626197189638,
  "gw": {
    "device": "SUB_DEVICE_ADDR1",
    "typeKey": "TYPE_KEY",
    "productKey": "PRODUCT_KEY"
  }
}

平台响应主题:

text
down/网关设备码/gateway/connect_reply

平台回复消息:

json
{
  "id": 101,
  "time": 1626197189640,
  "code": 200
}

子设备已断开

当网关确认子设备已断开时,向平台上报断开状态。

发布主题:

text
up/网关设备码/gateway/disconnect

网关发送消息:

json
{
  "id": 102,
  "ack": 1,
  "time": 1626197189638,
  "gw": {
    "device": "SUB_DEVICE_ADDR1"
  }
}

平台响应主题:

text
down/网关设备码/gateway/disconnect_reply

平台回复消息:

json
{
  "id": 102,
  "time": 1626197189640,
  "code": 200
}

子设备上报属性

网关代子设备上报属性值。gw 的每个键是子设备地址,值是该子设备的属性键值。

发布主题:

text
up/网关设备码/attributes/post

网关发送消息:

json
{
  "id": 1,
  "ack": 1,
  "time": 1626197189638,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "temperature": 26.5,
      "humidity": 62
    },
    "SUB_DEVICE_ADDR2": {
      "switch": true,
      "voltage": 220
    }
  }
}

平台响应主题:

text
down/网关设备码/attributes/post_reply

平台回复消息:

json
{
  "id": 1,
  "time": 1626197189640,
  "code": 200
}

子设备获取属性

网关代子设备获取平台保存的属性当前值。

发布主题:

text
up/网关设备码/attributes/get

网关发送消息:

json
{
  "id": 2,
  "time": 1626197189638,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "keys": ["temperature", "humidity"]
    },
    "SUB_DEVICE_ADDR2": {
      "keys": []
    }
  }
}

平台响应主题:

text
down/网关设备码/attributes/get_reply

平台回复消息:

json
{
  "id": 2,
  "time": 1626197189640,
  "code": 200,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "temperature": {
        "value": 26.5,
        "time": 1626197189640
      },
      "humidity": {
        "value": 62,
        "time": 1626197189640
      }
    },
    "SUB_DEVICE_ADDR2": {
      "switch": {
        "value": true,
        "time": 1626197189640
      }
    }
  }
}

子设备获取期望属性

网关代子设备获取平台暂存的期望属性。

发布主题:

text
up/网关设备码/attributes/desired_get

网关发送消息:

json
{
  "id": 3,
  "ack": 1,
  "time": 1626197189638,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "id": 31,
      "keys": ["switch", "brightness"]
    },
    "SUB_DEVICE_ADDR2": {
      "id": 32,
      "keys": []
    }
  }
}

平台响应主题:

text
down/网关设备码/attributes/desired_get_reply

平台回复消息:

json
{
  "id": 3,
  "time": 1626197189640,
  "code": 200,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "switch": {
        "value": true,
        "time": 1626197189640,
        "version": 1
      },
      "brightness": {
        "value": 80,
        "time": 1626197189640,
        "version": 2
      }
    }
  }
}

子设备清除期望属性

子设备成功执行期望属性后,网关代子设备清除对应期望值。

发布主题:

text
up/网关设备码/attributes/desired_delete

网关发送消息:

json
{
  "id": 4,
  "ack": 1,
  "time": 1626197189638,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "switch": {
        "version": 1
      },
      "brightness": {
        "version": 2
      }
    }
  }
}

平台响应主题:

text
down/网关设备码/attributes/desired_delete_reply

平台回复消息:

json
{
  "id": 4,
  "time": 1626197189640,
  "code": 200
}

子设备事件上报

网关代子设备上报事件。

发布主题:

text
up/网关设备码/event/post

网关发送消息:

json
{
  "id": 5,
  "ack": 1,
  "time": 1626197189638,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "id": 51,
      "key": "alarmEvent",
      "value": {
        "temperature": -10
      }
    },
    "SUB_DEVICE_ADDR2": {
      "id": 52,
      "key": "doorEvent",
      "value": {
        "open": true
      }
    }
  }
}

平台响应主题:

text
down/网关设备码/event/post_reply

平台回复消息:

json
{
  "id": 5,
  "time": 1626197189640,
  "code": 200
}

子设备批量上报或历史上报

网关可代多个子设备一次上报多条属性和事件数据。

发布主题:

text
up/网关设备码/batch/post

网关发送消息:

json
{
  "id": 6,
  "ack": 1,
  "time": 1626197189638,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "attributes": [
        {
          "key": "temperature",
          "value": 26.5,
          "time": 1626197182640
        },
        {
          "key": "humidity",
          "value": 62,
          "time": 1626197189640
        }
      ],
      "events": [
        {
          "key": "alarmEvent",
          "time": 1626197189640,
          "value": {
            "temperature": -10
          }
        }
      ]
    }
  }
}

平台响应主题:

text
down/网关设备码/batch/post_reply

平台回复消息:

json
{
  "id": 6,
  "time": 1626197189640,
  "code": 200
}

子设备属性下发

平台向子设备下发属性时,网关从订阅主题接收消息,并转发给对应子设备。

订阅主题:

text
down/网关设备码/attributes/set

网关接收消息:

json
{
  "id": 7,
  "time": 1626197189638,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "switch": true,
      "brightness": 80
    }
  }
}

网关回复主题:

text
up/网关设备码/attributes/set_reply

网关回复消息:

json
{
  "id": 7,
  "time": 1626197189640,
  "code": 200,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "code": 200
    }
  }
}

子设备属性采集

平台向子设备发起属性采集时,网关负责采集子设备实时值并回复。

订阅主题:

text
down/网关设备码/attributes/acquisition

网关接收消息:

json
{
  "id": 8,
  "time": 1626197189638,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "keys": ["temperature", "humidity"]
    }
  }
}

网关回复主题:

text
up/网关设备码/attributes/acquisition_reply

网关回复消息:

json
{
  "id": 8,
  "time": 1626197189640,
  "code": 200,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "temperature": 26.5,
      "humidity": 62
    }
  }
}

子设备命令下发

平台向子设备下发命令时,网关接收命令并转发给对应子设备,随后返回执行结果。

订阅主题:

text
down/网关设备码/command/send

网关接收消息:

json
{
  "id": 9,
  "time": 1626197189638,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "id": 91,
      "key": "set_param",
      "input": {
        "mode": "auto",
        "threshold": 30
      }
    }
  }
}

网关回复主题:

text
up/网关设备码/command/send_reply

网关回复消息:

json
{
  "id": 9,
  "time": 1626197189640,
  "code": 200,
  "gw": {
    "SUB_DEVICE_ADDR1": {
      "id": 91,
      "key": "set_param",
      "code": 200,
      "output": {
        "result": "ok"
      }
    }
  }
}