/
bedroom_bonus.py
182 lines (158 loc) · 4.64 KB
/
bedroom_bonus.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
#
# I have a Display-O-Tron 3000 connected to the Raspberry PI from my bedroom.
# https://shop.pimoroni.com/products/displayotron-3000
#
# This is what I'm using to see the status of the room (temperature, etc).
# Initially I used this to control the relay from the Attic, but I added more Raspberry PIs
# and commented out the functions that were actually starting/stopping the heating. The rest
# of the functionality remains.
#
# I didn't define the left and right buttons, but I'm thinking to program them so I could get the stats
# from other rooms. Depending on free time, I might maintain this file or not.
#
print("""
Press CTRL+C to exit.
""")
import Adafruit_DHT
import dothat.touch as j
import dothat.lcd as l
import dothat.backlight as b
import signal
import socket
import time
j.enable_repeat(True)
dtemp=float(21.00) # temperature that i want to be in the room
rtemp=float(20.00) # read temperature from sensor
heatc=0.0 # count to light or dim the vertical leds
heat="OFF" # default heating
display_status="ON"
room="Bedroom: "
def status_heating():
s = socket.socket()
s.connect(("192.168.1.2",8001))
s.send('status')
msg=s.recv(1024)
return str(msg)
def start_heating():
print time.strftime("%d-%m-%Y %H:%M:%S") + ": I read " + str(rtemp) + "*C and the room is set for " + str(dtemp) + "*C. I'm trying to start the heating..."
status=status_heating()
if status == "1":
s = socket.socket()
s.connect(("192.168.1.2",8001))
#s.send('start')
s.close()
print time.strftime("%d-%m-%Y %H:%M:%S") + ": I've sent to command to start the heating..."
else:
print "I've read: " + str (status)
print time.strftime("%d-%m-%Y %H:%M:%S") + ": Nevermind, the termostat is already on..."
def stop_heating():
print time.strftime("%d-%m-%Y %H:%M:%S") + ": I read " + str(rtemp) + "*C and the room is set for " + str(dtemp) + "*C. I'm trying to stop the heating..."
status=status_heating()
if status == "0":
s = socket.socket()
s.connect(("192.168.1.2",8001))
#s.send('stop')
s.close()
print time.strftime("%d-%m-%Y %H:%M:%S") + ": I've sent the command to stop the heating..."
else:
print "I've read " + str (status)
print time.strftime("%d-%m-%Y %H:%M:%S") + ": Nevermind, the termostat is already off..."
def get_temp():
global rtemp
humidity, temperature = Adafruit_DHT.read_retry(22, 5)
if humidity is not None and temperature is not None:
rtemp=float(format(temperature, '.2f'))
time.sleep(1)
def count_heat():
global heat,heatc,dtemp,rtemp
get_temp()
if (dtemp-rtemp) > 0:
if heatc < 1:
heatc=heatc+0.01
print time.strftime("%d-%m-%Y %H:%M:%S") + ": Counting heating, currently " + str(heatc) + "/1.0"
else:
heatc=1.0
heat="ON"
start_heating()
else:
if heatc > 0:
heatc=heatc-0.01
print time.strftime("%d-%m-%Y %H:%M:%S") + ": Counting heating, currently " + str(heatc) + "/0.0"
else:
heatc=0.0
heat="OFF"
stop_heating()
if display_status == "ON":
b.set_graph(heatc)
else:
b.set_graph(0)
def default_display():
global dtemp, rtemp, heat, display_status, heatc
l.clear()
if display_status == "ON":
b.rgb(128,128,128)
else:
b.rgb(0,0,0)
l.set_cursor_position(0,0)
l.write(str(room) + str(dtemp) + chr(223) + 'C')
l.set_cursor_position(0,1)
l.write("Heating: " + str(heat))
l.set_cursor_position(0,2)
x= "Now:" + str(rtemp) + chr(223) + 'C ' + time.strftime("%H:%M")
l.write(x)
count_heat()
time.sleep(1)
@j.on(j.UP)
def handle_up(ch, evt):
global dtemp, rtemp, display_status
l.clear()
b.rgb(128, 0, 0) #red
dtemp=dtemp+1
l.clear()
l.set_cursor_position(0,1)
l.write(str(room) + str(dtemp) + chr(223) + 'C')
time.sleep(1)
display_status="ON"
default_display()
@j.on(j.DOWN)
def handle_down(ch, evt):
global dtemp, rtemp, display_status
l.clear()
b.rgb(128, 128, 0) #blue
dtemp=dtemp-1
l.clear()
l.set_cursor_position(0,1)
l.write(str(room) + str(dtemp) + chr(223) + 'C')
time.sleep(1)
display_status="ON"
default_display()
@j.on(j.LEFT)
def handle_left(ch, evt):
print("Left pressed!")
l.clear()
b.rgb(0, 0, 255)
l.write("Leftie left left!")
@j.on(j.RIGHT)
def handle_right(ch, evt):
print("Right pressed!")
l.clear()
b.rgb(0, 255, 255)
l.write("Rightie tighty!")
@j.on(j.BUTTON)
def handle_button(ch, evt):
global display_status
display_status="ON"
time.sleep(1)
default_display()
@j.on(j.CANCEL)
def handle_cancel(ch, evt):
global display_status
display_status="OFF"
time.sleep(1)
default_display()
# Prevent the script exiting!
while 1:
default_display()
time.sleep(1)
#signal.pause()