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.
- 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.
P1 Camera is selected and press Enter.
Would you like the camera interface to be enabled?, select
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
from picamera import PiCamera
from time import sleep
from signal import pause
import RPi.GPIO as GPIO
from wia import Wia
PIR = 7
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
# Takes photo and calls function to send event and photo to Wia
# Sends motion detected event to Wia along with Image captured through camera
Wia().Event.publish(name = 'motionDetected', file = open(filePath, 'rb'))
# If the PIR motion sensor is HIGH then an intruder has been detected
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.
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
In the debugger in the Wia dashboard you should now see the Event appearing.