Voor iemand die geïnteresseert is wat ik met de e-mail (met attachment) gedaan heb (gewoon als spielerei):
Op m'n Rasberrypi2, als GPIO19 aan ground (aarde) wordt gelegd (via een uitwendige gebeurtenis ) zoals b.v. een deuralarm of bewegings sensor, etc. dan
neemt de raspberry pi camera een snapshot en emailt die dan naar mijn emailadres.
#!/usr/bin/python
# this Python application turns a Raspberry Pi into a security camera system
# it requires that you have a Pi camera installed
import datetime
import smtplib
import time
import picamera
import RPi.GPIO as GPIO
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
# define the GPIO port you will use for the door sensor
SENSOR = 19
now = datetime.datetime.now()
# number of seconds to delay between alarm and snapshot
# in case you want to wait a second or two for the person to enter the room after triggering the sensor
DELAY = 1
#setup GPIO using Broadcom SOC channel numbering
GPIO.setmode(GPIO.BCM)
# set to pull-up (normally closed position for a door sensor)
GPIO.setup(SENSOR, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# text message to send with photo
TXT_MSG = "Alarm Triggered!"
# directory to save the snapshot in
IMAGE_DIR = ""
# name and dimentsions of snapshot image
IMG = "gpioalarm.jpg"
IMG_WIDTH = 800
IMG_HEIGHT = 600
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'GPIO Alarm'
msg['From'] = 'henk@oegema.com'
msg['To'] = 'xxxx'
msg.preamble = 'GPIO 19 Alarm'
username = 'xxx'
password = 'yyy'
try:
# setup an indefinite loop that looks for the door sensor to be opened
while True:
GPIO.wait_for_edge(SENSOR, GPIO.RISING)
print("GPIO alarm!\n")
time.sleep(DELAY)
with picamera.PiCamera() as camera:
camera.resolution = (IMG_WIDTH, IMG_HEIGHT)
camera.capture(IMAGE_DIR + IMG)
# Open the files in binary mode..
image_path = '/home/pi/gpio_alarm/gpioalarm.jpg'
fp = open(image_path, 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)
# Send the email via the Gmail server.
s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login(username,password)
s.sendmail('henk@oegema.com', 'xxx', msg.as_string())
s.quit()
finally:
GPIO.cleanup() # ensures a clean exit