• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
ClassThink

ClassThink

School Technology Guides

  • E-mail
  • Facebook
  • RSS
  • Twitter
  • YouTube
  • HOME
  • LATEST
  • Featured
  • School App Search
    • Search
    • Dashboard
    • Directory Login
  • TEACHER GUIDES
    • Microsoft Teams
      • Microsoft Teams Teacher’s Hub
    • Microsoft OneDrive
    • Microsoft Stream
    • Microsoft Office
    • Microsoft Edge
    • Microsoft OneNote
    • Microsoft Office
    • Microsoft Outlook
    • Microsoft Windows
    • Microsoft SDS
    • Google Classroom
    • Google Meet
    • Zoom
  • REVIEWS
  • NEWSLETTER
  • ABOUT
    • Contact Us
    • About ClassThink
    • Support ClassThink
    • Write for ClassThink
    • Advertising and Sponsorship
You are here: Home / Raspberry Pi / Raspberry Pi: Vocal Intruder Warning System Project – Part 2

Raspberry Pi: Vocal Intruder Warning System Project – Part 2

29 March 2013 by Karl Rivers Leave a Comment

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
  • Raspberry Pi
  • 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).

Getting Started

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:

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:

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.

You might also like...

Filed Under: Raspberry Pi

This page contains references to products from one or more of our advertisers. We may receive compensation when you click on the links to these products. For an explanation of our Affiliate Policy, please visit this page.

About Karl Rivers

Karl Rivers is a Director of IT who has worked in education for more than twenty years. He won the Naace Impact Award for Supporting School Services.

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

  • E-mail
  • Facebook
  • RSS
  • Twitter
  • YouTube

You might also like...

Popular School Apps

  • Bromcom Computers Plc

    Bromcom Computers Plc

    MIS/SIS System

  • CleverTouch

    CleverTouch

    Classroom Screens & Projectors

  • Capita SIMS

    Capita SIMS

    MIS/SIS System +3 Parent Communications, Parent Portal, Parents Evenings,

  • Wakelet

    Wakelet

    Learning Platform +1 Social Bookmarking,

  • BKSK

    BKSK

    Assessments +1 Educational Games & Quizzes,

Latest Edtech News

Virtual breakout rooms for Microsoft Teams – everything you need to know

Download the free OneNote for Team Collaboration eBook

New Surface Laptop Go Announced

Featured

Best Chromebooks for students in 2021

Create beautiful infographics for your classroom

Record online lessons with Camtasia

Naace Impact Awards Winner

Footer

Recent

  • Best Chromebooks for students in 2021
  • Create beautiful infographics for your classroom
  • Virtual breakout rooms for Microsoft Teams – everything you need to know
  • Record online lessons with Camtasia
  • Download the free OneNote for Team Collaboration eBook
  • New Surface Laptop Go Announced
  • Managing Windows 10 in schools with Intune and Autopilot
  • How to stop students unmuting themselves in Microsoft Teams
  • How to spotlight students & demonstrations in Microsoft Teams meetings
  • How to record a PowerPoint presentation as a video
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Recent Forum Topics

  • Using VPN App in Apple or not? Not a big deal!
  • JvFHf5vfJ HyVSv2mpZ
  • Use VPN Security
  • How less is more with application development
  • How Much Does it Cost to Make an App like Uber
  • Microsoft Team class not appearing for teacher but is correct in Teams admin
  • FCM Test Notification!!! message in Microsoft Teams
  • FAQ: Can students begin a meeting in a private channel in Microsoft Teams
  • Microsoft Lists for Education
  • Benefits of hiring a ghostwriter

Most Popular Posts

  • How to turn off chat for students in Microsoft Teams
  • How to delete a video from Microsoft Teams
  • How to record a PowerPoint presentation as a video
  • How to stop students muting and kicking others in Microsoft Teams video meetings
  • How to annotate and share a screenshot in Microsoft Teams
  • How to restore a deleted team in Microsoft Teams
  • How to enable Large Gallery view and Together Mode in Microsoft Teams
  • How to record and share a lesson in Microsoft Teams
  • Virtual breakout rooms for Microsoft Teams - everything you need to know
  • How to repair a smashed Chromebook screen in 3 minutes
  • E-mail
  • Facebook
  • RSS
  • Twitter
  • YouTube

Copyright © 2021 · ClassThink.com · Affiliate Disclaimer · Privacy Policy · Here's the secret · Log in