LED and RPM can be controlled via Mqtt
This commit is contained in:
parent
d1db379f41
commit
f954cecfc0
@ -17,22 +17,33 @@ 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
|
# Start Serial communication
|
||||||
ser = serial.Serial(serial_device, 115200, timeout=1) # open serial port
|
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))
|
payload = msg.payload.decode("utf-8")
|
||||||
|
if ((mqtt_topic + "cmd/rpm") == msg.topic):
|
||||||
|
if (ser.is_open):
|
||||||
|
command = "ollpef{:02x}".format(int(payload))
|
||||||
|
ser.write(command.encode('utf-8'))
|
||||||
|
else:
|
||||||
|
client.publish(mqtt_topic + "cmd/exception", "Serial Connection closed") # clear exception
|
||||||
|
elif ((mqtt_topic + "cmd/led") == msg.topic):
|
||||||
|
command = "ollpel" + payload
|
||||||
|
ser.write(command.encode('utf-8'))
|
||||||
|
else:
|
||||||
|
print(msg.topic+" "+str(msg.payload))
|
||||||
|
|
||||||
# Handle MQTT Commands
|
# Handle MQTT Commands
|
||||||
def on_connect(mqttc, obj, flags, reason_code, properties):
|
def on_connect(client, 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")
|
||||||
print("Connected: " + str(reason_code))
|
print("Connected: " + str(reason_code))
|
||||||
|
|
||||||
|
# Start mqtt
|
||||||
|
client_id = f'python-fan-ctrl'
|
||||||
|
client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION2)
|
||||||
client.on_message = on_message
|
client.on_message = on_message
|
||||||
client.on_connect = on_connect
|
client.on_connect = on_connect
|
||||||
|
|
||||||
@ -91,13 +102,22 @@ try:
|
|||||||
|
|
||||||
if ((temperatur) and (percent) and (rpm)):
|
if ((temperatur) and (percent) and (rpm)):
|
||||||
parseError=False
|
parseError=False
|
||||||
currentTemperatur = float(temperatur.strip())
|
try:
|
||||||
|
currentTemperatur = float(temperatur)
|
||||||
|
except (ValueError):
|
||||||
|
print("Temperatur Error: could not convert string to float:" + temperatur)
|
||||||
|
currentTemperatur = lastTemperatur
|
||||||
if (abs(currentTemperatur - lastTemperatur) > TEMPERATUR_MIN_DIFFERENCE):
|
if (abs(currentTemperatur - lastTemperatur) > TEMPERATUR_MIN_DIFFERENCE):
|
||||||
client.publish(mqtt_topic + "temperatur", str(currentTemperatur))
|
client.publish(mqtt_topic + "temperatur", str(currentTemperatur))
|
||||||
lastTemperatur = currentTemperatur
|
lastTemperatur = currentTemperatur
|
||||||
|
|
||||||
if (percent.endswith('/100')):
|
if (percent.endswith('/100')):
|
||||||
currentTarget = int(percent[:-4])
|
try:
|
||||||
|
currentTarget = int(percent[:-4])
|
||||||
|
except (ValueError):
|
||||||
|
print("Percent Error: could not convert string to int:" + (percent[:-4]))
|
||||||
|
currentTarget = lastTarget
|
||||||
|
|
||||||
if (abs(lastTarget - currentTarget) > TARGET_MIN_DIFFERENCE):
|
if (abs(lastTarget - currentTarget) > TARGET_MIN_DIFFERENCE):
|
||||||
client.publish(mqtt_topic + "target", str(currentTarget))
|
client.publish(mqtt_topic + "target", str(currentTarget))
|
||||||
lastTarget = currentTarget
|
lastTarget = currentTarget
|
||||||
@ -108,7 +128,12 @@ try:
|
|||||||
# 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')):
|
||||||
currentRPM = int(rpm[:-3])
|
try:
|
||||||
|
currentRPM = int(rpm[:-3])
|
||||||
|
except (ValueError):
|
||||||
|
print("RPM Error: could not convert string to int:" + (rpm[:-3]))
|
||||||
|
currentRPM = lastRPM
|
||||||
|
|
||||||
if (abs(currentRPM-lastRPM) > RPM_MIN_DIFFERENCE):
|
if (abs(currentRPM-lastRPM) > RPM_MIN_DIFFERENCE):
|
||||||
client.publish(mqtt_topic + "rpm", str(currentRPM))
|
client.publish(mqtt_topic + "rpm", str(currentRPM))
|
||||||
lastRPM = currentRPM
|
lastRPM = currentRPM
|
||||||
|
Loading…
Reference in New Issue
Block a user