Raspberry Pi camera in hand, Vincent has updated his brilliant warning system project to take snapshots of intruders.
I have recently purchased one of the fantastic Raspberry Pi Camera modules. The camera costs around £20 and plugs into one of the special sockets on the Raspberry Pi. This socket connects straight into the Graphics Card (GPU) so it has excellent response times.
To take advantage of the Raspberry Pi camera I have added some code to my original Vocal Intruder Warning System Project that will take a still image each time somebody moves past the PIR sensor.
What you will require
- Raspberry Pi Camera Module (Amazon / CPC Farnel)
- Raspberry Pi (Amazon)
- A Speaker (Bluetooth, 3.5m jack or USB)
- A PIR Module (I recommend this one from Amazon)
- Three connecting wires (female to female – like this bundle from ModMyPi)
- An internet connection (Wireless or wired is fine – if you can’t provide internet access to Google then you will need to use an offline Text To Speech module).
For this project I will assume you have already followed the original vocal intruder project, so will only cover the extra setup required to get the camera working in the project.
First ensure your camera is connected as per the official instructions and video.
Adding the new Python librarys
On the Raspberry Pi, either via SSH or directly connected, enter the following commands:
sudo apt-get install python-picamera
When the module has finished installing then make a copy of your original code:
cp pir_1.py pir_2.py
Now open the new copy of the code and modify as follows:
#!/usr/bin/python #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ #|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k| #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # # pir_1.py # Detect movement using a PIR module # # Original Author : Matt Hawkins # Date : 21/01/2013 # Speech output Added : Vincent Willcox # Date : 12/01/2014 # Add Python Raspberry Pi Camera Control # Date : 18/03/2014 # Import required Python libraries import RPi.GPIO as GPIO import time # We need this next line so we can run the text to speech script later import os # Import the Python Camera Library import picamera # Tell the Pi about the camera camera = picamera.PiCamera() # Use BCM GPIO references # instead of physical pin numbers GPIO.setmode(GPIO.BCM) # Define GPIO to use on Pi GPIO_PIR = 4 print "PIR Module Test (CTRL-C to exit)" # Set pin as input GPIO.setup(GPIO_PIR,GPIO.IN) # Echo Current_State = 0 Previous_State = 0 try: print "Waiting for PIR to settle ..." # Loop until PIR output is 0 while GPIO.input(GPIO_PIR)==1: Current_State = 0 print " Ready" # Loop until users quits with CTRL-C while True : # Read PIR state Current_State = GPIO.input(GPIO_PIR) if Current_State==1 and Previous_State==0: # PIR is triggered print " Motion detected!" # Get the current Time and Date to give the image some context. timestr = time.strftime("%Y%m%d-%H%M%S") # Take A Picture of the subject camera.vflip = True # I have the camera mounted upsidown - so need to flip the images camera.hflip = True camera.capture('image'+timestr+'.jpg') # Tell the Pi to run our speech script and speak the words # motion dtected! - anything after the .sh will be read out. cmd_string = './speech.sh motion detected!' # now run the command on the Pi. os.system(cmd_string) # Record previous state Previous_State=1 elif Current_State==0 and Previous_State==1: # PIR has returned to ready state print " Ready" Previous_State=0 # Wait for 10 milliseconds time.sleep(0.01) except KeyboardInterrupt: print " Quit" # Reset GPIO settings GPIO.cleanup()
Remember to save your code and exit nano:
Line 21 & 22 : Here we are importing the new Python camera module code. This will allow us to capture images directly with python code.
Line 25: Here we are turning on the camera and telling our project to be ready to take an image.
Line 61 & 62 : We are getting the current time and date after motion has been detected by the project. I will stop here and go into some detail of line 62’s code.
Line 62 Detailed: We are creating a text string called “timestr” and then storing the date and time in the format “20140318-171725”. If you wish you can simply flip this around by changing the line to:
timestr = time.strftime("%H%M%S-%Y%m%d")
This would give “171725-20140318” or if you change it to:
timestr = time.strftime("%H%M%S-%d%m%Y")
You would now get “17725018032014”
You can play about with this code until you are happy with the output.
Line 64 & 65: Since my module is upside-down, I need to add these two lines of code to make sure the image that is saved is the correct way up. Experiment with changing either or both from “True” to “False” to ensure your images are correct.
Line 66 : Now we take a picture and save it with a file name of “image20140318-171725.jpg”. The time stamp is added by breaking out of the string and adding in the string we set earlier.
The rest of the code is unchanged.
Running the project
Enter the following on the command line to kick it off:
sudo python pir_2.py
To stop the code running at any time – press CTRL+C on your keyboard.
Now your code is complete. If you wish to view the images your project captures you can use a tool like FileZilla to access the Raspberry Pi and take the images from the project onto a PC or Mac. I will be back soon with a final follow up to this that will add some extra code to email the files to you.