Raspberry Pi Camera and Email

I recently bought the camera module for the raspberry pi, and decided I’d play around with it and post. To use the camera for the Pi, the picamera python library is used. It is very well documented and pretty easy to use.

There seems to be several versions of the Pi Camera Module V2 out there. Three from what I could find: Regular Camera, Pi Noir, and Night vision. Seems like night vision was just the camera module with two LEDs on the side. The Noir lacks an IR filter. I bought the Noir thinking that it would be cooler but I have an issue where pictures have color washed out. The camera seems to do fine in artificial light and not sunlight. I recommend just getting the regular version for simple use.

Today I’ll make a script to check if you left the stove on. When you run the script, it will make the Pi take a picture and then email it to you. Most of the code in the script is not new, since we already wrote code to email picture attachments.

So here’s the code to take a picture from the Pi:

def take_picture(filename):
    # Create a PiCamera object to control the pi's camera
    camera = PiCamera()

    # We want to rotate the photo since the camera is upside down
    camera.rotation = 180

    camera.start_preview()
    sleep(5)
    camera.capture(filename)
    print("Picture Taken!")

So what we are doing here is creating a PiCamera object, using that object to turn on the camera and letting it warm up for 5 seconds, then taking the picture. Then printing out a notification that we took it. A filename is going to be required for the take picture function, and remember to include the extension (ex. “.png”).

Now the code from the last post was just a script. Let’s be organized and package it into a function.

# First import the needed libraries
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import smtplib

# Import creds from config
from secrets import MY_ADDRESS, PASSWORD

def send_email(to_addr, sub, img_attachment):
    # Compose email in MIME format.
    # We want to send an email with a message and png attachment
    msg = MIMEMultipart()
    msg["Subject"] = sub
    msg["To"] = to_addr
    msg["From"] = MY_ADDRESS 

    # Attaching img to email
    with open(img_attachment, "rb") as f:
        mime_img= MIMEImage(f.read())
        msg.attach(mime_img)

    # Try sending, and print if it goes wrong
    try:
        server = smtplib.SMTP_SSL("smtp.gmail.com")
        server.login(MY_ADDRESS, PASSWORD)
        server.sendmail(MY_ADDRESS, to_addr, msg.as_string())
        server.quit()
        print("Email sent successfully!")
    except Exception as e:
        print(e)
        print("Email not sent!")

You will notice from the old code that I’m importing a new package:

from secrets import MY_ADDRESS, PASSWORD

This is just a second python file so that I do not have to store my password and email login on github. All the lines including MIMEText were excluded, since for this task I do not need to send a message with the email. Also, I included a print statement to tell me if the email was not sent.

Also, we can save our login credentials to the email account in a separate python file that can be imported as a package. So our file structure looks like this:

project/
|
|__ sendemail.py
|
|__ secrets.py

Your secrets.py file should look something like this:

MY_ADDRESS = "[email protected]"
PASSWORD = "PASSWORD123"

Now to combine the two functions and have the script take the picture and send the email at the same time:

def main():
    # First define where you want to send and subject
    TARGET_ADDRESS = "[email protected]" #Replace this with recipient
    SUBJECT = "Stove Query" 
    IMG = "stove.png"

    # Take the picture
    take_picture(IMG)

    send_email(TARGET_ADDRESS, SUBJECT, IMG)


if __name__ == "__main__":
    main()

So first I defined the variables such as recipient address, subject,and image name. Then we just need to plug those variables into the take_picture function and send_email function.  The if __name__ ==“__main__”: line just means that if I run the file, then everything below the line will be executed. In this case, main will be executed but also call the other functions we made.

So the whole code can be found on my Github.Now you can have your pi email you pics of the stove to see if you left it on or not.

Leave a Reply

Your email address will not be published. Required fields are marked *