
In this tutorial, we are going to build your own security system, using the Wia Python SDK, a motion sensor module, a camera module, and a Raspberry Pi.
Components
- Raspberry Pi 3 Model B
- PIR Sensor (HC-SR501)
- Raspberry Pi Camera Module
- Female jumper cables
Connecting the Motion Sensor
- 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.
Once you've got everything up and going, it's time to start setting up the camera on your Raspberry Pi. When you are logged into your Raspberry Pi via SSH, run the command sudo raspi-config
. Press the down arrow key until you reach 5 Interfacing Options
and press Enter.

Make sure P1 Camera
is selected and press Enter.

When asked Would you like the camera interface to be enabled?
, select <Yes>
.

To exit, press the down arrow key until you reach <Finish>
and press Enter.
Installing the python3 tools
Open up a terminal window for your Raspberry pi, enter the following commands (in this order):
sudo apt-get update
sudo apt-get install python3-setuptools python3-pip python3-picamera python3-rpi.gpio
Note: We are using python3 for this tutorial.
In the terminal, enter the following command to add the Wia library:
pip3 install wia
Write code for the motion detector
Now we're going to write the code that will read the PIR sensor, and if an intruder is detected, capture a photo and send it to Wia.
Create a new file by running the command touch security.py
. Open the text editor by running nano security.py
.
from picamera import PiCamera
from time import sleep
from signal import pause
import RPi.GPIO as GPIO
from wia import Wia
PIR = 7
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIR, GPIO.IN)
wia = Wia()
wia.access_token = 'your_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', file = open(filePath, 'rb'))
while True:
# If the PIR motion sensor is HIGH then an intruder has been detected
if GPIO.input(PIR):
printWhenMotion()
Copy and paste the code above, replacing your_device_secret_key
with your device's secret key. If you haven't already set one up, you can do so in the Wia dashboard here.
Press CTRL+O
(that's the letter o, not the number) to save the code, followed by CTRL+X
to exit Nano.
Running the code
Now that we should have everything setup, lets see if it all works
Back in your terminal, run the code by running the command python3 security.py
.
In the debugger in the Wia dashboard you should now see the Event appearing.
