Have you ever wondered how to build your own home surveillance system? Whether this be to monitor your children, monitor vulnerable people in their home, or to be your home security system?
In this tutorial, you get to build a small and cheap home surveillance system using a Raspberry Pi 4 with a Raspberry Pi Camera module and motion sensor. The software side of this will be using Vonage Video Api (formerly TokBox) to publish the stream and Vonage SMS Api to notify the user that motion has been detected.
Table of Contents
- Getting Started
- Setting up the Raspberry Pi
- Installing Raspberry Pi Camera Module
- Enabling SSH & Camera
- Installing the Motion Sensor
- Node & NPM
- Our CLI
- Git (Optional)
- Install a Mysql Server
- This Demo Application
- Installing an SSL Certificate
- Install Ngrok
- Configure the Application
- Store the Credentials in the Environment
- Run all Database Migrations
- Start the App
- Code of Conduct
- Contributing
- License
On the Raspberry Pi website is a great step by step guide on what each part of the Raspberry Pi device is, how to get the Operating System installed, and how to get started with using a Raspberry Pi. On the site, there are also many other resources helping with troubleshooting any issues you may be having, or other projects that may interest you.
This code is configured to use a Raspberry Pi 4 and official Raspberry Pi Camera module, although other cameras may have no issues being used.
The photo below is the Raspberry Pi and a camera module I've used:
Connect the camera module as shown below:
To enable SSH, in the Raspberry Pi Terminal, run
sudo raspi-config
You will be greeted with a screen that looks like the image below:
Choose option 5 - Interfacing Options
- From the next menu, choose Option P1 for
Camera
, then chooseYes
- Following this choose Option P2 for
SSH
, again chooseYes
.
You have now enabled the Camera module and SSH on your Raspberry Pi.
The next step is to wire the Raspberry Pi to a motion sensor, for which this example I've used an HC-SR501 PIR motion sensor.
First, take the sensor and connect three wires to it. I've used red for the live, blue for the GPIO, and black for the ground. For the sensor in this example, the first pin is ground, second GPIO and third live as shown below:
A great example to describe each of the pins of the Raspberry Pi is on The Raspberry Pi Website. On this page is a diagram describing the layout of the GPIO pins, this diagram is shown below:
The final part is connecting the wires to the Raspberry Pi. The live (red) wire needs to be connected to one of the 5V power
pins on the Pi, referring to the diagram above I used pin 2. The ground (black) wire needs to be connected to one of the GND
pins on the Pi, again referring to the diagram I used pin 6. The final wire to connect is the GPIO (blue) wire, which needs to connect to one of the GPIO
pins. In this example, I used pin 12, labelled "GPIO 18".
You will see the wires wired up, as shown below:
node --version
npm --version
Both Node and NPM need to be installed and at the correct version. Go to nodejs.org, download and install the correct version if you don't have it.
To set up your application, you'll need to install our CLI. Install it using NPM in the terminal.
npm install -g nexmo-cli@beta
You can check you have the right version with this command. At the time of writing, I was using version 0.4.9-beta-3
.
nexmo --version
Remember to sign up for a free Vonage account and configure the CLI with the API key and secret found on your dashboard.
nexmo setup <your_api_key> <your_api_secret>
You can use git to clone the demo application from GitHub.
For those uncomfortable with git commands, don't worry, I've you covered.
Follow this guide to install git.
On the Raspberry Pi, run the following command to install the MySQL database server:
sudo apt install mariadb-server
By default MySQL is installed with the root
user having no password. This needs to be rectified so the database isn't insecure. On the Pi run the command below and follow the instructions.
sudo mysql_secure_installation
Now the root
user's password is set, it's time to create a database and user to access that database. Connect to the MySQL server:
sudo mysql -u root -p
-- Creates the database with the name picam
CREATE DATABASE picam;
-- Creates a new database user "camuser" with a password "securemypass" and grants them access to picam
GRANT ALL PRIVILEGES ON picam.* TO `camuser`@localhost IDENTIFIED BY "securemypass";
-- Flushes these updates to the database
FLUSH PRIVILEGES;
Your database is now set up!
Clone or download the demo application. To download, go to the repository and click on the Clone or download button on GitHub.
Note: If you download, make sure you're on the right version number before downloading.
git clone git@github.com:GregHolmes/home-surveillance-system-with-raspberry-pi.git
Once unzipped or cloned, change into the directory.
cd home-surveillance-system-with-raspberry-pi-master/
Then, use npm to install the dependencies for the server and client apps.
npm install
Inside your Raspberry Pis Terminal, change directory to your project path and run the following command to generate a self signed SSL certificate, this is required for Vonage Video to work.
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365
Ngrok allows users to expose their webserver running on a local machine to the Internet without needing to configure their router or expose their IP address. To install Ngrok, inside the project directory run:
npm install ngrok
Using our CLI, set up the service and configure the app with credentials to begin.
nexmo setup <api_key> <api_secret>
Create an application with messages capabilities. The inbound url and status url don't matter at this point as the code will automatically update these when an ngrok instance is created.
nexmo app:create "My Messages App" --capabilities=messages --messages-inbound-url=https://example.com/webhooks/inbound-message --messages-status-url=https://example.com/webhooks/message-status --keyfile=private.key
# Application created: <Application id>
# Credentials written to .nexmo-app
# Private Key saved to: private.key
Create an account on both:
Now, create a .env
file and add the credentials you've now generated.
# Create a Project on Tokbox.com and use api key and secret key found there.
VONAGE_VIDEO_API_KEY=
VONAGE_VIDEO_API_SECRET=
# Retrieve api key and secret key on https://dashboard.nexmo.com/getting-started-guide
VONAGE_API_KEY=
VONAGE_API_SECRET=
# The name of your company / unique to you
VONAGE_BRAND_NAME=
# as generated from `nexmo app:create` above
VONAGE_APPLICATION_ID=
# path to private.key generated by `nexmo app:create` eg. /home/pi/pi-cam/private.key
VONAGE_APPLICATION_PRIVATE_KEY_PATH=
# the number to receive SMS notifications
TO_NUMBER=
# database name and credentials you specified when configuring your database
DB_NAME=
DB_USERNAME=
DB_PASSWORD=
# default host and port, unless specified otherwise
DB_HOST=127.0.0.1
DB_PORT=3306
#Duration for video session on motion detection (In milliseconds)
VIDEO_SESSION_DURATION=
### Run all Database Migrations
There are generated migrations within the migrations/
table. Running the command below creates the specified database tables into the database.
npx sequelize db:migrate
In your Terminal, navigate to the project root directory and type:
node server.js
This will run your server monitoring for motion sensor, then proceeding to start a Vonage Video Session, and SMS your specified number with the link to view.
In the interest of fostering an open and welcoming environment, we strive to make participation in our project and our community a harassment-free experience for everyone. Please check out our Code of Conduct in full.
We ❤️ contributions from everyone! Check out the Contributing Guidelines for more information.
This project is subject to the MIT License