Security System w/ Motion Sensor, Camera, Wia, Raspberry Pi

Build your own security system, using the Wia Python SDK, a Motion sensor module, a camera module, and a Raspberry Pi.

Project Requirements

  • Raspberry Pi 2 or 3 Model B
  • Motion sensor
  • PiCamera
  • A Wia account. You can create one for free here: https://www.wia.io/signup
  • 3 female to female jumper cables.

Connecting the Motion Sensor

  • First step, ensure the Raspberry Pi is not powered on.
  • On the motion sensor there are 3 connectors: VCC, GND and OUT.
  • Using female to female jumper cables, connect the VCC connector to the 5V GPIO pin of the Raspberry Pi.
  • Next, connect the GND connector to a ground GPIO pin on the Raspberry Pi.
  • Finally, connect the OUT connector to GPIO pin 4 on the Raspberry Pi.

Connecting the Camera

  • Locate the camera port and connect the camera. Ensure the connector is inserted correctly and fully fastened to the port.

Install PiCamera Module

On the terminal enter the following commands (in this order):

sudo apt-get update 
sudo apt-get install python-picamera python3-picamera 

Create Motion Detector

  • Login to Wia dashboard @ https://dashboard.wia.io/login
  • In the dashboard select the Devices section.
  • Click the ‘+’ symbol to create a device.
  • Name the device, “Motion Sensor”.
  • Remember to take note of the device secret key for your new device.

Install Wia Python SDK

On the command line enter the following commands (in this order):

sudo apt-get install python-setuptools 
sudo easy_install pip 
sudo pip install wia 

Write code for the motion detector

  • We need to create a file for our program, let’s call it security.py
  • Copy the code from the example below and add to the security.py file.
from picamera import PiCamera
from gpiozero import MotionSensor
from time import sleep
import wia
 
wia.secret_key = 'device-secret-key'
 
filePath = 'image.jpg'
 
camera = PiCamera()
 
# Print message to terminal stating motion has been detected
# and calls function to take photo
def printWhenMotion():
    print("Motion Detected")
    capturePhoto()
 
# Takes photo and calls function to send event and photo to Wia
def capturePhoto():
    camera.capture(filePath)
    sleep(5)
    sendWiaConnection()
 
# Sends motion detected event to Wia along with Image captured through camera
def sendWiaConnection():
    wia.Event.publish(
        name = "motionDetected",
        data = "motionDetected",
        file = open(filePath, 'rb').read()
    )
 
pir = MotionSensor(4)
pir.when_motion = printWhenMotion
raw_input()
  • Replace the “device-secret-key” placeholder on line 5 with your device’s secret key that you took note of earlier.

Now to test that you have done your setup correctly, simply run python security.py . Your device should have updated in the dashboard under the Events tab, with your event readings.

Source: Security System w/ Motion Sensor, Camera, Wia, Raspberry Pi

Scroll to Top