Reduce MQTT update frequency
This commit is contained in:
parent
90ceb142a8
commit
d1db379f41
@ -7,23 +7,27 @@ import serial
|
|||||||
|
|
||||||
MAXIMUM_COMMUNICATION_FAILURE=100
|
MAXIMUM_COMMUNICATION_FAILURE=100
|
||||||
|
|
||||||
|
TEMPERATUR_MIN_DIFFERENCE = 2
|
||||||
|
TARGET_MIN_DIFFERENCE = 1
|
||||||
|
RPM_MIN_DIFFERENCE = 50
|
||||||
|
|
||||||
# MQTT Settings
|
# MQTT Settings
|
||||||
mqtt_server=os.environ['MQTT_SERVER']
|
mqtt_server=os.environ['MQTT_SERVER']
|
||||||
mqtt_topic=os.environ['MQTT_TOPIC']
|
mqtt_topic=os.environ['MQTT_TOPIC']
|
||||||
# SERIAL Port
|
# SERIAL Port
|
||||||
serial_device=os.environ['SERIAL_DEVICE']
|
serial_device=os.environ['SERIAL_DEVICE']
|
||||||
|
|
||||||
|
# Start mqtt
|
||||||
|
client_id = f'python-fan-ctrl'
|
||||||
|
client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION2)
|
||||||
|
# Start Serial communication
|
||||||
|
ser = serial.Serial(serial_device, 115200, timeout=1) # open serial port
|
||||||
|
|
||||||
# The callback for when a PUBLISH message is received from the server.
|
# The callback for when a PUBLISH message is received from the server.
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
print(msg.topic+" "+str(msg.payload))
|
print(msg.topic+" "+str(msg.payload))
|
||||||
|
|
||||||
|
# Handle MQTT Commands
|
||||||
# Start mqtt
|
|
||||||
client_id = f'python-fan-ctrl'
|
|
||||||
client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION2)
|
|
||||||
|
|
||||||
|
|
||||||
def on_connect(mqttc, obj, flags, reason_code, properties):
|
def on_connect(mqttc, obj, flags, reason_code, properties):
|
||||||
client.subscribe(mqtt_topic + "cmd/led")
|
client.subscribe(mqtt_topic + "cmd/led")
|
||||||
client.subscribe(mqtt_topic + "cmd/rpm")
|
client.subscribe(mqtt_topic + "cmd/rpm")
|
||||||
@ -42,15 +46,18 @@ if (not (mqtt_topic)):
|
|||||||
client.connect(mqtt_server, 1883)
|
client.connect(mqtt_server, 1883)
|
||||||
client.loop_start()
|
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
|
client.publish(mqtt_topic + "device", ser.name) # check which port was really used
|
||||||
|
|
||||||
SerialCommunicationFailureCounter=0
|
SerialCommunicationFailureCounter=0
|
||||||
|
|
||||||
|
# store last published value
|
||||||
|
lastTemperatur = -1
|
||||||
|
lastRPM = -1
|
||||||
|
lastTarget = -1
|
||||||
|
|
||||||
# Endless Loop
|
# Endless Loop
|
||||||
while True:
|
try:
|
||||||
try:
|
while True:
|
||||||
try:
|
try:
|
||||||
if (not (ser.is_open)):
|
if (not (ser.is_open)):
|
||||||
ser.open()
|
ser.open()
|
||||||
@ -84,10 +91,16 @@ while True:
|
|||||||
|
|
||||||
if ((temperatur) and (percent) and (rpm)):
|
if ((temperatur) and (percent) and (rpm)):
|
||||||
parseError=False
|
parseError=False
|
||||||
temperatur = temperatur.strip()
|
currentTemperatur = float(temperatur.strip())
|
||||||
client.publish(mqtt_topic + "temperatur", str(temperatur))
|
if (abs(currentTemperatur - lastTemperatur) > TEMPERATUR_MIN_DIFFERENCE):
|
||||||
|
client.publish(mqtt_topic + "temperatur", str(currentTemperatur))
|
||||||
|
lastTemperatur = currentTemperatur
|
||||||
|
|
||||||
if (percent.endswith('/100')):
|
if (percent.endswith('/100')):
|
||||||
client.publish(mqtt_topic + "target", str(percent[:-4]))
|
currentTarget = int(percent[:-4])
|
||||||
|
if (abs(lastTarget - currentTarget) > TARGET_MIN_DIFFERENCE):
|
||||||
|
client.publish(mqtt_topic + "target", str(currentTarget))
|
||||||
|
lastTarget = currentTarget
|
||||||
else:
|
else:
|
||||||
client.publish(mqtt_topic + "debug", "target%% invalid:" + str(percent))
|
client.publish(mqtt_topic + "debug", "target%% invalid:" + str(percent))
|
||||||
parseError=True
|
parseError=True
|
||||||
@ -95,7 +108,10 @@ while True:
|
|||||||
# remove new line for last element
|
# remove new line for last element
|
||||||
rpm = rpm.replace('\r','').replace('\n','')
|
rpm = rpm.replace('\r','').replace('\n','')
|
||||||
if (rpm.endswith('RPM')):
|
if (rpm.endswith('RPM')):
|
||||||
client.publish(mqtt_topic + "rpm", str(rpm[:-3]))
|
currentRPM = int(rpm[:-3])
|
||||||
|
if (abs(currentRPM-lastRPM) > RPM_MIN_DIFFERENCE):
|
||||||
|
client.publish(mqtt_topic + "rpm", str(currentRPM))
|
||||||
|
lastRPM = currentRPM
|
||||||
else:
|
else:
|
||||||
client.publish(mqtt_topic + "debug", "RPM invalid:" + str(rpm))
|
client.publish(mqtt_topic + "debug", "RPM invalid:" + str(rpm))
|
||||||
parseError=True
|
parseError=True
|
||||||
@ -108,9 +124,10 @@ while True:
|
|||||||
else:
|
else:
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
|
|
||||||
except (KeyboardInterrupt, SystemExit):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
|
print("User aborted service ...")
|
||||||
client.publish(mqtt_topic + "debug", "User aborted service")
|
client.publish(mqtt_topic + "debug", "User aborted service")
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
print("User aborted service")
|
print("service Dead")
|
||||||
|
|
||||||
client.loop_stop()
|
client.loop_stop()
|
||||||
|
Loading…
Reference in New Issue
Block a user