From f722f8aec1c08be8001bf808f7082edbd4b1b6b3 Mon Sep 17 00:00:00 2001 From: marcel Date: Fri, 1 Aug 2025 20:43:27 +0200 Subject: [PATCH] Start implementing MQTT routines --- software/test_software/modbus_registers.yaml | 15 +++++++++++--- .../weather_station_rs485_client.py | 20 ++++++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/software/test_software/modbus_registers.yaml b/software/test_software/modbus_registers.yaml index 2eb5402..12de44a 100644 --- a/software/test_software/modbus_registers.yaml +++ b/software/test_software/modbus_registers.yaml @@ -1,9 +1,18 @@ # This file defines the Mees Electronincs ModBus device registers devices: - 1: - input_registers: 6 # The number of available input registers, starting from offset 40 - input_register_names: # Description, unit + - device_type: 1 + input_registers: 6 # The number of available input registers, starting from offset 40 + input_register_names: # Description, unit + - [Temperature A, °C] + - [Temperature B , °C] + - [Minimum temperature A, °C] + - [Minimum temperature B, °C] + - [Maximum temperature A, °C] + - [Maximum temperature B, °C] + - device_type: 2 + input_registers: 6 # The number of available input registers, starting from offset 40 + input_register_names: # Description, unit - [Temperature A, °C] - [Temperature B , °C] - [Minimum temperature A, °C] diff --git a/software/test_software/weather_station_rs485_client.py b/software/test_software/weather_station_rs485_client.py index dbd1d0c..8548fba 100644 --- a/software/test_software/weather_station_rs485_client.py +++ b/software/test_software/weather_station_rs485_client.py @@ -104,7 +104,21 @@ def data_logger(data, configuration): except: logging.warning("Could not write to file: " + new_filename) -def send_data_to_mqtt(data, configuration): +def send_data_to_mqtt(data, configuration, modbus_registers): + + #logging.debug(modbus_registers) + # Match actual device on ModBus with definition in modbus_registers.yaml + for index1, entry1 in enumerate(modbus_registers['devices']): + if entry1['device_type'] == data['Type']: + # Format serial number for unique MQTT ID + logging.debug("topic: mees_electronics_" + hex(data['ID'][0])[2:].zfill(4) + hex(data['ID'][1])[2:].zfill(4) + hex(data['ID'][2])[2:].zfill(4) + hex(data['ID'][3])[2:].zfill(4)) + logging.debug("type: " + str(data['Type'])) + logging.debug("type_string: " + data['TypeString']) + + # Go through every input register and match the unit and description with the value + for index2, entry2 in enumerate(data['InputRegisters']): + logging.debug(entry1['input_register_names'][index2][0] + ": " + str(entry2) + entry1['input_register_names'][index2][1]) + logging.debug("Send data to MQTT broker.") @@ -174,7 +188,7 @@ while (1): logging.debug("Serial number: " + hex(ModBusData['ID'][1]) + " " + hex(ModBusData['ID'][2]) + " " + hex(ModBusData['ID'][3])) logging.debug("Device type: " + str(ModBusData['Type']) + " (" + ModBusData['TypeString'] + ")") logging.debug (ModBusData['InputRegisters']) - logging.debug (json.dumps(ModBusData, indent=1, sort_keys=False)) + #logging.debug (json.dumps(ModBusData, indent=1, sort_keys=False)) except: logging.warning("Modbus device type " + str(ModBusData['Type']) + " not found in register definition file. Ignoring sensor data.") @@ -183,4 +197,4 @@ while (1): data_logger(ModBusData, Configuration) # Send sensor data to MQTT broker - send_data_to_mqtt(ModBusData, Configuration) + send_data_to_mqtt(ModBusData, Configuration, ModbusRegisters.definition_file_data)