Skip to content

Instantly share code, notes, and snippets.

@luemar

luemar/onMotion_img.py

Last active Jan 6, 2019
Embed
What would you like to do?
Home surveillance using raspberry pi 3, PIR Sensor, NoIR camera
#!/usr/bin/python
import smtplib, sys, picamera, time
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from gpiozero import MotionSensor
from pushbullet import PushBullet
from socket import gethotname, gaierror
def foto(foto_pfad): 
camera = picamera.PiCamera()
camera.rotation = 90
try:   
camera.capture(foto_pfad, resize=(640, 480))   
except picamera.exc.PiCameraError:
print'Camera Fehler'
camera.close()
def mail(foto_pfad):
frm = 'my email address'   
to = 'my email address'   
smtpHost = 'my post exit server address'   
smtpPort =  587   
smtpUser = 'my email user name'   
smtpPassword = 'my email password'   
subj = 'from RPi 3'   
msg = 'Foto from RPi 3'
    mime = MIMEMultipart()   
mime['From'] = frm   
mime['To'] = to   
mime ['Subject'] = Header(subj, 'utf-8')   
mime.attach(MIMEText(msg, 'plain', 'utf-8'))
    with open (foto_pfad, 'rb') as f:
img = MIMEImage(f.read())
mime.attach(img
try:
smtp = smtplib.SMTP(smtpHost, smtpPort)   
smtp.login(smtpUser, smtpPassword)   
smtp.sendmail(frm, to, mime.as_string()) 
smtp.quit()
except gaierror:
print 'email Fehler'
def mes():   
api_key = "my api key"   
phone_number = "my cellphone number"   
pb = PushBullet(api_key)   
device = pb.devices[0]   
try:
device = pb.push_sms(device, phone_number,'Foto from RPi 3')
except(Exception):
print'sms Fehler'
count = 0
def printit():   
global count   
count +=1   
t = time.strftime('%d.%m.%Y %H:%M:%S')   
print'PIR 3 am', t, 'zum %d.Mal ausgelöst' % count
while True:
def on_motion():   
pfad = '/home/pi/bild1.jpg'
foto(pfad)
mail(pfad)
mes()   
printit()   
if count %3 == 0:       
print'im Schlafmodus'
time.sleep(1800)       
pir = MotionSensor(11)
try:
pir.when_motion = on_motion
time.sleep(60)
except KeyboardInterrupt:
sys.exit()
@luemar

This comment has been minimized.

Copy link
Owner Author

@luemar luemar commented Apr 7, 2018

updated by an ifinite loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment