Start implementing MQTT routines

This commit is contained in:
marcel
2025-08-01 20:43:27 +02:00
parent 053ab6348e
commit f722f8aec1
2 changed files with 29 additions and 6 deletions

View File

@@ -1,9 +1,18 @@
# This file defines the Mees Electronincs ModBus device registers # This file defines the Mees Electronincs ModBus device registers
devices: devices:
1: - device_type: 1
input_registers: 6 # The number of available input registers, starting from offset 40 input_registers: 6 # The number of available input registers, starting from offset 40
input_register_names: # Description, unit 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 A, °C]
- [Temperature B , °C] - [Temperature B , °C]
- [Minimum temperature A, °C] - [Minimum temperature A, °C]

View File

@@ -104,7 +104,21 @@ def data_logger(data, configuration):
except: except:
logging.warning("Could not write to file: " + new_filename) 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.") 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("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("Device type: " + str(ModBusData['Type']) + " (" + ModBusData['TypeString'] + ")")
logging.debug (ModBusData['InputRegisters']) 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: except:
logging.warning("Modbus device type " + str(ModBusData['Type']) + " not found in register definition file. Ignoring sensor data.") 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) data_logger(ModBusData, Configuration)
# Send sensor data to MQTT broker # Send sensor data to MQTT broker
send_data_to_mqtt(ModBusData, Configuration) send_data_to_mqtt(ModBusData, Configuration, ModbusRegisters.definition_file_data)