-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.py
executable file
·68 lines (56 loc) · 1.73 KB
/
detect.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
#!/usr/bin/python
import RPi.GPIO as GPIO
from sparkpost import SparkPost
from twilio.rest import Client
import time
import os
# Connection Setup for SP and Twilio
sparkpostKey = os.environ.get('SPKEY')
twilioKey = os.environ.get('TWILIOKEY')
twilioAccount = os.environ.get('TWILIOACCT')
sparky = SparkPost(sparkpostKey)
twilio = Client(twilioAccount, twilioKey)
# Yay, you don't have water anymore
def sendHappyEmail():
sparky.transmissions.send(
recipients=['[email protected]'],
template='detector-email-happy'
)
def sendHappyText():
twilio.messages.create(
to='+14436059355',
from_='+14438927539',
body='Hooray! No more water in your basement'
)
# Boo, you have water detected
def sendSadEmail():
sparky.transmissions.send(
recipients=['[email protected]'],
template='detector-email-sad'
)
def sendSadText():
twilio.messages.create(
to='+14436059355',
from_='+14438927539',
body='Uh oh! We have detected that there is water in your basement'
)
# Generic callback handler for any state change
def inputCallback(channel):
if GPIO.input(channel):
print('No Water Detected')
sendHappyEmail()
sendHappyText()
else:
print('Water Detected')
sendSadEmail()
sendSadText()
# Setup the pin we are listening to
GPIO.setmode(GPIO.BOARD)
channel = 38
GPIO.setup(channel, GPIO.IN)
# Add our event handlers and callback (bouncetime is for preventing false positive changes)
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=1000)
GPIO.add_event_callback(channel, inputCallback)
# Infinite loop of detection, with a sleep to keep CPU down on the Pi
while True:
time.sleep(0.5)