├── picamera.jpg ├── README.md └── PIR_Motion_Camera_Email.py /picamera.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WillPhillipsCVdemo/Raspberry-Pi-Camera-Motion-Detection./HEAD/picamera.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Raspberry-Pi-Camera-Motion-Detection. 2 | Using Python code with a Raspberry-Pi, PIR motion and a Picamera, you can create a device which will email you a snapshot image when someone enters your room. 3 | -------------------------------------------------------------------------------- /PIR_Motion_Camera_Email.py: -------------------------------------------------------------------------------- 1 | import RPi.GPIO as GPIO 2 | import time 3 | import datetime 4 | import picamera 5 | import os 6 | import smtplib 7 | from email import encoders 8 | from email.mime.base import MIMEBase 9 | from email.mime.multipart import MIMEMultipart 10 | 11 | 12 | camera = picamera.PiCamera() 13 | GPIO.setmode(GPIO.BCM) 14 | 15 | GPIO.setup(23, GPIO.IN) #PIR 16 | GPIO.setup(24, GPIO.OUT) #BUzzer 17 | 18 | ''' 19 | ts = time.time() 20 | st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') 21 | ''' 22 | 23 | 24 | 25 | COMMASPACE = ', ' 26 | 27 | def Send_Email(image): 28 | sender = '###YOUREMAIL###' 29 | gmail_password = '###YOURPASSWORD###' 30 | recipients = ['##YOURRECIPENTEMAIL###'] 31 | 32 | # Create the enclosing (outer) message 33 | outer = MIMEMultipart() 34 | outer['Subject'] = 'Attachment Test' 35 | outer['To'] = COMMASPACE.join(recipients) 36 | outer['From'] = sender 37 | outer.preamble = 'You will not see this in a MIME-aware mail reader.\n' 38 | 39 | # List of attachments 40 | attachments = [image] 41 | 42 | # Add the attachments to the message 43 | for file in attachments: 44 | try: 45 | with open(file, 'rb') as fp: 46 | msg = MIMEBase('application', "octet-stream") 47 | msg.set_payload(fp.read()) 48 | encoders.encode_base64(msg) 49 | msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file)) 50 | outer.attach(msg) 51 | except: 52 | print("Unable to open one of the attachments. Error: ", sys.exc_info()[0]) 53 | raise 54 | 55 | composed = outer.as_string() 56 | 57 | # Send the email 58 | try: 59 | with smtplib.SMTP('smtp.gmail.com', 587) as s: 60 | s.ehlo() 61 | s.starttls() 62 | s.ehlo() 63 | s.login(sender, gmail_password) 64 | s.sendmail(sender, recipients, composed) 65 | s.close() 66 | print("Email sent!") 67 | except: 68 | print("Unable to send the email. Error: ", sys.exc_info()[0]) 69 | raise 70 | 71 | 72 | 73 | try: 74 | time.sleep(2) # to stabilize sensor 75 | 76 | 77 | while True: 78 | ##Timeloop 79 | ts = time.time() 80 | st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') 81 | if GPIO.input(23): 82 | ##If loop 83 | GPIO.output(24, True) 84 | time.sleep(0.5) #Buzzer turns on for 0.5 sec 85 | print("Motion Detected at {}".format(st)) 86 | ##Adds timestamp to image 87 | camera.capture('image_Time_{}.jpg'.format(st)) 88 | image = ('image_Time_{}.jpg'.format(st)) 89 | Send_Email(image) 90 | time.sleep(2) 91 | GPIO.output(24, False) 92 | time.sleep(5) #to avoid multiple detection 93 | 94 | time.sleep(0.1) #loop delay, should be less than detection delay 95 | 96 | except: 97 | GPIO.cleanup() 98 | 99 | 100 | 101 | --------------------------------------------------------------------------------