Have you thought about installing a home security system, but hesitated because it’s expensive and you don’t think that you really need it? You are not alone. 🙂
Installing home security system can be costly, but not installing one could cost you even more. The biggest reason why you should consider is that it offers protection to you and your loved ones, your home, and your treasured possessions.
There are many types of home security systems available in the market today. There are systems specifically designed for surveillance and recording purposes, and there are systems that provide more on detection and reporting. Like any other device, cost varies according to its features, efficiency, accuracy, resolution, form factor, etc. It’s hard to choose a system that is right for you off the shelf. That is the reason why most people would prefer to sign contract with home security systems provider that offers customized/tailored fit home security solutions. But not me. 🙂
In this blog, I would like to share how to build our own customized home security system using open-source hardware and software, and a USB camera.
There are actually quite a number of posts related to this out there that does the same thing but programmed differently, but I like the way Marc-Olivier Schwartz put it in one of his posts.
We are going to design here a system that has the following basic features:
- Monitors and detects for movements.
- When movement is detected, it will automatically take a snapshot of the event, save it locally, upload it to the cloud, then remove the local copy of the snapshot to optimise your SD card capacity.
- On certain conditions, takes video and streams them as M-JPEG via HTTP locally or send the stream to YouTube so you can view the live streaming anywhere.
- 1x Arduino Yun (with power supply)
- 1x micro SD Card (at least 1 GB)
- 1x USB video class or UVC Camera
- 1x PIR Sensor
- 1x Pushbutton Switch to trigger and stop video stream
- 1x 220Ω 1/4W pull down resistor for the pushbutton switch
- Jumper Wires
Optional Parts (used for debug and to display status)
- 1x Tri-color LED
- 3x 100Ω 1/4W Resistors
- 1x 16×2 Alphanumeric LCD
- 10KΩ Potentiometer
- Additional jumper wires
Web Service Needed
- Dropbox – Cloud storage service
- Temboo – A layer that acts as an interface between the Dropbox’s API and your Yun.
- YouTube – YouTube Live Event to broadcast our video stream live.
Setting up the Hardware
Mount and connect all the parts according to the schematic diagram above, insert the micro SD card, then connect the camera to the USB port of the Yun and power up the board.
For the USB camera, you can refer to the following for the List of USB video class webcams.
Setting up Temboo Application and Dropbox API
The first service that we need to set is Temboo – a library of programming processes (virtualised codes) that makes the life of the developer, like us, a lot easier. In this case, Temboo will act as an interface layer between the Arduino Yun and Dropbox.
Go over to https://www.temboo.com. Login to your account if you already have, else, sign up for a free account. Once you are in, click “Account”, then Applications and create New Application. Take note of your account name, the name of the application that you just created and your API key, you will need them later.
Then go over to https://www.temboo.com/python to download the latest Temboo Python SDK. Once downloaded, extract the folder to the micro SD card. Yes, correct :), you need to remove the SD card from the Yun, plug it in to your PC, copy the folder over to the root directory, then insert the SD card back to the Yun.
The next service that we need to set is Dropbox. If you don’t have a Dropbox account, you need to create one.
Then go over to the developers section of Dropbox to create an app for you to be able to integrate with Dropbox.
Click “Create App” and follow the snapshot below.
Once you have create your app, write down the “App Key” and “App Secret” because these will be your input in the next step.
Go over to https://temboo.com/library/Library/Dropbox/OAuth/InitializeOAuth, then input the Dropbox App Key and App Secret to get the next set of data for the next authorization process.
You will be asked to visit a URL to grant your application access to your Dropbox data. Write down the OAuthTokenSecret and the CallbackID. You’ll need them, together with the previous two data (App Key and App Secret) to run the final authorization.
Go over to https://temboo.com/library/Library/Dropbox/OAuth/FinalizeOAuth, input the four data you’ve saved previously to get the Dropbox Access Token, Access Token Secret, and UserID associated with the access token and secret. Write them down because you’ll need them later.
Then the last service that you need to set is Youtube. You need to have a google account to stream videos on Youtube. You can create one from here: https://accounts.google.com.
Setting Up your Arduino Yun
The details on how to connect and setup your Yun to your network will not be covered here. It is assumed that your board is already set and properly connected to your WiFi network.
We are going to login to our Yun using SSH from a terminal and we’re going to install all the packages needed for our application. We are also going to test the USB camera to see if everything are ok.
Open a terminal and type the following to connect to your Yun via SSH.
or using your Yun’s private IP address if you know.
If your connection is successful, you should see some nice ASCII art (default is LININO):
We can now install the required packages. We’ll start with an update of the package manager:
opkg install kmod-video-uvc
opkg install python-openssl
fswebcam utility (we will use this utility to take snapshot (picture) of an event)
opkg install fswebcam
mjpg streaming library:
opkg install mjpg-streamer
The installation process should be smooth.
Testing the USB Webcam
Since we have all the drivers and dependencies installed, we are now ready to test the webcam. Make sure that the SD card is mounted into the Yun.
You should be able to see the Temboo Python SDK folder that you extracted earlier.
To test the camera and take a picture, just execute the following command.
You should see something like this:
You should also see the test.png when you do an “ls”
You can check the picture by removing the SD card from your Yun and open the picture on your PC.
Now that the infrastructure (both HW and SW) are set, it’s time to add in the program. The program is categorized into two – the microcontroller sketch (FW) that handles the inputs (triggers), LCD and indicators, and the Shell and Python scripts that handles webcam, snapshot, streams and uploads.
You can get all the files needed here: http://downloads.bitsflipper.com/Docs/Sikyo.zip
Motion detection is done in the microcontroller side. We use a passive infrared (PIR) sensor to detect motion by measuring changes in the infrared (heat) levels emitted by surrounding objects. When motion is detected the PIR sensor outputs a high signal on its output pin. We use this change of the pin state to execute “fswebcam” utility to take a snapshot of the event and save in the SD card first.
Upload Snapshots Automatically to Dropbox
After a snapshot is taken, a Python script “upload_picture.py” will be executed to upload the snapshot to the Dropbox folder we’ve set earlier. There few items that you need to change in order for the script to work. Now is the time to put all those details that you wrote down earlier to use.
Change the following lines accordingly with your Temboo Application details:
# Declare Temboo session and Choreo to upload files
session = TembooSession(‘ACCOUNT’, ‘APPLICATION’, ‘KEY’)
uploadFileChoreo = UploadFile(session)
Change the following lines accordingly with your Dropbox App details:
# Set inputs
You also need to ensure that the folder of the Temboo Python SDK you extracted earlier in the root directory of the SD card is named “temboo“.
Once the upload process is completed, the sketch will execute another shell command to remove the local copy of the uploaded snapshot.
Streaming Video Locally
In this project, for simplicity, we are going to use a pushbutton switch to trigger (start) and stop video stream. When the switch is pressed the first time, the sketch will execute the “streamViaHttp.sh” to initiate mjpg streamer utility to capture JPG frames from a connected webcam and stream it locally via HTTP.
mjpg_streamer -i “input_uvc.so -d /dev/video0 -r 640×480 -f 25” -o “output_http.so -p 8080 -w /www/webcam” &
You can change the resolution to the right number for your webcam.
To test if your setup could stream locally, press and release the pushbutton switch. You should be able to see that you the built-in red LED of your Yun is lit up and if your camera has LED, it should lit up also indicating that the camera is used.
Then go to http://xxx.xxx.xxx.xxx:8080/stream.html or http://YourYunName.local:8080/stream.html to see the live stream.
You should see something like this:
Press again the pushbutton switch to stop streaming. Your onboard LED and camera LED should be turned OFF.
Streaming Live Video to YouTube
In order for YouTube to broadcast your live content, you need to send them an encoded stream. YouTube Live provides support for a variety of encoders, but since I’ve tried Wirecast before without issue, we’ll use the same here.
If you don’t have Wirecast yet, you can get the installer here:
If you already have, don’t launch it yet. Create a new live event first in YouTube.
Once done, launch Wirecast and setup as follows:
- Go to Sources and click New Web Stream… Start demo
- Select “HTTP” for protocol and “Motion JPEG” for “Format Options”
- Tick “Live Stream”
- Put http://YourYunIP:8080/?action=stream for URI. You can also use your home network’s public IP provided that you have forwarded port 8080 to your Yun’s private IP.
- Click the “Connect” button. Video stream should appear in the video frame.
- Click the “Stream” button on the top left corner. It will ask you for destination. Choose “YouTube”. Then authenticate using your Google Account.
- Choose your event and click OK.
- Look for the Master Layer and click the + sign to add camera. Choose the Web Stream Source that we configured earlier.
- Select the source. You should see the video.
Now, go back to YouTube and click the Live event control room, you should see something like this:
Click Preview. It may take a while before you will be able to click “Start Streaming”. Once done, you will be able to view your live video like this:
Congratulations! You just completed your first Customized Home Wireless Security System hack!
There are so many things that you can do in order to make this base design more interesting. For example, you can add feature to automatically turn ON your light or send you a tweet or SMS when motion is detected. You may also want to program the device to monitor only at a specific time period. There are endless tweaks that you can do.
Snapshots of the working setup