Introduction
The following guides are more up to date:
- https://learn.adafruit.com/adafruit-io-home-security for IR detection + cloud
- https://learn.adafruit.com/cloud-cam-connected-raspberry-pi-security-camera for camera
In this project, we are going to build our own DIY version of such devices. The project is based on the Arduino Yun, to which we are going to connect a standard USB webcam and a PIR motion detector to create some cool applications.
The first application will be a modern version of standard tasks that you want for a security camera: taking pictures when some motion is detected. The project will store pictures taken by the USB camera on an SD card inserted into the Yun, but that's not all. Because we are in the age of the Internet of Things, we also want these pictures to be automatically uploaded on a secure location. And that's exactly what we are going to do by uploading the pictures to Dropbox at the same time.
Then, we are going to stream video coming from the camera directly on Youtube. At the end, you will have your own video stream accessible from anywhere in the world so you can check what is going on in your home. This way, you can also just share the link with your family or friends, so they can monitor your home when you are not there. Excited? Let's dive in!
Page last edited March 08, 2024
Text editor powered by tinymce.
Connections
For the USB camera, you can choose any webcam that is compatible with the UVC protocol. Most of the recent webcams are compatible. I choose a Logitech C270 that can take pictures up to 720p resolution. You can find a list of compatible cameras here:
http://en.wikipedia.org/wiki/List_of_USB_video_class_devices
The first step is to insert the SD card in the Arduino Yun board:
Page last edited March 08, 2024
Text editor powered by tinymce.
Setting up your Temboo & Dropbox accounts
The first one is Temboo, where you will need to have an account. Temboo will basically make the interface between the Arduino Yun and Dropbox. Just go over to:
https://www.temboo.com/
You will be prompted to create an account, and then your first app. Write down the name of your account, the name of the app and your app API key, you will need them later. There is also one more thing you need from the Temboo website: the Temboo Python SDK, that we will use later to upload pictures on Dropbox. You can get it at:
https://www.temboo.com/python
Once downloaded, simply extract the folder on the microSD card. Then, you need a Dropbox account. Go over to the Dropbox website to do so:
https://www.dropbox.com/home
Once the account is created, you need to create an App so the Yun can upload pictures to your Dropbox folder. To create an app, you need to go to the developers section of Dropbox:
https://www.dropbox.com/developers/apps
Then, click on “Create app”, and choose which type of app you want to create (Dropbox API app for our project):
The next data we need to get is the Token Key & Token Secret key. To get them, the first step is to go to the InitialiseOAuth Choreo on the Temboo website:
https://temboo.com/library/Library/
Dropbox/OAuth/InitializeOAuth/
Here, you will need to enter the App Key and App Secret. That will generate some additional information, like a callback ID, and a temporary token secret. You'll also be asked to visit a link to Dropbox to confirm the authentification. Finally, go to the FinalizeOAuth page to finish the process. You'll be asked to enter your App Key, App Secret, callback ID and temporary token secret:
https://temboo.com/library/Library/
Dropbox/OAuth/FinalizeOAuth/
After that step, you'll be given your final Token Key and Token Secret. Write them down, you'll need them later.
You will also need to have a Google account to stream videos on Youtube. Simply create one at:
https://accounts.google.com/
Page last edited March 08, 2024
Text editor powered by tinymce.
Setting up your Arduino Yun
First, the UVC drivers. To install them, you need to connect to your Yun via SSH. Simply open a terminal, and type:
opkg update
opkg install kmod-video-uvc
opkg install python-openssl
opkg install fswebcam
opkg install mjpg-streamer
cd /mnt/sda1
fswebcam test.png
--- Opening /dev/video0... Trying source module v4l2... /dev/video0 opened.
Page last edited March 08, 2024
Text editor powered by tinymce.
Upload pictures to Dropbox
Let's first code the Python script. It starts by including the required libraries from the Temboo Python SDK:
from temboo.core.session import TembooSession from temboo.Library.Dropbox.FilesAndMetadata import UploadFile
with open(str(sys.argv[1]), "rb") as image_file: encoded_string = base64.b64encode(image_file.read())
session = TembooSession('yourTembooName', 'yourTembooApp', 'yourTembooKey')
uploadFileChoreo = UploadFile(session) uploadFileInputs = uploadFileChoreo.new_input_set()
uploadFileInputs.set_AppSecret("appSecret")
uploadFileInputs.set_AccessToken("accessToken")
uploadFileInputs.set_FileName(str(sys.argv[1]))
uploadFileInputs.set_AccessTokenSecret("accessTokenSecret")
uploadFileInputs.set_AppKey("appKey")
uploadFileInputs.set_FileContents(encoded_string)
uploadFileInputs.set_Root("sandbox")
uploadFileResults = uploadFileChoreo.execute_with_results(uploadFileInputs)
https://github.com/openhomeautomation/arduino-yun-camera
We'll now work on the Arduino sketch. The sketch starts by including the required libraries:
#include <Bridge.h> #include <Process.h>
Process picture;
String filename;
int pir_pin = 8;
String path = "/mnt/sda1/";
Bridge.begin();
if (digitalRead(pir_pin) == true) {
filename = "";
picture.runShellCommand("date +%s");
while(picture.running());
while (picture.available()>0) {
char c = picture.read();
filename += c;
}
filename.trim();
filename += ".png";
picture.runShellCommand("fswebcam " + path + filename + " -r 1280x720");
while(picture.running());
picture.runShellCommand("python " + path + "upload_picture.py " + path + filename);
while(picture.running());
https://github.com/openhomeautomation/arduino-yun-camera
First, put the Python file at the root of the SD card, and put the SD card back into the Arduino Yun board. Then, upload the Arduino sketch to the Yun. Now, try to trigger the motion sensor, for example by waiving your hand in front of it. You should see that the webcam is being activated shortly after (for example, my webcam has a LED that turns green when it is active).
To check that the project is working correctly, after a while you can check the SD card, you should see that some pictures have been recorded:
Page last edited March 08, 2024
Text editor powered by tinymce.
Stream video to Youtube
Streaming video locally is actually really easy, thanks to the software we installed earlier. Just log to the Yun again via SSH, and type:
mjpg_streamer -i "input_uvc.so -d /dev/video0 -r 640x480 -f 25" -o "output_http.so -p 8080 -w /www/webcam" &
http://myarduinoyun.local:8080/stream.html
In Wirecast, you need to add a new "Web stream" with the following parameters: HTTP protocol, Motion JPEG format, and "nameofyourYun.local:8080/?action=stream" as the URI. This picture shows the parameters I used:
Page last edited March 08, 2024
Text editor powered by tinymce.
Summary
Of course, there are several ways to build other cool applications using this project. You can for example drop the motion detection part, and build a camera that take snapshots at regular intervals and upload these on Dropbox. You can for example easily create time-lapse videos with this kind of project: just collect the pictures from your Dropbox account, paste them into a time-lapse software, and done! You can also extend this project by adding more Yun + camera modules, to have a complete video monitoring system in your home!
Page last edited March 08, 2024
Text editor powered by tinymce.