summaryrefslogtreecommitdiff
path: root/thermo.py
diff options
context:
space:
mode:
Diffstat (limited to 'thermo.py')
-rw-r--r--thermo.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/thermo.py b/thermo.py
new file mode 100644
index 0000000..dc3dba3
--- /dev/null
+++ b/thermo.py
@@ -0,0 +1,45 @@
+# import modules
+import RPi.GPIO as GPIO
+import time
+
+# initialise variables
+device_file = '/sys/bus/w1/devices/28-00000400aaf8/w1_slave'
+# initialise LEDs array [GPIO_ID]
+leds = [18,17,27,22,23,24,25] # 32 16 8 4 2 1 0.5
+# initialise GPIO for LEDs
+GPIO.setmode(GPIO.BCM)
+GPIO.setwarnings(False)
+for led in leds:
+ GPIO.setup(led, GPIO.OUT)
+
+# read thermometer
+def read_temp_raw():
+ f = open(device_file, 'r')
+ lines = f.readlines()
+ f.close()
+ return lines
+
+def temp_updates():
+ lines = read_temp_raw()
+ # check that it hasn't failed to read in some way
+ while lines[0].strip()[-3:] != 'YES':
+ time.sleep(0.2)
+ lines = read_temp_raw()
+ # get temperature string from lines read from device
+ temp_string = float(str(lines[1].split("t=",1)[1]))
+ # round temp to nearest 0.5
+ temp = round(temp_string / 1000.0*2.0)/2.0
+ # set 0.5 LED value
+ if str(temp)[-1] == "5":
+ GPIO.output(leds[6], 1)
+ else:
+ GPIO.output(leds[6], 0)
+ # turn temp into binary, string, remove "0b", pad with 0s
+ temp = str(bin(int(temp))).replace("0b","").zfill(6)
+ # set LED states
+ for i in range(6):
+ GPIO.output(leds[i], int(temp[i]))
+
+while True:
+ temp_updates()
+ time.sleep(1)