From a58b5266703db88ca6d9b0895b0274365d898bc9 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sat, 7 Dec 2024 15:26:22 +0100 Subject: [PATCH] Controller to MQTT pipeline is working --- mqttclient/fanATserial2mqtt.py | 67 ++++++++++++++++++++++++++++++---- src/main.cpp | 2 +- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/mqttclient/fanATserial2mqtt.py b/mqttclient/fanATserial2mqtt.py index f5fc23d..6001f03 100644 --- a/mqttclient/fanATserial2mqtt.py +++ b/mqttclient/fanATserial2mqtt.py @@ -5,6 +5,8 @@ from paho.mqtt import client as mqtt_client import os import serial +MAXIMUM_COMMUNICATION_FAILURE=100 + # MQTT Settings mqtt_server=os.environ['MQTT_SERVER'] mqtt_topic=os.environ['MQTT_TOPIC'] @@ -27,24 +29,75 @@ if (not (mqtt_server)): if (not (mqtt_topic)): print("MQTT_TOPIC is not set") +# Start MQTT Client client.connect(mqtt_server, 1883) -val=0 - client.loop_start() +# Start Serial communication ser = serial.Serial(serial_device, 115200, timeout=1) # open serial port client.publish(mqtt_topic + "device", ser.name) # check which port was really used -line = ser.readline() # read a '\n' terminated line -client.publish(mqtt_topic + "debug", line) # check which port was really used +SerialCommunicationFailureCounter=0 # Endless Loop while True: try: - client.publish(mqtt_topic + "val", val) + try: + if (not (ser.is_open)): + ser.open() + continue + except (serial.SerialException): + client.publish(mqtt_topic + "exception", "serial reopen") # check which port was really used + time.sleep(1.0) - time.sleep(0.2) - val = val + 1 + try: + line = ser.readline() # read a '\n' terminated line + except (serial.SerialException): + SerialCommunicationFailureCounter = SerialCommunicationFailureCounter + 1 + client.publish(mqtt_topic + "serialcom/failure", str(SerialCommunicationFailureCounter)) + line=None + if (SerialCommunicationFailureCounter > MAXIMUM_COMMUNICATION_FAILURE): + raise serial.SerialException("device reports readiness / device disconnected " + str(MAXIMUM_COMMUNICATION_FAILURE) + " times") + + if (line): + client.publish(mqtt_topic + "exception", "") # clear exception + # convert byte array to string + l = line.decode("utf-8") + try: + # Parse the controller information + # e.g. : 22.00;0%;0RPM + temperatur, percent, rpm = l.split(";") + except (ValueError): + # activate debug code + temperatur = None + percent = None + rpm = None + + if ((temperatur) and (percent) and (rpm)): + parseError=False + temperatur = temperatur.strip() + client.publish(mqtt_topic + "temperatur", str(temperatur)) + if (percent.endswith('/100')): + client.publish(mqtt_topic + "target", str(percent[:-4])) + else: + client.publish(mqtt_topic + "debug", "target%% invalid:" + str(percent)) + parseError=True + + # remove new line for last element + rpm = rpm.replace('\r','').replace('\n','') + if (rpm.endswith('RPM')): + client.publish(mqtt_topic + "rpm", str(rpm[:-3])) + else: + client.publish(mqtt_topic + "debug", "RPM invalid:" + str(rpm)) + parseError=True + + # cleanup debugging + if (parseError == False): + client.publish(mqtt_topic + "debug", None) + else: + client.publish(mqtt_topic + "debug", line) # line was invalid and could not be parsed + else: + time.sleep(0.2) except (KeyboardInterrupt, SystemExit): cleanAndExit() diff --git a/src/main.cpp b/src/main.cpp index 4585b20..e698370 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -241,7 +241,7 @@ void loop() } Serial.print(fanSpeedPercent); - Serial.print("%;"); + Serial.print("/100;"); setFanSpeedPercent(fanSpeedPercent); Serial.print(actualFanSpeedRpm);