From 378ac52c75fc575f464d0f407054fb238ad44d26 Mon Sep 17 00:00:00 2001 From: Asa Venton Date: Tue, 31 Mar 2020 20:04:06 +0100 Subject: Inital commit --- thermo.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ thermopi.png | Bin 0 -> 35279 bytes 2 files changed, 45 insertions(+) create mode 100644 thermo.py create mode 100644 thermopi.png 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) diff --git a/thermopi.png b/thermopi.png new file mode 100644 index 0000000..f781f56 Binary files /dev/null and b/thermopi.png differ -- cgit v1.2.3