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.
Locate the camera port and connect the camera. Ensure the connector is inserted correctly and fully fastened to the port.>
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
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.
touch security.py
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.
your_device_secret_key
Press CTRL+O (that's the letter o, not the number) to save the code, followed by CTRL+X to exit Nano.
CTRL+O
CTRL+X
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.
python3 security.py
In the debugger in the Wia dashboard you should now see the Event appearing.