cortez705
Published

Lane Tech PCL - emailing camera

Learn the truth about who is entering your room by using a magnetic sensor and raspberry pi

IntermediateWork in progress303
Lane Tech PCL - emailing camera

Things used in this project

Story

Read more

Schematics

layout

Code

camera code and emailing

Python
(you will need to use raspberry pi os )
you will need to use the terminal on the pi to download the library smtplib use these lines of code in the pi terminal

sudo apt-get install ssmtp mailutils
type yes when prompted with y/n
sudo nano /etc/ssmtp/ssmtp.conf
then edit the code that pops up with
mailhub = smtp.gmail.com;587

AuthUser = youremail
AuthPass = yourpassword
UseSTARTTLS = YES
UstTLS = YES
save with control o
and exit with control x

after this open up thonny python ide and paste the code then edit it to your email address and password as well as any other tweaks you would like to make
import time
import datetime
import RPi.GPIO as GPIO
import smtplib
from picamera import PiCamera
from time import sleep
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
fromaddr = "yourEmailAddress@"
toaddr = "yourEmailAddress@"


camera = PiCamera()
#camera.capture("/home/pi/Pictures/img.jpg")
def sendEmail():
    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = "caught"
    body = "I swear"
    msg.attach(MIMEText(body, 'plain'))
    filename = "ok.jpg"
    attachment = open(filename, "rb")
    part = MIMEBase('application', 'octet-stream')
    #this carries the attachment
    part.set_payload((attachment).read())
    #reads the data
    encoders.encode_base64(part)
    #encodes it 
    part.add_header('Content-Disposition', "attachment; filename= {}".format(filename))
    #label of the file to write in the email
    msg.attach(part)
    #msg object attaches the file 
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "yourPassword")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()
def sensorCallback(channel): 
  timestamp = time.time()
  if GPIO.input(channel):
    print("Sensor HIGH ")    
  else:
    print("Sensor LOW")
    camera.start_preview()
    sleep(5)
    camera.capture("/home/pi/Pictures/ok.jpg")
    camera.stop_preview()
    sleep(60)
    sendEmail()
def main():
    sensorCallback(17)

GPIO.setmode(GPIO.BCM)
GPIO.setup(17 , GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(17, GPIO.BOTH, callback=sensorCallback, bouncetime=200)

if __name__=="__main__":
    main()
    

Credits

cortez705
1 project • 0 followers

Comments