-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurityCam.py
More file actions
144 lines (108 loc) · 4.06 KB
/
Copy pathsecurityCam.py
File metadata and controls
144 lines (108 loc) · 4.06 KB
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
import datetime
import os
import smtplib
import time
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import RPi.GPIO as GPIO
import picamera
from photoresistor import rc_time
# This method rotates the servo motor 3 times to take pictures in each direction
def turnMotor(servo, position):
if position == 1:
servo.ChangeDutyCycle(7.5)
print "This is 7.5"
elif position == 2:
servo.ChangeDutyCycle(9.5)
print "This is 9.5"
elif position == 3:
servo.ChangeDutyCycle(5.5)
print "This is 5.5"
else:
return False
time.sleep(.5)
return True
def sendGroupMsg(fileDir, email_to, email_from, password, mmsaddr, picdatetime, imagelist):
# Initialize and fill MIME message
msg = MIMEMultipart()
msg['Subject'] = "ENTRANCE DETECTED: PICTURES TAKEN"
msg['From'] = email_from
msg['To'] = email_to
msg.attach(MIMEText("SECURITY IMAGES TAKEN ON " + picdatetime, 'plain'))
# Set up connection to SMTP server
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo() # Check the smtp connection wit telnet session
mail.starttls() # Secure connection with SSL/TLS
mail.login(email_from, password)
# Attach images and send MMS
for i, img in enumerate(imagelist):
filestr = 'piccapture' + str(i) + '.jpg'
msg.attach(MIMEImage(img, name=os.path.basename(fileDir + filestr)))
# Send MMS with individual images
sendMMS(filestr, email_from, mmsaddr, picdatetime, img, mail)
# Send the email with grouped images
mail.sendmail(email_from, email_to, msg.as_string())
mail.quit()
def sendMMS(fileDir, email_from, mmsaddr, picdatetime, image, mail):
# Initialize and fill MIME message
msg = MIMEMultipart()
msg['Subject'] = "ENTRANCE DETECTED: PICTURES TAKEN"
msg['From'] = email_from
msg.attach(MIMEText("SECURITY IMAGES TAKEN ON " + picdatetime, 'plain'))
msg.attach(MIMEImage(image, name=os.path.basename(fileDir)))
# Send message on already opened connection
mail.sendmail(email_from, mmsaddr, msg.as_string())
def snapAndSend(fileDir, servomotor, camera):
imagelist = []
for i in range(1, 4):
if not turnMotor(servomotor, i):
print "Motor turning function failed!!!"
filestr = 'piccapture' + str(i) + '.jpg'
camera.capture(filestr)
img = open(fileDir + filestr, 'rb').read()
imagelist.append(img)
return imagelist, str(datetime.datetime.now())
def main():
# WE NEED TRY/CATCH STATEMENTS FOR USER INPUTS
# SET FILE DIRECTORY
fileDir = "/home/pi/Desktop/CSE4709_Project/NATHAN/"
# SETUP AND CONFIGURE PINS
GPIO.setmode(GPIO.BOARD)
print "What pin is the servomotor connected to?"
servopin = int(raw_input())
GPIO.setup(servopin, GPIO.OUT)
print "What pin is the photosensor connected to?"
photospin = int(raw_input())
# SET SERVO TO USER DEFINED PIN
smotor = GPIO.PWM(servopin, 50)
smotor.start(0)
# INITIALIZE PI CAMERA
camera = picamera.PiCamera()
# SET UP CONTACTS
email_from = "Insert From-email address here"
password = "Insert email password here"
print "What is the destination email?"
email_to = raw_input()
print "What is your number?"
mmsnumber = raw_input()
options = {1: "@vzwpix.com",
2: "@mms.att.net",
3: "@tmomail.net",
4: "@pm.sprint.com"}
print "What is your carrier?"
print "Choose one: \n 1. Verizon \n 2. AT&T \n 3. T-Mobile \n 4. Sprint"
mmscarrier = int(raw_input())
mmsaddr = mmsnumber + options[mmscarrier]
try:
while True:
lightlevel = rc_time(photospin)
if lightlevel < 200:
imagelist, picdatetime = snapAndSend(fileDir, smotor, camera)
sendGroupMsg(fileDir, email_to, email_from, password, mmsaddr, picdatetime, imagelist)
except KeyboardInterrupt:
# CLEAN UP
smotor.stop()
GPIO.cleanup()
if __name__ == '__main__':
main()