-
Notifications
You must be signed in to change notification settings - Fork 0
/
gardener.py
73 lines (51 loc) · 2.19 KB
/
gardener.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
import RPi.GPIO as GPIO
import twitter
import os, sys
from time import sleep, strftime
def start_pump(duration):
''' run pump for a set interval '''
GPIO.output(PUMP, PUMP_ON)
sleep(duration) # keep pump running for set interval
GPIO.output(PUMP, PUMP_OFF)
def send_tweet(mention):
consumer_key = os.environ['twitter_consumer_key']
consumer_secret = os.environ['twitter_consumer_secret']
access_token_key = os.environ['twitter_access_token_key']
access_token_secret = os.environ['twitter_access_token_secret']
api = twitter.Api(consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token_key=access_token_key,
access_token_secret=access_token_secret)
tweet = '@{0} The plants have been watered at ⏰{1}'.format(mention, strftime("%H:%M:%S"))
# post tweet
api.PostUpdate(status=tweet)
def water_plants():
print('Watering plants at {0}'.format(strftime("%H:%M:%S")))
start_pump(duration=4) # run pump for set interval time
send_tweet(mention='ankitsejwal') # mention username to tag people in tweet
if __name__ == "__main__":
PUMP = 17
BUTTON = 18
PUMP_ON = 0
PUMP_OFF = 1
WATERING_TIME = '16:00'
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(PUMP, GPIO.OUT)
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.output(PUMP, PUMP_OFF) # initial state
print('Program started successfully ...\n')
try:
# infinite loop
while True:
button_pressed = not GPIO.input(BUTTON)
# water plants when button is pressed
if button_pressed:
water_plants()
# water plants at a fixed time everyday
if strftime('%H:%M') == WATERING_TIME:
water_plants()
sleep(60) # sleep for a minute
sleep(0.2) # slight delay in while loop
except KeyboardInterrupt:
sys.exit('\nProgram stopped successfully.')