Wireless Security Camera with the Arduino Yun

yun_cameraEver saw these wireless security cameras that you can buy off the shelf? These are devices that you can setup somewhere in your home or outside, connect to your WiFi network, and then automatically take pictures if some motion is detected, for example. However, they are usually using the interface given by the manufacturer, which means you are quite limited with what you can do with your 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 sensor to create a cool application.

The application will be a modern version of a standard task that you expect from 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. Excited? Let’s dive in!

Hardware & Software Requirements

The first step is to make sure you have the right hardware components. You need the following components to build this project:

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

Before we can build exciting applications with our hardware, we need to setup some accounts on the web services we are going to use. 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 to 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):

8007_03_03Then, select “Files and datastores”:

8007_03_04

What you need to get now is all the keys relative to your Dropbox app, so you can enter them later in the software part of the project. You will need the App Key and App Secret at this point, which are displayed on the same page as your app.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 authentication. 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.

Hardware Configuration

The first step is to insert the SD card in the Arduino Yun board:

8007_03_05

Then, connect the camera to the USB port of the Yun:

8007_03_06

Finally, connect the motion sensor to the Yun. Simply connect the VCC pin to the Yun 5V pin, GND to GND, and the SIG pin to the Yun pin number 8:

8007_03_07

Finally, just connect the project to your computer via the microUSB port, and you’re good to go!

Testing the Camera

Before we can write the software for our project, we need to install some more software on your Arduino Yun. We are also going to test the USB camera to see if the drivers are working correctly. In the rest of the project, I will assume that your Yun is already setup and connected to your local WiFi network. If it is your first time using your Yun, I recommend following this guide first:

http://arduino.cc/en/Guide/ArduinoYun

First, the UVC drivers for the camera. To install them, you need to connect to your Yun via SSH. Simply open a terminal, and type:

ssh root@yourYunName.local

Where you need to insert the correct name of your Arduino Yun, that you set when you first used the Yun. You will also be prompted to enter your password. If the connection is successful, you should see some ASCII art:

8007_03_09

We can now install the required packages. Start with an update of the package manager:

opkg update

Then, install the UVC drivers:

opkg install kmod-video-uvc

And the python-openssl package:

opkg install python-openssl

We also need the fswebcam utility that we will use to take pictures from the terminal:

opkg install fswebcam

We are now ready to test the webcam. Make sure that the SD card is mounted into the Yun, and go over to the SD card folder with:

cd /mnt/sda1

To test the camera and take a picture, it is really easy. Simply type:

fswebcam test.png

You should see some information being displayed, along with some errors, but don’t worry about them. The important thing is to see these lines:

— Opening /dev/video0…
Trying source module v4l2…
/dev/video0 opened.

To check that the picture was correctly taken, remove the SD card from the Yun and read it using your computer. You should see the picture appearing at the root of the SD card:

8007_03_10

Simply open it to make sure it was correctly taken and that it is not corrupted. If the picture looks good, you can go over to the next section and start building cool applications with the project!

Building the Security Camera

What we want to achieve in this first application is to take a picture whenever some motion is detected by the PIR motion sensor. And when that happens, store this picture locally on the SD card, and upload it to Dropbox. To do so, the code will be composed of two parts. T

he first one is a Python script that will connect to Dropbox, take a picture on the SD card, and then upload this picture to Dropbox. The reason to use Python for this part is that it is much easier to upload files to Dropbox using Python than directly from the Arduino sketch.

The second part of the code will be the Arduino sketch itself, which will basically call the Python script to take pictures via the Bridge library of the Yun.

Let’s first code the Python script. It starts by including the required libraries from the Temboo Python SDK:

The Python script will also take the name of the picture we want to upload as an argument:

Remember these Temboo credentials that you created earlier ? That’s where you need to enter them:

We can then create the correct Dropbox library to upload files, called a “Choreo” on Temboo:

It’s now the time to enter all the informations about your Dropbox account, like your app Key, app Secret, Access Token an Access Token Secret:

And finally, upload the file on Dropbox:

You can now save this code in a file named upload_picture.py. Note that all the files are available on the GitHub repository of the project:

https://github.com/openhomeautomation/arduino-yun-camera

We’ll now work on the Arduino sketch. The sketch starts by including the required libraries:

We also have to declare a process, that we are going to use to call functions on the Linux machine of the Yun (for example the fswebcam utility we used before):

We are also going to build a filename for each picture the project will take, that will be stored in a string:

We also declare the pin on which the PIR motion sensor is connected:

And the path of the SD card on the Yun:

Because we need to call functions on the Linux machine of the Yun, we have to start the Bridge:

Then, in the loop() part of the sketch, we check if some motion was detected by the PIR sensor:

If this is the case, we build a unique filename for the picture, with the date at which the picture was taken:

We then make the first call to the Linux machine of the Yun, first to take a picture with the fswebcam utility. Note that here, we provide an extra argument with the -r command, which set the resolution. I used the maximum resolution of my camera, which is 720p:

We then make a second call to the Linux machine, this time calling the Python script with the name of the picture as an argument, which will upload the picture to Dropbox:

You are now ready to test the project. Again, all the files are available on the GitHub repository of the project:

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:

8007_03_11

You can also check on your Dropbox folder, where the same pictures should have been uploaded. They should be located in your Dropbox apps folder:

8007_03_13

How to go Further

In this project, you learned how to connect a USB camera to your Arduino Yun and build an exciting project on top of it. We built a security camera that automatically uploads pictures on Dropbox when motion is detected by a motion sensor.

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 monitoring system in your home!

  • Pingback: Construire une caméra de sécurité avec une webcam et un arduino | Geekofyou

  • Kyle Cox

    Thank you for the great info!! I was wondering if this can be adjusted to stream video instead of pics. If not, where do you suggest i look. Thank you for any advice you may have.

    • http://www.marcoschwartz.com Marco Schwartz

      Hi Kyle, to my knowledge it is not possible at the moment to stream directly from the Yun to the web. However, it is possible to stream video locally using a dedicated library. You can find more information about that in my Internet of Things with the Arduino Yun book: http://www.packtpub.com/hardware-and-creative/internet-things-arduino-y%C3%BAn

  • Jonathan Oberreuter

    What should I put on “sandbox”?

    uploadFileInputs.set_Root(“sandbox”)

    • http://www.marcoschwartz.com Marco Schwartz

      Hi Jonathan, just leave this like this, sandbox means this app can’t access your other Dropbox files.