diff --git a/software/temp-controller.py b/software/temp-controller.py new file mode 100644 index 0000000000000000000000000000000000000000..9539d07a5d6ca209f63b3d96cdac82c2a0145b88 --- /dev/null +++ b/software/temp-controller.py @@ -0,0 +1,130 @@ + + +#!/usr/bin/env python3 + +import paho.mqtt.client as paho +import paho.mqtt.publish as publish +from time import sleep, time +import socket +import struct +import configparser +from threading import Event + +import os + +CHAMBER_ONE_NAME = "lctc-c01" +CHAMBER_TWO_NAME = "lctc-c04" + +# vars for setting the chamber temp +DELTA = 0.5 +CHAMBER_ONE_TARGET_TEMP = 10 +CHAMBER_TWO_TARGET_TEMP = 10 + +client_one = paho.Client(client_id="temp_logger_" + CHAMBER_ONE_NAME, clean_session=True) +client_two = paho.Client(client_id="temp_logger_" + CHAMBER_TWO_NAME, clean_session=True) + +CHAMBER_ONE_TEMPERATURE_TOPIC = "ds1820/lctc-c01/28-000007fb4c59/temp" +CHAMBER_TWO_TEMPERATURE_TOPIC = "ds1820/lctc-c04/28-000007fbfbcb/temp" + +#logging vars +temp_chamber_one = 0 +temp_chamber_two = 0 + +#event flags +temperature_chamber_one_reached = Event() +temperature_chamber_two_reached = Event() + +def on_message(client, userdata, msg): + global temp_chamber_one + global temp_chamber_two + + if msg.topic == CHAMBER_ONE_TEMPERATURE_TOPIC: + temp, = struct.unpack("d", msg.payload) + temp_chamber_one = temp + if abs( temp - CHAMBER_ONE_TARGET_TEMP ) < DELTA: + temperature_chamber_one_reached.set() + + + elif msg.topic == CHAMBER_TWO_TEMPERATURE_TOPIC: + temp, = struct.unpack("d", msg.payload) + temp_chamber_two = temp + if abs( temp - CHAMBER_TWO_TARGET_TEMP ) < DELTA: + temperature_chamber_two_reached.set() + + else: + print("[on_message] {} {}".format(msg.payload, msg.topic) ) + + print(" {:0>5} | {:0>5} ".format(temp_chamber_one, temp_chamber_two)) + +def on_disconnect(client, userdata, rc): + print("[on_disconnect] {}".format( rc )) + +def on_connect(client, userdata, flags, rc): + if userdata == CHAMBER_ONE_NAME: + client.subscribe(CHAMBER_ONE_TEMPERATURE_TOPIC, qos=2) + print("Subscribed to topic: {}".format(CHAMBER_ONE_TEMPERATURE_TOPIC)) + elif userdata == CHAMBER_TWO_NAME: + client.subscribe(CHAMBER_TWO_TEMPERATURE_TOPIC, qos=2) + print("Subscribed to topic: {}".format(CHAMBER_TWO_TEMPERATURE_TOPIC)) + else: + print("Connected to unknwon broker!") + + +def set_temp_chamber_one(temp): + client_one.publish("chamber/"+CHAMBER_ONE_NAME+"/temp", struct.pack("d", temp), qos=2) + temperature_chamber_one_reached.clear() #Set Event flag to false and Block until set is called + +def set_temp_chamber_two(temp): + client_two.publish("chamber/"+CHAMBER_TWO_NAME+"/temp", struct.pack("d", temp), qos=2) + temperature_chamber_two_reached.clear() #Set Event flag to false and Block until set is called + +if __name__ == "__main__": + + SERVERNAME = "{}.ibr.cs.tu-bs.de".format(CHAMBER_ONE_NAME) + print(SERVERNAME) + client_one.connect( SERVERNAME, port=1883, keepalive=5 ) + client_one.user_data_set(CHAMBER_ONE_NAME) + client_one.on_connect = on_connect + client_one.on_message = on_message + client_one.on_disconnect = on_disconnect + client_one.loop_start() + + SERVERNAME = "{}.ibr.cs.tu-bs.de".format(CHAMBER_TWO_NAME) + print(SERVERNAME) + client_two.connect( SERVERNAME, port=1883, keepalive=5 ) + client_two.user_data_set(CHAMBER_TWO_NAME) + client_two.on_connect = on_connect + client_two.on_message = on_message + client_two.on_disconnect = on_disconnect + client_two.loop_start() + + #init arrays with temps for both chambers + chamber_one_temperature_values = [10, 25, 40] + chamber_two_temperature_values = [10, 25, 40] + + while True: + + + for chamber_one_numerator in range(0, len(temperature_values)): + for chamber_two_numerator in range(0, len(temperature_values)): + CHAMBER_ONE_TARGET_TEMP = temperature_values[chamber_one_numerator] + CHAMBER_TWO_TARGET_TEMP = temperature_values[chamber_two_numerator] + + print("Target temps for the chambers: {} | {}".format(CHAMBER_ONE_TARGET_TEMP, CHAMBER_TWO_TARGET_TEMP)) + + set_temp_chamber_one(CHAMBER_ONE_TARGET_TEMP) + set_temp_chamber_two(CHAMBER_TWO_TARGET_TEMP) + + #wait until both temperatures are reached + while not ( temperature_chamber_one_reached.wait() and temperature_chamber_one_reached.wait() ) + pass + + #wait for 30 Minutes + time.sleep( 60 * 15) + #invert array for chamber two + chamber_two_temperature_values = chamber_two_temperature_values[::-1] + + #invert array for chamber one + chamber_one_temperature_values = chamber_one_temperature_values[::-1] + +