diff options
author | Asa Venton <asav1410@gmail.com> | 2020-03-31 20:04:06 +0100 |
---|---|---|
committer | Asa Venton <asav1410@gmail.com> | 2020-03-31 20:04:06 +0100 |
commit | 378ac52c75fc575f464d0f407054fb238ad44d26 (patch) | |
tree | 45f013b977df7975a4d289add1fcda4b22f3950b /thermo.py | |
parent | 4f794c9c9d83340e93052586880b4e43434982f0 (diff) |
Inital commit
Diffstat (limited to 'thermo.py')
-rw-r--r-- | thermo.py | 45 |
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) |