From 5b9e8a3754644eb2a4f47830c57194816012188a Mon Sep 17 00:00:00 2001 From: Ollo Date: Mon, 6 Jan 2025 11:45:51 +0100 Subject: [PATCH] Reduce serial reading to once a second, but then read all available lines --- mqttclient/fanATserial2mqtt.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/mqttclient/fanATserial2mqtt.py b/mqttclient/fanATserial2mqtt.py index a4b3f58..8942093 100644 --- a/mqttclient/fanATserial2mqtt.py +++ b/mqttclient/fanATserial2mqtt.py @@ -24,6 +24,8 @@ serial_device=os.environ['SERIAL_DEVICE'] try: if (os.environ['PY_LOGGING']): customLevel = os.environ['PY_LOGGING'].upper() + else: + customLevel = 'ERROR' except (KeyError): customLevel = 'WARNING' @@ -77,6 +79,7 @@ def on_message(client, userdata, msg): def on_connect(client, obj, flags, reason_code, properties): client.subscribe(mqtt_topic + "cmd/led") client.subscribe(mqtt_topic + "cmd/rpm") + client.publish(mqtt_topic + "logging", payload=customLevel) client.publish(mqtt_topic + "online", payload="true", qos=1, retain=True) logging.debug("Connected: " + str(reason_code)) @@ -122,6 +125,14 @@ try: try: line = ser.readline() # read a '\n' terminated line + logging.debug("Read one line: " + str(line) ) + if (ser.in_waiting > 0): + for i in range(10): + if (ser.in_waiting > 0): + line = ser.readline() # read a '\n' terminated line + logging.debug("" + str(i+1) + ". line: " + str(line) ) + else: + logging.debug("No additional lines") except (serial.SerialException): SerialCommunicationFailureCounter = SerialCommunicationFailureCounter + 1 client.publish(mqtt_topic + "serialcom/failure", str(SerialCommunicationFailureCounter)) @@ -194,9 +205,9 @@ try: 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.5) + client.publish(mqtt_topic + "debug", "invalid:" + str(l)) # line was invalid and could not be parsed + # Always wait between two cycles + time.sleep(0.9) except (KeyboardInterrupt, SystemExit): logging.error("User aborted service ...")