-
Notifications
You must be signed in to change notification settings - Fork 0
/
rainmaker.py
334 lines (317 loc) · 9.65 KB
/
rainmaker.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import logging
from datetime import datetime, timedelta, time
import RPi.GPIO as GPIO
import threading
import time
from smbus2 import SMBus
#import smbus
from lcd_i2c import lcd_string, lcd_init
#logging, logs all events to the file rainmaker.log
logging.basicConfig(filename='/home/pi/rainmaker.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s: %(message)s')
#GPIO, use this section to change pin numbers if your wiring differs from mine
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
A = 21
B = 20
C = 19
D = 16
RedButton = 22
GreenButton = 24
RedLed = 23
GreenLed = 25
PumpRelais = 4
clk = 13
dt = 12
SW = 6
GPIO.setup(SW, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(A, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(B, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(D, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(RedButton, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(GreenButton, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(RedLed,GPIO.OUT)
GPIO.setup(GreenLed,GPIO.OUT)
GPIO.setup(PumpRelais,GPIO.OUT)
#LCD
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
#lcd_init()
def format_timedelta(td):
hours, remainder = divmod(td.total_seconds(), 3600)
minutes, seconds = divmod(remainder, 60)
hours, minutes, seconds = int(hours), int(minutes), int(seconds)
if hours < 10:
hours = '0%s' % int(hours)
if minutes < 10:
minutes = '0%s' % minutes
if seconds < 10:
seconds = '0%s' % seconds
return '%s:%s' % (hours, minutes)
def pump_on_led(): # The LED's for pump on indefinately, started as a thread in pump_on
GPIO.output(RedLed,GPIO.HIGH)
while True:
GPIO.output(GreenLed,GPIO.LOW)
time.sleep(0.5)
GPIO.output(GreenLed,GPIO.HIGH)
time.sleep(0.5)
if stop_threads:
break
def timer_menu_led(stop_event): # The LED's for pump on indefinately, started as a thread in pump_on
GPIO.output(GreenLed,GPIO.LOW)
while not stop_event.is_set():
GPIO.output(RedLed,GPIO.HIGH)
time.sleep(0.2)
GPIO.output(RedLed,GPIO.LOW)
time.sleep(0.8)
logging.debug('Stop timer_menu_led')
def pump_on_lcd(): # The LCD for pump on indefinately, started as a thread in pump_on
while True:
Pumptime = datetime.now() - TimeOn
lcd_string("RAINMAKER "+datetime.now().strftime('%H:%M'),LCD_LINE_1)
lcd_string("Pomp aan "+str(format_timedelta(Pumptime)),LCD_LINE_2)
if stop_threads:
break
def pump_on_timer_led(): # The LED's for pump on timer, started as a thread in pump_on_timer
while True:
GPIO.output(GreenLed,GPIO.LOW)
GPIO.output(RedLed, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(GreenLed,GPIO.HIGH)
GPIO.output(RedLed,GPIO.LOW)
time.sleep(0.5)
if stop_threads:
break
def pump_on_timer_lcd(): # The LCD for pump on timer, started as a thread in pump_on_timer
while True:
lcd_string("RAINMAKER "+datetime.now().strftime('%H:%M'),LCD_LINE_1)
if datetime.now() < TimeOn:
lcd_string("Pomp aan: "+TimeOn.strftime('%H:%M'),LCD_LINE_2)
time.sleep(2)
lcd_string("Pomp uit: "+TimeOff.strftime('%H:%M'),LCD_LINE_2)
time.sleep(2)
if stop_threads:
break
elif datetime.now() >= TimeOn:
lcd_string("Pomp uit: "+TimeOff.strftime('%H:%M'),LCD_LINE_2)
time.sleep(0.5)
if stop_threads:
break
def pump_on(): # Turns the pump on
logging.debug('pump_on started')
global stop_threads
stop_threads = False
t1 = threading.Thread(target = pump_on_led)
t1.start()
t2 = threading.Thread(target = pump_on_lcd)
t2.start()
TimeOn = datetime.now()
while True:
GPIO.output(PumpRelais,GPIO.HIGH)
if GPIO.input(B):
logging.debug('Input B')
logging.info('Pump turned off remotely, on-time: %s', (datetime.now() - TimeOn))
GPIO.output(PumpRelais,GPIO.LOW)
stop_threads = True
t1.join()
t2.join()
input()
break
elif GPIO.input(RedButton):
logging.debug('Input RedButton')
OnTime = (datetime.now() - TimeOn)
logging.info('Pump turned off locally, on-time: %s', OnTime)
GPIO.output(PumpRelais,GPIO.LOW)
stop_threads = True
t1.join()
t2.join()
input()
break
def pump_on_timer(): # Turns on the pump and turns it off after a set time
logging.debug('pump_on_timer started, StartTime = %s, EndTime = %s', TimeOn, TimeOff)
global stop_threads
stop_threads = False
# TimeOn = datetime.now()
# global TimeOff
# TimeOff = datetime.now() + timedelta(minutes=1) #For testing purposes
t1 = threading.Thread(target = pump_on_timer_led)
t1.start()
t2 = threading.Thread(target = pump_on_timer_lcd)
t2.start()
while True:
if TimeOn > datetime.now():
if GPIO.input(B):
logging.debug('Input B')
logging.info('Timer interrupted remotely')
stop_threads = True
t1.join()
t2.join()
input()
break
elif GPIO.input(RedButton):
logging.debug('Input RedButton')
logging.info('Timer interrupted locally')
stop_threads = True
t1.join()
t2.join()
input()
break
elif TimeOn <= datetime.now():
GPIO.output(PumpRelais,GPIO.HIGH)
if GPIO.input(B):
logging.debug('Input B')
logging.info('Timer interrupted remotely, on-time: %s', (datetime.now() - TimeOn))
GPIO.output(PumpRelais,GPIO.LOW)
stop_threads = True
t1.join()
t2.join()
input()
break
elif GPIO.input(RedButton):
logging.debug('Input RedButton')
logging.info('Timer interrupted locally, on-time: %s', (datetime.now() - TimeOn))
GPIO.output(PumpRelais,GPIO.LOW)
stop_threads = True
t1.join()
t2.join()
input()
break
elif datetime.now() >= TimeOff:
logging.info('Pump turned off by timer, on-time: %s', (datetime.now() - TimeOn))
GPIO.output(PumpRelais,GPIO.LOW)
stop_threads = True
t1.join()
t2.join()
input()
break
def timer_menu():
logging.debug('Timer menu started')
global stop
stop = threading.Event()
global t1
t1 = threading.Thread(target = timer_menu_led, args=(stop,))
t1.start()
now = datetime.now()
global TimeOn
TimeOn = now + timedelta(minutes=-now.minute,seconds=-now.second,microseconds=-now.microsecond) + timedelta(minutes=(int(now.minute/15)*15)+15)
clkLastState = GPIO.input(clk)
lcd_string("RAINMAKER "+datetime.now().strftime('%H:%M'),LCD_LINE_1)
lcd_string("Start time "+TimeOn.strftime("%H:%M"),LCD_LINE_2)
while True:
clkState = GPIO.input(clk)
dtState = GPIO.input(dt)
if clkState != clkLastState:
time.sleep(0.02)
if dtState != clkState:
TimeOn += timedelta(minutes=15)
else:
TimeOn -= timedelta(minutes=15)
lcd_string("Start time "+TimeOn.strftime("%H:%M"),LCD_LINE_2)
print ("Start time = "+TimeOn.strftime("%H:%M"))
clkLastState = clkState
time.sleep(0.02)
elif GPIO.input(SW) == False:
time.sleep(0.1)
logging.debug('Input SW')
logging.info('Start Time set at '+ TimeOn.strftime("%H:%M"))
timer_menu_end()
break
elif GPIO.input(RedButton):
logging.debug('Input RedButton')
logging.info('Timer menu closed without setting a timer')
stop.set()
t1.join()
input()
break
def timer_menu_end():
logging.debug('Timer menu end started')
global TimeOff
TimeOff = TimeOn + timedelta(minutes=30)
clkLastState = GPIO.input(clk)
lcd_string("Start time "+TimeOn.strftime("%H:%M"),LCD_LINE_1)
lcd_string("End time "+TimeOff.strftime("%H:%M"),LCD_LINE_2)
while True:
clkState = GPIO.input(clk)
dtState = GPIO.input(dt)
if clkState != clkLastState:
time.sleep(0.02)
if dtState != clkState:
TimeOff += timedelta(minutes=15)
else:
TimeOff -= timedelta(minutes=15)
lcd_string("End time "+TimeOff.strftime("%H:%M"),LCD_LINE_2)
print ("End time = "+TimeOff.strftime("%H:%M"))
clkLastState = clkState
time.sleep(0.02)
elif GPIO.input(SW) == False:
time.sleep(0.1)
logging.debug('Input SW')
logging.info('End time set at '+ TimeOff.strftime("%H:%M"))
stop.set()
t1.join()
pump_on_timer()
break # Removed break to see if RAINMAKER wouldn't appear ater pushing button while nothing else was configured
elif GPIO.input(RedButton):
logging.debug('Input RedButton')
logging.info('Timer menu closed without setting a timer')
stop.set()
t1.join()
input()
break
def input():
logging.debug('Input started')
GPIO.output(GreenLed,GPIO.HIGH)
GPIO.output(RedLed,GPIO.LOW)
global TimeOn
global TimeOff
lcd_string("Pomp standby..",LCD_LINE_2)
while True:
lcd_string("RAINMAKER "+datetime.now().strftime('%H:%M'),LCD_LINE_1)
if GPIO.input(A):
logging.debug('Input A')
logging.info('Pump turned on remotely')
TimeOn = datetime.now()
pump_on()
break
elif GPIO.input(C):
logging.debug('Input C')
TimeOn = datetime.now()
TimeOff = datetime.now() + timedelta(minutes = 60)
logging.info('Pump turned on remotely, timeOff set at: %s', TimeOff)
pump_on_timer()
break
elif GPIO.input(D):
logging.debug('Input D')
TimeOn = datetime.now()
TimeOff = datetime.now() + timedelta(minutes = 120)
logging.info('Pump turned on remotely, timeOff set at: %s', TimeOff)
pump_on_timer()
break
elif GPIO.input(SW) == False:
logging.debug('Input SW(rotenc)')
time.sleep(0.1)
timer_menu()
break
elif GPIO.input(GreenButton):
logging.debug('Input GreenButton')
logging.info('Pump turned on locally')
TimeOn = datetime.now()
pump_on()
break
if __name__ == "__main__":
try:
logging.info('rainmaker started')
lcd_init()
input()
# pump_on_timer() # For LCD testing purposes
except RuntimeError as error:
print(error.args[0])
logging.exception(error.args[0])
GPIO.input(RedLed,HIGH)
except KeyboardInterrupt:
print("\nExiting application\n")
# exit the applications
logging.info('rainmaker stopped')
GPIO.cleanup()