Start implementing MQTT routines
This commit is contained in:
@@ -1,7 +1,16 @@
|
||||
# This file defines the Mees Electronincs ModBus device registers
|
||||
|
||||
devices:
|
||||
1:
|
||||
- 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]
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user