This post was written by Rui Santos and Luís Perestrelo authors of BeagleBone For Dummies.
Building a surveillance system with a PIR sensor, the BeagleBone Black and Python.
PIR sensors are quite neat devices that are able to detect motion. Their output pin, which will be connected to the BeagleBone Black sends a digital signal HIGH whenever somebody passes by, and is LOW whenever else. Automatic lights in public places are built on this concept.
The project sends you an email whenever motion is detected.
Here’s a list with everything you need:
- BeagleBone Black (Click to see on Amazon)
- PIR Motion Sensor (Click to see on eBay)
- Internet Connection
NOTE: You can read my tutorial on how to modify a cheap PIR motion sensor to operate at 3.3V.
You can click here to download this Python script.
NOTE: You can only use this example with Gmail. And don’t forget to replace the example below with your own credentials…
#!/usr/bin/python #import libraries import smtplib import Adafruit_BBIO.GPIO as GPIO import time #create a variable called PIR, which refers to the P8_11 pin PIR = "P8_11" #initialize the pin as an INPUT GPIO.setup(PIR, GPIO.IN) GPIO.add_event_detect(PIR, GPIO.RISING) def send_email(): server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() server.starttls() server.ehlo() my_email = "REPLACE_WITH_YOUR_EMAIL@gmail.com" my_password = "REPLACE_WITH_YOUR_PASSWORD" destination = "REPLACE_WITH_YOUR_EMAIL@gmail.com" text = "Motion has been detected at your house!" server.login(my_email, my_password) server.sendmail(my_email, destination, text) server.quit() print("Your email has been sent!") #loop forever while True: #sends an email when motion has been detected if GPIO.event_detected(PIR): send_email() time.sleep(0.05) #loop every 50 miliseconds to not overburden the CPU
Now you’ve built your own surveillance system that sends you an email when the PIR sensor detects movement!
If you have any questions post them in the comments below. Special thanks to Luís Perestrelo for helping Rui Santos putting this series together!
Want to learn more about the BeagleBone Black?
You can buy our book BeagleBone For Dummies on Amazon!