跳到主要内容

MQTT Topic

INDEVOLT MQTT 默认提供以下 Topic 结构,开发者可根据实际部署需求进行自定义。

Topic说明
/test/subtopic设备数据上报
/test/subtopic/control控制命令
/test/subtopic/control_ack控制执行结果

1. 数据上报

设备通过数据 Topic 主动发布运行数据。

Topic

/test/subtopic

Subscribe

# Example: connect to a local MQTT Broker
# Replace 127.0.0.1 with your MQTT Broker address if using a remote Broker
mosquitto_sub -h 127.0.0.1 -t "/test/subtopic" -v

数据类型

同一个 Topic 下可能包含多种数据类型,客户端可通过 dataType 字段区分消息类型。

JSON 消息中的 Key 对应设备数据点位,Value 为对应的数据值。详细数据点定义请参考 MQTT Data Points

dataType说明
gateway_data网关信息
battery_data电池数据
inv_data逆变器数据
subdevice_info子设备信息数据
subdevice_data子设备数据

示例消息

{
"GW_SN": "xxxxxxxxxx",
"dataType": "battery_data",
"1118": 10108,
"1109": 10045,
"1119": 138,
"1120": 10175,
"614": 1792,
"142": 3.59,
...
}

2. 控制命令

第三方系统可以通过控制 Topic 向设备发送控制请求。

Topic

/test/subtopic/control

Publish

# Example: connect to a local MQTT Broker
# Replace 127.0.0.1 with your MQTT Broker address if using a remote Broker
mosquitto_pub -h 127.0.0.1 -t "/test/subtopic/control" -m '{}'

消息格式

{
"GW_SN": "xxxxxxxx",
"dataType": "modbusString",
"SN1": "xxxxxxxx",
"dataMap": [
{
"groupNum": "2"
},
{
"groupID": "0",
"commandString": "010300000001840A"
},
{
"groupID": "1",
"commandString": "0110000001020000303A"
}
]
}

字段说明

字段类型说明
GW_SNString网关序列号。
dataTypeString消息类型,固定为 modbusString
SN1String目标设备序列号。
dataMapArrayModbus 命令列表。

dataMap 说明

dataMap 中可包含一条或多条 Modbus 命令,设备按照数组顺序依次执行。

字段类型说明
groupNumString命令总数。
groupIDString当前命令编号,从 0 开始。
commandStringStringModbus 指令,十六进制字符串格式。

commandString 的内容应符合 Modbus 报文格式,包括设备地址、功能码、寄存器地址、数据及 CRC 校验。具体寄存器地址请参考:Modbus 寄存器说明


3. 控制应答

设备执行控制命令后,通过控制应答 Topic 返回执行结果。

Topic

/test/subtopic/control_ack

Subscribe

# Example: connect to a local MQTT Broker
# Replace 127.0.0.1 with your MQTT Broker address if using a remote Broker
mosquitto_sub -h 127.0.0.1 -t "/test/subtopic/control_ack" -v

示例消息

{
"GW_SN": "xxxxxxxx",
"dataType": "modbusString",
"SN1": "xxxxxxxx",
"dataMap": [
{
"groupNum": "2"
},
{
"groupID": "0",
"result": "success",
"commandResponse": "018302C0F1"
},
{
"groupID": "1",
"result": "success",
"commandResponse": "0190030C01"
}
]
}

字段说明

字段类型说明
resultString命令执行结果
commandResponseStringModbus 返回数据