forked from brycesub/silvia-pi
-
Notifications
You must be signed in to change notification settings - Fork 2
/
heat.py
77 lines (68 loc) · 2.06 KB
/
heat.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
#!/usr/bin/python
from time import sleep, time
from datetime import datetime, timedelta
import RPi.GPIO as GPIO
from config import config
he_pins = config.he_pins
max_sleep = config.sample_time
import logging
logger = logging.getLogger('silvia.heat')
class Heat(object):
def __init__(self, state):
self._state = state
self.setup()
def setup(self):
GPIO.setmode(GPIO.BCM)
for pin in he_pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
def cleanup(self):
for pin in he_pins:
GPIO.output(pin, 0)
GPIO.cleanup()
def run(self):
heating = False
logger.info('Heat control loop started')
while True:
# Check if snooze is over
if self._state['snoozeon'] == True :
now = datetime.now()
dt = datetime.strptime(self._state['snooze'],'%H:%M')
if dt.hour == now.hour and dt.minute == now.minute :
self._state['snoozeon'] = False
# Sleep if snoozing
if self._state['snoozeon'] or not self._state['on']:
self._state['heating'] = False
for pin in he_pins:
GPIO.output(pin,0)
sleep(1)
elif time() < self._state['boost']:
self._state['heating'] = True
for pin in he_pins:
GPIO.output(pin, 1)
sleep(1)
else:
avgpid = self._state['pidval']
# Full heat
if avgpid >= 100 :
self._state['heating'] = True
for pin in he_pins:
GPIO.output(pin, 1)
sleep(max_sleep)
# Heat for avgpid/100 seconds, sleep the rest of the second
elif avgpid > 0 and avgpid < 100:
self._state['heating'] = True
for pin in he_pins:
GPIO.output(pin, 1)
on_time = max_sleep * (avgpid/100.)
sleep(on_time)
for pin in he_pins:
GPIO.output(pin, 0)
sleep(max_sleep - on_time)
self._state['heating'] = False
# Don't heat
else:
for pin in he_pins:
GPIO.output(pin, 0)
self._state['heating'] = False
sleep(max_sleep)