Refactor MQTT topic structure in sensor_bridge.py to adopt a nested format for temperature, humidity, and CO2 data. Updated publishing methods to reflect the new tree structure, enhancing data organization and clarity in sensor reporting.
This commit is contained in:
@ -77,24 +77,24 @@ def read_and_publish_temperature_humidity(mqtt_client, modbus_client, host_info)
|
|||||||
humidity = raw_hum * 100 / 1000
|
humidity = raw_hum * 100 / 1000
|
||||||
logging.info(f"Humidity from {host_info['ip']}: {humidity:.1f}%RH")
|
logging.info(f"Humidity from {host_info['ip']}: {humidity:.1f}%RH")
|
||||||
|
|
||||||
# Publish data
|
# Publish data in new tree structure
|
||||||
location = host_info["location"]
|
location = host_info["location"]
|
||||||
current_time = datetime.now(LOCAL_TIMEZONE).strftime("%Y-%m-%d %H:%M:%S")
|
current_time = datetime.now(LOCAL_TIMEZONE).strftime("%Y-%m-%d %H:%M:%S")
|
||||||
base_topic = f"Location/{location}"
|
base_topic = f"Location/{location}"
|
||||||
topics_data = [
|
topics_data = [
|
||||||
(f"{base_topic}/Time", current_time),
|
(f"{base_topic}/Time", current_time),
|
||||||
(f"{base_topic}/Status", "online"),
|
(f"{base_topic}/Status", "online"),
|
||||||
(f"{base_topic}/Temperature", round(temperature, 1)),
|
(f"{base_topic}/Temperature/temperature_value", round(temperature, 1)),
|
||||||
(f"{base_topic}/Humidity", round(humidity, 1))
|
(f"{base_topic}/Humidity/humidity_value", round(humidity, 1)),
|
||||||
]
|
]
|
||||||
|
|
||||||
all_published = True
|
all_published = True
|
||||||
for topic, value in topics_data:
|
for topic, value in topics_data:
|
||||||
try:
|
try:
|
||||||
payload = str(value)
|
payload = str(value)
|
||||||
result = mqtt_client.publish(topic, payload)
|
result = mqtt_client.publish(topic, payload)
|
||||||
result.wait_for_publish()
|
result.wait_for_publish()
|
||||||
|
|
||||||
if result.is_published():
|
if result.is_published():
|
||||||
logging.debug(f"Published to '{topic}': {payload}")
|
logging.debug(f"Published to '{topic}': {payload}")
|
||||||
else:
|
else:
|
||||||
@ -103,7 +103,7 @@ def read_and_publish_temperature_humidity(mqtt_client, modbus_client, host_info)
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Error publishing to '{topic}': {e}")
|
logging.error(f"Error publishing to '{topic}': {e}")
|
||||||
all_published = False
|
all_published = False
|
||||||
|
|
||||||
return all_published
|
return all_published
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -115,46 +115,46 @@ def read_and_publish_cwt_co2(mqtt_client, modbus_client, host_info):
|
|||||||
try:
|
try:
|
||||||
# Read all 3 registers
|
# Read all 3 registers
|
||||||
result = modbus_client.read_holding_registers(address=0, count=3, slave=UNIT_ID)
|
result = modbus_client.read_holding_registers(address=0, count=3, slave=UNIT_ID)
|
||||||
|
|
||||||
if not hasattr(result, 'registers') or len(result.registers) != 3:
|
if not hasattr(result, 'registers') or len(result.registers) != 3:
|
||||||
logging.error(f"Error reading CWT registers from {host_info['ip']}: {result}")
|
logging.error(f"Error reading CWT registers from {host_info['ip']}: {result}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
raw_humidity = result.registers[0]
|
raw_humidity = result.registers[0]
|
||||||
raw_temperature = result.registers[1]
|
raw_temperature = result.registers[1]
|
||||||
raw_co2 = result.registers[2]
|
raw_co2 = result.registers[2]
|
||||||
|
|
||||||
# Process values
|
# Process values
|
||||||
humidity = raw_humidity / 10.0
|
humidity = raw_humidity / 10.0
|
||||||
|
|
||||||
if raw_temperature > 32767:
|
if raw_temperature > 32767:
|
||||||
temperature = (raw_temperature - 65536) / 10.0
|
temperature = (raw_temperature - 65536) / 10.0
|
||||||
else:
|
else:
|
||||||
temperature = raw_temperature / 10.0
|
temperature = raw_temperature / 10.0
|
||||||
|
|
||||||
co2_ppm = raw_co2
|
co2_ppm = raw_co2
|
||||||
|
|
||||||
logging.info(f"CWT from {host_info['ip']} - Temp: {temperature:.1f}°C, Humidity: {humidity:.1f}%RH, CO2: {co2_ppm}ppm")
|
logging.info(f"CWT from {host_info['ip']} - Temp: {temperature:.1f}°C, Humidity: {humidity:.1f}%RH, CO2: {co2_ppm}ppm")
|
||||||
|
|
||||||
# Publish data
|
# Publish data in new tree structure
|
||||||
location = host_info["location"]
|
location = host_info["location"]
|
||||||
current_time = datetime.now(LOCAL_TIMEZONE).strftime("%Y-%m-%d %H:%M:%S")
|
current_time = datetime.now(LOCAL_TIMEZONE).strftime("%Y-%m-%d %H:%M:%S")
|
||||||
base_topic = f"Location/{location}"
|
base_topic = f"Location/{location}"
|
||||||
topics_data = [
|
topics_data = [
|
||||||
(f"{base_topic}/Time", current_time),
|
(f"{base_topic}/Time", current_time),
|
||||||
(f"{base_topic}/Status", "online"),
|
(f"{base_topic}/Status", "online"),
|
||||||
(f"{base_topic}/Temperature", round(temperature, 1)),
|
(f"{base_topic}/Temperature/temperature_value", round(temperature, 1)),
|
||||||
(f"{base_topic}/Humidity", round(humidity, 1)),
|
(f"{base_topic}/Humidity/humidity_value", round(humidity, 1)),
|
||||||
(f"{base_topic}/CO2", co2_ppm)
|
(f"{base_topic}/CO2/CO2_value", co2_ppm),
|
||||||
]
|
]
|
||||||
|
|
||||||
all_published = True
|
all_published = True
|
||||||
for topic, value in topics_data:
|
for topic, value in topics_data:
|
||||||
try:
|
try:
|
||||||
payload = str(value)
|
payload = str(value)
|
||||||
result = mqtt_client.publish(topic, payload)
|
result = mqtt_client.publish(topic, payload)
|
||||||
result.wait_for_publish()
|
result.wait_for_publish()
|
||||||
|
|
||||||
if result.is_published():
|
if result.is_published():
|
||||||
logging.debug(f"Published to '{topic}': {payload}")
|
logging.debug(f"Published to '{topic}': {payload}")
|
||||||
else:
|
else:
|
||||||
@ -163,7 +163,7 @@ def read_and_publish_cwt_co2(mqtt_client, modbus_client, host_info):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Error publishing to '{topic}': {e}")
|
logging.error(f"Error publishing to '{topic}': {e}")
|
||||||
all_published = False
|
all_published = False
|
||||||
|
|
||||||
return all_published
|
return all_published
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
Reference in New Issue
Block a user