Linux Format

Postie watch

Email a snap of your special deliveries.

-

Are you always backing kickstarte­rs but never at home to receive your rewards when the postman comes? Well, this project can alert you to a parcel via email. With your Raspberry Pi turned off, attach the camera by the camera slot located near the Ethernet port. Next, connect your Passive Infra-Red (PIR) sensor to the following GPIO pins of your Pi. Please note: we are using the Broadcom pin mapping.

PIR PIN

VCC

OUT

GND

GPIO PIN

5V

17

GND

Boot your Pi and use the configurat­ion tool located in the Preference­s menu. Enable your camera and ensure that SSH login is enabled. Reboot and then open Python 3 from the Programmin­g menu. Create a new file, save and call it emailer. py. We start our code by importing a series of libraries. (You can view the full list via our source code link, and it starts with from mail_settings import * .) These handle sending email, taking pictures using the camera and timing our project. One extra library is mail_settings. This is an external library written just for this project and used to store email usernames and passwords. We’ll be using the Broadcom pin mapping and need to set this before we proceed: GPIO.setmode(GPIO.BCM) global file PIR = 17 GPIO.setup(PIR, GPIO.IN)

We now create two variables: the first is a global variable that we can use between functions and the second, called PIR, stores the number of the pin used for our sensor. We set up our PIR connected to GPIO17 as an input. Next, we create two functions: the first takes a picture with the camera: def takepic(): global file current_time = str(datetime.datetime.now()) current_time = current_time[0:19] with PiCamera() as camera: camera.resolution = (800, 600) camera.framerate = 24 camera.capture((current_time)+’.jpg’) takepic.file = ((current_time)+’.jpg’) With takepic() we capture the current time and date as the name of the file, and we store and slice the string stored in the variable using only the text that we need. Next, we capture an image and save it with that file name. Our second function handles the email: def email_send(to,file): current_time = str(datetime.datetime.now()) current_time = current_time[0:19] msg = MIMEMultip­art() msg[‘Subject’] = ‘ALERT - AT ‘+current_time+’ THE POST HAS ARRIVED’ msg[‘From’] = email msg[‘To’] = to with open(takepic.file, ‘rb’) as pic:

pic = MIMEImage(pic.read()) msg.attach(pic) server = smtplib.SMTP(‘smtp.gmail.com’,587) server.starttls() server.login(email,password) server.ehlo() server.send_message(msg) server.quit()

We can reuse the same method to capture the date and time of the alert. We create a mixed content email with a subject that’s composed of an alert string featuring the time and date of alert. Who the email is from is generated by the customer mail_settings library. The recipient is passed as an argument in the function and our image is attached as a file to the email. A variable called server stores the location of our mail server, in this case a Gmail account. We open a secure connection to the server, login and then announce to the server that we’re there. We then send the message before closing the connection to the server.

With the functions written, we use a while true loop to constantly check if the PIR sensor has been triggered. If this is the case a photo is taken, attached to an email and sent to the recipient. If the sensor isn’t triggered then the loop repeats. Now save your work and click on Run > Run Module to start.

Newspapers in English

Newspapers from Australia