-
Notifications
You must be signed in to change notification settings - Fork 0
/
brewer.py
executable file
·84 lines (71 loc) · 2.57 KB
/
brewer.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python3
import argparse
from argparse import RawTextHelpFormatter
import os
import RPi.GPIO as GPIO
import subprocess
import sys
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
parser = argparse.ArgumentParser(
description="A brewing temperatur controller. \n\
Reads the temperatur from the sensors, turns on/off the heating element to reach the target temperatur. \n\
Prints temperaturs without any options",
formatter_class=RawTextHelpFormatter)
parser.add_argument("-d", "--duration", help="time to hold the temp in minutes")
parser.add_argument("-t", "--temp", help="target temp")
args = parser.parse_args()
#GPIO PIN of the heating element
GPIO_HEAT = 12
#Temp offset
TEMP_OFFSET = 1.0
temp_sensors = []
temp_sensors.append("/sys/bus/w1/devices/28-02129245142c/w1_slave")
temp_sensors.append("/sys/bus/w1/devices/28-021292453eba/w1_slave")
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_HEAT, GPIO.OUT)
#duration in seconds
if args.duration:
duration = 60 * int(args.duration)
try:
start_time = time.monotonic()
while True:
avg_temp = 0
i = 0
print("\r", end='')
for sensor in temp_sensors:
sensor_file = open(sensor)
output = sensor_file.read()
sensor_file.close()
secondline = output.split("\n")[1]
temp = float(secondline.split("t=")[1]) / 1000 + TEMP_OFFSET
avg_temp += temp
print("t{0}: {1}\t".format(i, str(temp)), end='')
i += 1
avg_temp = avg_temp / len(temp_sensors)
print("avg: {0}".format(str(avg_temp)), end='')
if args.temp:
print("\t target temp: {0}".format(args.temp), end='')
if(round(float(args.temp),1) > round(avg_temp,1)):
GPIO.output(GPIO_HEAT, GPIO.HIGH)
else:
GPIO.output(GPIO_HEAT, GPIO.LOW)
if GPIO.input(GPIO_HEAT):
print(u"\t Heater:\033[1;31;402m\u25CF \033[0m", end='')
else:
print(u"\t Heater:\u2716", end='')
print("\t ", str(subprocess.getoutput("date").strip()), end='')
if args.duration:
seconds_elapsed = time.monotonic() - start_time
print("\t ", "Minutes left: ", int(args.duration) - int(seconds_elapsed/60), end='')
if (time.monotonic() - start_time) > duration:
print("\nTime is up")
break
sys.stdout.flush()
time.sleep(5)
except KeyboardInterrupt:
print("\nKeyboard interupt!")
finally:
GPIO.output(GPIO_HEAT, GPIO.LOW)