The first application we are going to build with the hardware that we just step up will be only local, so nothing will be sent to the Web yet. In this section, we just want to build a camera that will be triggered by the motion sensor.
With this, you can, for example, check whether somebody entered your home while you were not there because the PIR motion sensor would instantly notice it. This section is really the foundation of the whole project. We are going to reuse the code developed in this section later when we write the piece of code to upload pictures to Dropbox.
For this part of the project, we don't want to use the SSH access to take pictures anymore; we need to trigger the camera right from the Arduino sketch. For this, we are going to use the Bridge
library and the Process
library to call a command on the Linux machine, just as if you were typing it on a terminal window.
The sketch starts by declaring the libraries that we need to use:
#include <Bridge.h> #include <Process.h>
To call some commands on the Yún's Linux machine, we will need to declare a process, which is an object that we will call to emulate some terminal entries:
Process picture;
We'll also build a filename for each picture that will be taken, as shown in the following line of code. Indeed, we named the file test.png
earlier, but in this application, we want every picture taken by the project to have a different name:
String filename;
Declare the pin on which the motion sensor is connected, as follows:
int pir_pin = 8;
We also need to define where the pictures will be stored. Remember, we want to store them all on the SD card, as follows:
String path = "/mnt/sda1/";
You can also store pictures locally on the Yún, but it would quickly saturate the memory of the Arduino Yún.
Then, in the setup()
function, we start the bridge between the Atmel microcontroller and the Linux machine of the Yún, as follows:
Bridge.begin();
Also, we set the pin of the PIR motion sensor as an input, as follows:
pinMode(pir_pin,INPUT);
In the loop()
function, what we want to do is to continuously read data from the motion sensor and trigger the camera if any motion is detected.
This is done by a simple if
statement that checks the sensor's value, as follows:
if (digitalRead(pir_pin) == true)
Then, if some motion is detected, we need to prepare everything to take the picture. The first step is to build a filename that will contain the date on which the picture was taken. To do so, we are using the Linux date command that outputs the current date and time. This is important because we want to know what time the picture was taken at and give a unique filename to every picture. At the end, we also want to specify that this picture will be taken in a PNG
format. The filename formatting part is done by the following code:
filename = ""; picture.runShellCommand("date +%s"); while(picture.running()); while (picture.available()>0) { char c = picture.read(); filename += c; } filename.trim(); filename += ".png";
Finally, we can take the picture. What we are going to do here is to call the fswebcam
command again using the runShellCommand
function of our picture process that will emulate a terminal entry.
We also want to use the maximum resolution available on the camera. In the case of the camera we chose, it was 1280 x 720 (standard HD resolution). We have quite a lot of space available on the SD card (4 GB with the one I used), so you can use the maximum resolution without running into problems. We recommend that you use a dedicated SD card for this project so that you don't run into problems with other files that could be stored on the card. For the sake of simplicity, we won't add an automated check to see whether the card is full, but this is something you should consider if you want to let the project run continuously over time. You can specify the resolution using the –o
command at the end of the call. Finally, we can build the complete code to take a picture:
picture.runShellCommand("fswebcam " + path + filename + " -r 1280x720"); while(picture.running());
Note that we are also using a while()
statement here to make sure that the fswebcam
utility has enough time to take the picture. The complete code can be found at https://github.com/openhomeautomation/geeky-projects-yun/tree/master/chapter3/triggered_camera.
You can now upload the code to the Yún board and test the project. Once it's uploaded, try moving your hand in front of the sensor. The Arduino Yún should trigger the camera to take a picture and save it to the SD card. To make sure that a picture was taken at this point, you can simply check on the camera itself. For example, the Logitech webcam that I used has a small LED that turns green whenever it is active.
After a while, remove the SD card from the Arduino Yún (as earlier, unmount the SD card from the Yún first), and put it in your computer with the adapter we used earlier. You should see all the pictures that were taken at the root of the SD card, as shown in the following screenshot:
Again, check these pictures to make sure that they are not corrupted and that everything worked as planned.