多个设备接收modbus,ies104协议 并解析协议 通过mqtt上传到服务端代码
【代码】多个设备接收modbus,ies104协议 并解析协议 通过mqtt上传到服务端代码。
·
建立一个iot平台,可以配置(bms(电压、电流、温度、soh),pcs,电池组、智能电表、空调)多个设备接收modbus,ies104协议 并解析协议 通过mqtt上传到服务端
要建立一个IoT平台,可以配置多个设备接收Modbus和IES104协议,并解析这些协议,并通过MQTT将数据上传到服务端,您可以使用以下Python示例代码作为基础:
from pymodbus.client.sync import ModbusTcpClient
from pyies104 import IES104Client
import paho.mqtt.client as mqtt
# MQTT连接信息
mqtt_broker = "mqtt.example.com"
mqtt_port = 1883
mqtt_username = "your_username"
mqtt_password = "your_password"
mqtt_topic_prefix = "your_topic_prefix"
# 设备配置信息
devices = [
{
"name": "BMS",
"protocol": "modbus",
"host": "bms_device.example.com",
"port": 502,
"registers": [
{"name": "Voltage", "address": 0x0000, "count": 10},
{"name": "Current", "address": 0x0010, "count": 10},
{"name": "Temperature", "address": 0x0020, "count": 10},
{"name": "SOH", "address": 0x0030, "count": 10}
]
},
{
"name": "PCS",
"protocol": "modbus",
"host": "pcs_device.example.com",
"port": 502,
"registers": [
{"name": "Data1", "address": 0x0000, "count": 10},
{"name": "Data2", "address": 0x0010, "count": 10},
{"name": "Data3", "address": 0x0020, "count": 10}
]
},
{
"name": "Battery",
"protocol": "modbus",
"host": "battery_device.example.com",
"port": 502,
"registers": [
{"name": "Data1", "address": 0x0000, "count": 10},
{"name": "Data2", "address": 0x0010, "count": 10},
{"name": "Data3", "address": 0x0020, "count": 10}
]
},
{
"name": "SmartMeter",
"protocol": "modbus",
"host": "smartmeter_device.example.com",
"port": 502,
"registers": [
{"name": "Data1", "address": 0x0000, "count": 10},
{"name": "Data2", "address": 0x0010, "count": 10},
{"name": "Data3", "address": 0x0020, "count": 10}
]
},
{
"name": "AirConditioner",
"protocol": "ies104",
"host": "airconditioner_device.example.com",
"port": 2404,
"do_address": "your_do_address"
}
]
# Modbus读取函数
def read_modbus_data(device, mqtt_client):
client = ModbusTcpClient(device["host"], device["port"])
client.connect()
# 读取并解析Modbus数据
for register in device["registers"]:
response = client.read_holding_registers(register["address"], register["count"])
if response.isError():
print(f"Error reading Modbus data from {device['name']}")
else:
data = response.registers
print(f"Modbus data from {device['name']} - {register['name']}: {data}")
# 发布数据到MQTT
mqtt_topic = f"{mqtt_topic_prefix}/{device['name']}/{register['name']}"
mqtt_client.publish(mqtt_topic, str(data))
client.close()
# IES104读取函数
def read_ies104_data(device, mqtt_client):
client = IES104Client(device["host"], device["port"])
client.connect()
# 读取IES104数据
response = client.read_data_object(device["do_address"])
# 解析IES104数据
if response.error:
print(f"Error reading IES104 data from {device['name']}")
else:
data = response.value
print(f"IES104 data from {device['name']}: {data}")
# 发布数据到MQTT
mqtt_topic = f"{mqtt_topic_prefix}/{device['name']}"
mqtt_client.publish(mqtt_topic, str(data))
client.close()
# MQTT连接回调函数
def on_connect(client, userdata, flags, rc):
print(f"Connected to MQTT broker with result code {rc}")
# 主程序
if __name__ == "__main__":
# 创建MQTT客户端
mqtt_client = mqtt.Client()
mqtt_client.username_pw_set(mqtt_username, mqtt_password)
mqtt_client.on_connect = on_connect
# 连接到MQTT服务器
mqtt_client.connect(mqtt_broker, mqtt_port)
# 循环处理MQTT消息
mqtt_client.loop_start()
# 配置多个设备
for device in devices:
if device["protocol"] == "modbus":
# 读取并解析Modbus数据,并上传到MQTT
read_modbus_data(device, mqtt_client)
elif device["protocol"] == "ies104":
# 读取并解析IES104数据,并上传到MQTT
read_ies104_data(device, mqtt_client)
else:
print(f"Unsupported protocol: {device['protocol']}")
# 断开MQTT连接
mqtt_client.loop_stop()
mqtt_client.disconnect()
请根据您的实际情况修改代码中的相关参数,包括MQTT连接信息、设备配置信息和MQTT主题前缀。确保您已安装所需的依赖库(pymodbus、pyies104和paho-mqtt)。
这是一个基本的示例,您可以根据您的需求进行进一步的修改和扩展。例如,您可以根据设备的协议类型选择不同的解析方式,并根据需要添加更多的设备和寄存器配置信息。希望对您有所帮助!如果您有任何进一步的问题,请随时告诉我。
更多推荐
所有评论(0)