Raspberry Pi: Vocal Intruder Warning System Project – Part 2

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.

Adding the new Python librarys

On the Raspberry Pi, either via SSH or directly connected, enter the following commands:

Shell

1

sudo apt-getinstall python-picamera

When the module has finished installing then make a copy of your original code:

Shell

1

cppir_1.pypir_2.py

Now open the new copy of the code and modify as follows:

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

#!/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

importRPi.GPIO asGPIO

importtime

# We need this next line so we can run the text to speech script later

importos

# Import the Python Camera Library

importpicamera

# 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

whileGPIO.input(GPIO_PIR)==1:

Current_State=0

print" Ready"

# Loop until users quits with CTRL-C

whileTrue:

# Read PIR state

Current_State=GPIO.input(GPIO_PIR)

ifCurrent_State==1andPrevious_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

elifCurrent_State==0andPrevious_State==1:

# PIR has returned to ready state

print" Ready"

Previous_State=0

# Wait for 10 milliseconds

time.sleep(0.01)

exceptKeyboardInterrupt:

print" Quit"

# Reset GPIO settings

GPIO.cleanup()

Remember to save your code and exit nano:

CTRL+O
CTRL+X

What’s changed

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:

Python

1

timestr=time.strftime("%H%M%S-%Y%m%d")

This would give “171725-20140318″ or if you change it to:

1

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:

Shell

1

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.