Skip to content

Instantly share code, notes, and snippets.

@AO8
Last active May 12, 2017 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AO8/39d0d63d3d53e7bd6bc24e506d13c73a to your computer and use it in GitHub Desktop.
Save AO8/39d0d63d3d53e7bd6bc24e506d13c73a to your computer and use it in GitHub Desktop.
Use a PIR sensor, Raspberry Pi, and PiCamera to detect motion, take a timestamped photo, then send a text message alert from Gmail with Python.
# Allow less secure apps to access your Gmail account
# https://en.wikipedia.org/wiki/List_of_SMS_gateways
# 10digitNumber@mms.att.net for AT&T
# 10digitNumber@pm.sprint.com for Sprint
# 10digitNumber@tmomail.net for T-Mobile
# 10digitNumber@vzwpix.com for Verizon
from gpiozero import MotionSensor
from picamera import PiCamera
from datetime import datetime
import smtplib
camera = PiCamera()
pir = MotionSensor(4) # GPIO pin on Raspberry Pi
while True:
pir.wait_for_motion()
filename = datetime.now().strftime("%m-%d-%Y_%H.%M.%S.jpg")
camera.capture(filename)
pir.wait_for_no_motion()
username = "yourAddress@gmail.com"
password = "yourPassword"
server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login(username, password)
server.sendmail(username, "10digitNumber@mms.att.net", "Warning! Motion detected!")
server.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment