Raspberry pi home security monitor
Iteration 1 - basic functionality
This is first, simplest iteration of the project that has following features:
- motion sensor to detect motion and trigger the camera
- camera takes a picture and picture is stored locally with a timestamp as the name of the file
- the picture is then emailed to the house owner
import RPi.GPIO as GPIO import time import os import datetime from smtplib import SMTP_SSL as SMTP import sys from email.mime.text import MIMEText from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from os.path import basename from email.utils import COMMASPACE, formatdate sensor = 4 GPIO.setmode(GPIO.BCM) GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN) previous_state = False current_state = False def send_email(f): send_from='[email protected]' send_to='[email protected]' subject='motion notice' text='see attached' msg = MIMEMultipart( From=send_from, To=COMMASPACE.join(send_to), Date=formatdate(localtime=True), Subject=subject ) msg.attach(MIMEText(text)) with open(f, "rb") as fil: msg.attach(MIMEApplication( fil.read(), Content_Disposition='attachment; filename="%s"' % basename(f), Name=basename(f) )) try: conn = SMTP('smtp.mail.yahoo.com') conn.set_debuglevel(False) conn.login('[email protected]', 'password_here') try: conn.sendmail(send_from, send_to, msg.as_string()) finally: conn.close() except Exception as exc: sys.exit("email failed: {}".format(exc)) def take_photo(): now = datetime.datetime.now() os.system("raspistill -w 640 -h 480 -n -t 100 -q 10 -e jpg -th none -o 'pictures/" + now.isoformat() +".jpg'") send_email('pictures/' + now.isoformat() +'.jpg') while True: time.sleep(0.01) previous_state = current_state current_state = GPIO.input(sensor) if current_state != previous_state: new_state = "HIGH" if current_state else "LOW" print("GPIO pin %s is %s" % (sensor, new_state)) if new_state=="HIGH": take_photo()