-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaterer.py
63 lines (53 loc) · 1.7 KB
/
waterer.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
import RPi.GPIO as GPIO
import time
import datetime
import file_manager
from os import environ
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.IN) # button
GPIO.setup(11, GPIO.OUT, initial=GPIO.LOW) # led
GPIO.setup(7, GPIO.OUT, initial=GPIO.HIGH) # rele 1
GPIO.setup(13, GPIO.OUT, initial=GPIO.HIGH) # rele 2
def init():
print("WATERER::initializing...")
GPIO.output(7, GPIO.HIGH)
GPIO.output(13, GPIO.HIGH)
now = datetime.datetime.now()
while (datetime.datetime.now() - now).seconds < 3:
toggle_led()
print("WATERER::ready!")
def toggle_led():
GPIO.output(11, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(11, GPIO.LOW)
time.sleep(0.5)
def shutdown():
GPIO.cleanup()
print("WATERER::goodbye!")
def is_button_pressed():
return (GPIO.input(3) == 0)
def isnt_stop_requested():
return (environ.get('IS_WATERING') == 'True')
def water(source):
environ['IS_WATERING'] = 'True'
print("Water IS_WATERING = True")
file_manager.write(source)
print("WATERER::triggered via " + source)
print("WATERER::start watering area 1...")
time_area_1 = int(environ.get('TIME_AREA_1'))
now = datetime.datetime.now()
GPIO.output(7, GPIO.LOW)
while isnt_stop_requested() and ((datetime.datetime.now() - now).seconds < time_area_1):
toggle_led()
GPIO.output(7, GPIO.HIGH)
print("WATERER::stop area 1 and start watering area 2...")
time_area_2 = int(environ.get('TIME_AREA_2'))
now = datetime.datetime.now()
GPIO.output(13, GPIO.LOW)
while isnt_stop_requested() and ((datetime.datetime.now() - now).seconds < time_area_2):
toggle_led()
GPIO.output(13, GPIO.HIGH)
environ['IS_WATERING'] = 'False'
print("Water IS_WATERING = False")
print("WATERER::stop watering!")