diff --git a/sensor_bridge.py b/sensor_bridge.py index 5cc2e80..1b6d42b 100644 --- a/sensor_bridge.py +++ b/sensor_bridge.py @@ -77,24 +77,24 @@ def read_and_publish_temperature_humidity(mqtt_client, modbus_client, host_info) humidity = raw_hum * 100 / 1000 logging.info(f"Humidity from {host_info['ip']}: {humidity:.1f}%RH") - # Publish data + # Publish data in new tree structure location = host_info["location"] current_time = datetime.now(LOCAL_TIMEZONE).strftime("%Y-%m-%d %H:%M:%S") base_topic = f"Location/{location}" topics_data = [ (f"{base_topic}/Time", current_time), (f"{base_topic}/Status", "online"), - (f"{base_topic}/Temperature", round(temperature, 1)), - (f"{base_topic}/Humidity", round(humidity, 1)) + (f"{base_topic}/Temperature/temperature_value", round(temperature, 1)), + (f"{base_topic}/Humidity/humidity_value", round(humidity, 1)), ] - + all_published = True for topic, value in topics_data: try: payload = str(value) result = mqtt_client.publish(topic, payload) result.wait_for_publish() - + if result.is_published(): logging.debug(f"Published to '{topic}': {payload}") else: @@ -103,7 +103,7 @@ def read_and_publish_temperature_humidity(mqtt_client, modbus_client, host_info) except Exception as e: logging.error(f"Error publishing to '{topic}': {e}") all_published = False - + return all_published except Exception as e: @@ -115,46 +115,46 @@ def read_and_publish_cwt_co2(mqtt_client, modbus_client, host_info): try: # Read all 3 registers result = modbus_client.read_holding_registers(address=0, count=3, slave=UNIT_ID) - + if not hasattr(result, 'registers') or len(result.registers) != 3: logging.error(f"Error reading CWT registers from {host_info['ip']}: {result}") return False - + raw_humidity = result.registers[0] raw_temperature = result.registers[1] raw_co2 = result.registers[2] - + # Process values humidity = raw_humidity / 10.0 - + if raw_temperature > 32767: temperature = (raw_temperature - 65536) / 10.0 else: temperature = raw_temperature / 10.0 - + co2_ppm = raw_co2 - + 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"] current_time = datetime.now(LOCAL_TIMEZONE).strftime("%Y-%m-%d %H:%M:%S") base_topic = f"Location/{location}" topics_data = [ (f"{base_topic}/Time", current_time), (f"{base_topic}/Status", "online"), - (f"{base_topic}/Temperature", round(temperature, 1)), - (f"{base_topic}/Humidity", round(humidity, 1)), - (f"{base_topic}/CO2", co2_ppm) + (f"{base_topic}/Temperature/temperature_value", round(temperature, 1)), + (f"{base_topic}/Humidity/humidity_value", round(humidity, 1)), + (f"{base_topic}/CO2/CO2_value", co2_ppm), ] - + all_published = True for topic, value in topics_data: try: payload = str(value) result = mqtt_client.publish(topic, payload) result.wait_for_publish() - + if result.is_published(): logging.debug(f"Published to '{topic}': {payload}") else: @@ -163,7 +163,7 @@ def read_and_publish_cwt_co2(mqtt_client, modbus_client, host_info): except Exception as e: logging.error(f"Error publishing to '{topic}': {e}") all_published = False - + return all_published except Exception as e: