summaryrefslogtreecommitdiff
path: root/thermo.py
blob: dc3dba3cd86dbdefdf6fee62eb5c8262ed60270c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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)