LuckyPants
  • Home
  • Poems
  • Python
    • Python Interview Prep Notes for Students >
      • Merge Intervals Leetcode
      • Merge k Sorted Lists
    • Python Setup >
      • Pep 8 style guide
    • Lecture 1 - Basics
    • Lecture 2 - Loops, Lists >
      • Homework 2 Program 1 Step By Step
      • Homweork 2 Shuffle String Program
    • Lecture 3 - structures, packing, string functions >
      • Homework
      • Additional Exercises - you do not need to submit these
    • Lecture 4 - Dictionaries, Functions, Modules
    • Lecture 5 - functions, variable scope, modules >
      • Previous Exams
    • Lecture 6 - CLI, Files, Regex, pickle, shelve
    • Lecture 7 - capturing web content >
      • Processing XML data
    • Midterm Prep >
      • Programming assignment prep
    • Lecture 8 - Exceptions, Classes, Inheritance >
      • Exceptions Recording
      • Multiple Except Clauses Recording
      • Eating an Exception Recording
      • finally and else clause recordings
      • Exception Stack Trace Recording
      • exit() and CTRL+C
      • Class Variables Recording
      • Static Methods and Class Methods Recording
      • Inheritance Recordings
    • Section 9 - db, flask, pip >
      • Accessing Databases
      • HTTP Server
      • Web Applications
      • Finding and installing additional Libraries with pip
      • Web Services with Flask
      • Calling Web Services from Python
      • Placing your application on the web
    • Section 10 - flask, grpc, venv, docker >
      • Flask based Web Applications
      • gRPC
      • Packaging, Dependencies and Virtual Environments
      • Dockerized Python Application
    • Section 11 - Django, GUI >
      • Cross Site Scripting XSS and markupsafe
      • Django >
        • Cross Site Request Forgery CSRF
      • tkinter - frame and button
      • Toplevel
      • Frame
      • Mouse and Keyboard Events
      • Canvas
      • Menu
      • Radiobuttons and Checkboxes
      • Entry
      • ImageMagic
    • Lecture 12 - WebSockets, System Monitoring, PyGame >
      • Web Sockets
      • PyGame Platformer
    • Lecture 13 - Data Structure and Algorithms >
      • Arrays and Strings
      • Dictionaries
      • Linked Lists
      • Trees
      • Stack and Queue
      • Sets
      • Merge Sort
      • Coverage Reports
    • Lecture 14 - Subprocesses, Threads, Multiprocessing >
      • Graphs
      • Singleton Design Pattern
      • Subprocesses and Multithreading
      • Jupiter Notebooks
      • Pandas Operations
    • Extra >
      • Raspberry Pi
      • Raspberry PI home security
      • OpenCV and template matching
      • Compare SQLite DB's in Python
      • Natural Language Processing and nltk
      • Hackathon Projects
  • SaaS
    • Java Interview Prep Notes for Students
    • Setup >
      • SaaS and Cloud Computing
      • Eclipse set up
      • Building web application in Eclipse using Tomcat
    • Section2 >
      • Maven Intro
      • Set up a simple web app and deploy on Heroku
      • Add Jersey Dependencies
      • Dissecting our first REST service
    • Section3 >
      • Jackson intro
      • @POST, @PUT, @DELETE
      • Your Project
    • Section4 >
      • Rules of Injection
      • Root Resource, Subresource, Subresource Locator
      • @CONTEXT
    • Section 5 >
      • Heroku, Java, Jersey and Postgres
      • Music Store with Postgres
      • Working with Files
      • Storing files in Postgres
      • Homework
    • Section 6 >
      • Books Store with Mongo
      • Google APIs
      • Twitter APIs
      • Properties for a web application
      • Homework
    • Section 7 >
      • Jersey Client Famework
      • Java Jersey SalesForce and OAuth
      • Servlets and JSP
      • Data access examples
    • Section 8 >
      • MVC - Model View Controller
      • JSTL Core tags
      • Scopes
      • Building Services to work with forms
    • Section 9 >
      • Calling REST services with JQuery
      • Angular.js
    • Section 10 >
      • consuming SOAP services
      • Web Sockets
      • Websockets, continued
    • Section 11 - Software Development Methodologies, Testing, Performance and Security >
      • Software Development Methodologies
      • Testing, Performance and Security
    • Your Project
    • Previous Projects
    • Course Project Submission
    • SaaS Projects
    • Install Tomcat on Mac

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()
  • Home
  • Poems
  • Python
    • Python Interview Prep Notes for Students >
      • Merge Intervals Leetcode
      • Merge k Sorted Lists
    • Python Setup >
      • Pep 8 style guide
    • Lecture 1 - Basics
    • Lecture 2 - Loops, Lists >
      • Homework 2 Program 1 Step By Step
      • Homweork 2 Shuffle String Program
    • Lecture 3 - structures, packing, string functions >
      • Homework
      • Additional Exercises - you do not need to submit these
    • Lecture 4 - Dictionaries, Functions, Modules
    • Lecture 5 - functions, variable scope, modules >
      • Previous Exams
    • Lecture 6 - CLI, Files, Regex, pickle, shelve
    • Lecture 7 - capturing web content >
      • Processing XML data
    • Midterm Prep >
      • Programming assignment prep
    • Lecture 8 - Exceptions, Classes, Inheritance >
      • Exceptions Recording
      • Multiple Except Clauses Recording
      • Eating an Exception Recording
      • finally and else clause recordings
      • Exception Stack Trace Recording
      • exit() and CTRL+C
      • Class Variables Recording
      • Static Methods and Class Methods Recording
      • Inheritance Recordings
    • Section 9 - db, flask, pip >
      • Accessing Databases
      • HTTP Server
      • Web Applications
      • Finding and installing additional Libraries with pip
      • Web Services with Flask
      • Calling Web Services from Python
      • Placing your application on the web
    • Section 10 - flask, grpc, venv, docker >
      • Flask based Web Applications
      • gRPC
      • Packaging, Dependencies and Virtual Environments
      • Dockerized Python Application
    • Section 11 - Django, GUI >
      • Cross Site Scripting XSS and markupsafe
      • Django >
        • Cross Site Request Forgery CSRF
      • tkinter - frame and button
      • Toplevel
      • Frame
      • Mouse and Keyboard Events
      • Canvas
      • Menu
      • Radiobuttons and Checkboxes
      • Entry
      • ImageMagic
    • Lecture 12 - WebSockets, System Monitoring, PyGame >
      • Web Sockets
      • PyGame Platformer
    • Lecture 13 - Data Structure and Algorithms >
      • Arrays and Strings
      • Dictionaries
      • Linked Lists
      • Trees
      • Stack and Queue
      • Sets
      • Merge Sort
      • Coverage Reports
    • Lecture 14 - Subprocesses, Threads, Multiprocessing >
      • Graphs
      • Singleton Design Pattern
      • Subprocesses and Multithreading
      • Jupiter Notebooks
      • Pandas Operations
    • Extra >
      • Raspberry Pi
      • Raspberry PI home security
      • OpenCV and template matching
      • Compare SQLite DB's in Python
      • Natural Language Processing and nltk
      • Hackathon Projects
  • SaaS
    • Java Interview Prep Notes for Students
    • Setup >
      • SaaS and Cloud Computing
      • Eclipse set up
      • Building web application in Eclipse using Tomcat
    • Section2 >
      • Maven Intro
      • Set up a simple web app and deploy on Heroku
      • Add Jersey Dependencies
      • Dissecting our first REST service
    • Section3 >
      • Jackson intro
      • @POST, @PUT, @DELETE
      • Your Project
    • Section4 >
      • Rules of Injection
      • Root Resource, Subresource, Subresource Locator
      • @CONTEXT
    • Section 5 >
      • Heroku, Java, Jersey and Postgres
      • Music Store with Postgres
      • Working with Files
      • Storing files in Postgres
      • Homework
    • Section 6 >
      • Books Store with Mongo
      • Google APIs
      • Twitter APIs
      • Properties for a web application
      • Homework
    • Section 7 >
      • Jersey Client Famework
      • Java Jersey SalesForce and OAuth
      • Servlets and JSP
      • Data access examples
    • Section 8 >
      • MVC - Model View Controller
      • JSTL Core tags
      • Scopes
      • Building Services to work with forms
    • Section 9 >
      • Calling REST services with JQuery
      • Angular.js
    • Section 10 >
      • consuming SOAP services
      • Web Sockets
      • Websockets, continued
    • Section 11 - Software Development Methodologies, Testing, Performance and Security >
      • Software Development Methodologies
      • Testing, Performance and Security
    • Your Project
    • Previous Projects
    • Course Project Submission
    • SaaS Projects
    • Install Tomcat on Mac