Skip to content
Snippets Groups Projects
Commit 69e94e0f authored by Robert Hartung's avatar Robert Hartung
Browse files

added example for controlling a chamber (untested)

parent e75125c8
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
import paho.mqtt.client as paho
import paho.mqtt.publish as publish
import time
import socket
import struct
from threading import Event
CHAMBER_NAME = "lctc-c0X"
TARGET_TEMPERATURE = 10.0
DELTA = 0.5
TIMEOUT = 60.0
CHAMBER_TEMPERATURE_TOPIC = "sht21/"+CHAMBER_NAME+"/temp"
temperature_reached = Event()
client = paho.Client(client_id="chamber_controller_" + socket.gethostname(), clean_session=True)
def on_message(client, userdata, msg):
if msg.topic == CHAMBER_TEMPERATURE_TOPIC:
temp, = struct.unpack("d", msg.payload)
if abs( temp - TARGET_TEMPERATURE ) < DELTA:
temperature_reached.set()
else:
print( "[on_message] {} {}".format(msg.payload, msg.topic) )
def on_disconnect(client, userdata, rc):
print(" [on_disconnect] {}".format( rc ))
def on_connect(client, userdata, flags, rc):
client.subscribe(CHAMBER_TEMPERATURE_TOPIC, qos=2)
def set_temp(temp):
client.publish("chamber/"+CHAMBER_NAME+"/temp", struct.pack("d", temp), qos=2)
if __name__ == "__main__":
client.connect( "localhost", port=1883, keepalive=5 )
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.loop_start()
while True:
# Wait until temperature has been reached
if temperature_reached.wait(TIMEOUT):
print("timeout reached.")
# Do something here for 1 minute
time.sleep(60.0)
# Increase target temperature
TARGET_TEMPERATURE += 1.0
set_temp( TARGET_TEMPERATURE )
temperature_reached.clear()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment