-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadHCSR501.py
85 lines (76 loc) · 2.7 KB
/
ReadHCSR501.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
"""
Reads the HC-SR501 sensor and takes a photo if sensor detects movement.
Sends photo as email to alert email addresses.
"""
from gpio_96boards import GPIO
import time
import json
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import SimpleCV
import socket
def send_email(recipients, subject, message, img_path):
img_data = open(img_path, 'rb').read()
try:
pwd = None
with open('secrets.json', 'r') as secretsfile:
secrets = json.load(secretsfile)
pwd = secrets['smtp_pwd']
fromaddr = "[email protected]"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = ", ".join(recipients)
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
image = MIMEImage(img_data, name='snapshot.png')
msg.attach(image)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, pwd)
text = msg.as_string()
server.sendmail(fromaddr, recipients, text)
server.quit()
except socket.gaierror:
print("Network down, can't send email...")
pin = GPIO.gpio_id('GPIO-B')
pins = (
(pin, 'in'),
)
cam = SimpleCV.Camera()
last_detection = True
detection_enabled = True
IMAGE_PATH = "/home/linaro/motion_detection.png"
with GPIO(pins) as gpio:
while True:
# If motion detection enabled AND pin is high, take picture and send to alert recipients
with open('settings.json', 'r') as settingsfile:
data = json.load(settingsfile)
if not data['motion_detection']:
if detection_enabled:
print ("Motion detection is disabled")
detection_enabled = False
time.sleep(2)
continue
else:
if not detection_enabled:
print("Motion detection is enabled")
detection_enabled = True
pinValue = gpio.digital_read(pin)
if pinValue:
print ("Motion detected!")
img = cam.getImage()
img.save(IMAGE_PATH)
send_email(
data['recipients'],
"Motion detected!",
"Motion was detected, see image attachment!",
IMAGE_PATH)
last_detection = True
sleep(10)
else:
if last_detection:
last_detection = False
print ("No motion detected...")
time.sleep(0.5)