From 69fa026a64ecc8f4708ab965ebf296c6449b4526 Mon Sep 17 00:00:00 2001
From: Torben Petersen <petersen@ibr.cs.tu-bs.de>
Date: Thu, 31 Jan 2019 13:09:24 +0100
Subject: [PATCH] Changes Chamber Adress to lctc-c01 and adds ctach while
 trying to set temp

---
 examples/chamber.py | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/examples/chamber.py b/examples/chamber.py
index ca232a3..961b2b1 100644
--- a/examples/chamber.py
+++ b/examples/chamber.py
@@ -9,47 +9,54 @@ import socket
 import struct
 from threading import Event
 
-CHAMBER_NAME = "lctc-c0X"
-TARGET_TEMPERATURE = 10.0
+CHAMBER_NAME = "lctc-c01"
+TARGET_TEMPERATURE = 5.0
 DELTA = 0.5
 TIMEOUT = 60.0
 CHAMBER_TEMPERATURE_TOPIC = "sht21/"+CHAMBER_NAME+"/temp"
-temperature_reached = Event()
+temperature_reached = Event() #set event flag to flase
 
 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()
+            temperature_reached.set() #set the event flag to true
     else:
-        print( "[on_message] {} {}".format(msg.payload, msg.topic) )
+        print("[on_message] {} {}".format(msg.payload, msg.topic) )
 
 def on_disconnect(client, userdata, rc):
-    print(" [on_disconnect] {}".format( 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)
-    temperature_reached.clear()
+    temperature_reached.clear() #Set Event flag to false and Block until set is called
 
 if __name__ == "__main__":
-    client.connect( "localhost", port=1883, keepalive=5 )
+    client.connect( "lctc-c01.dyn.ibr.cs.tu-bs.de", port=1883, keepalive=5 )
     client.on_connect = on_connect
     client.on_message = on_message
     client.on_disconnect = on_disconnect
     client.loop_start()
 
     while True:
-        set_temp( TARGET_TEMPERATURE )
-        # 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
+        try:
+                set_temp( TARGET_TEMPERATURE )
+                # Wait until temperature has been reached
+                if not temperature_reached.wait(TIMEOUT): #wait until flag is set or TIMEOUT
+                    print("timeout reached.")
+                else:
+                    print("reached Temp")
+                # Do something here for 1 minute
+                print("Entering sleep")
+                time.sleep(60.0)      
+                # Increase target temperature
+                #TARGET_TEMPERATURE += 1.0
+        except KeyboardInterrupt:
+                set_temp( None )
 
-- 
GitLab