Building a surveillance system with a PIR sensor, the BeagleBone Black and Python.The BeagleBone Black is an outstanding tool for projects that involve the Internet. Access is easy (simply connect it to the router through an Ethernet cable ), and both Python and JavaScript feature libraries that greatly simplifies matters.
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.
Parts Required
Here’s a list with everything you need:
- BeagleBone Black (Click to see on Amazon.com *)
- PIR Motion Sensor
Schematics
NOTE: You can read my tutorial on how to modify a cheap PIR motion sensor to operate at 3.3V.
Python Code
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 = "[email protected]"
my_password = "REPLACE_WITH_YOUR_PASSWORD"
destination = "[email protected]"
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
Conclusion
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!
* We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.
How about a camera? Can you have it snap a picture on given intervals and upload it via ftp? Or combine IR and camera to send a picture of 5 the IR goes off? Thanks.
Hi C. Koh,
there’s a good tutorial on that subject in this video: youtube.com/watch?v=8QouvYMfmQo
Thank you for asking,
Rui
Hello Rui Santos,
interesting article thanks!
one remark
it says that one can download the code, the Python script
but i cannot find the link
thanks
Jan Sterenborg
Hi Jan,
Thank you for letting me know.
I’ve fixed the download link.
Rui
Will this work with iMAC OSX?
If not there is a challenge for you.
Yes Robert!
The BeagleBone Black is compatible with Mac OS X.
Thanks for asking,
Rui
its showing me this thing, what does it mean? “(bill.py) is my file name”
File “bill.py” , line 21
my_email = [email protected]
You need to add the “” in that line.
my_email = “[email protected]”