How-to: Build a Arduino Yún powered cat camera

Posted by | January 12, 2014 | Actually useful, Geeky Nonsense | 6 Comments

UPDATE (31-03-2014): An awesome tutorial is now available on the Adafruit learning system website. You might want to check this out!

I have an Arduino Yún for some time now, but never really had time to do create something with it. That’s a pity. It’s such an awesome tool and if you didn’t believe the possibilities to create whatever you want were endless with an Arduino Uno, with the Yún you will definetely feel enlightened! It has a little Linux machine on board – together with an USB host -, onboard wi-fi + ethernet and a uSD card slot (see product page for more specs). It’s really pimped. Despite the fact that I didn’t know anything about Linux I was able to put together a little project this weekend: The Yún grabs pictures from a connected USB Webcam every 5 minutes and saves it on its micro-SD card. From there the newest photo gets uploaded automatically to this webspace here where a little script transforms all the pictures in one folder to a nice little photo gallery (that is showing empty photos without cats – what a pity!). The last step is optional of course, but this way it enables me to see my cats from everywhere – if they are in their favorite spot underneath the living room table. I could see this coming in handy in many areas: Combined with an PIR motion sensor as “wildlife” sensor outside your window; in the lab for constant observation of your animals …
It was quite nice to finally get it to work. So for future reference and for those that are interested to build their own, here is a step-by-step tutorial (after the break).

Step1: Pimp your Yún with the MJPEG Streamer library for openWRT. The easiest is just to download this package here, unzip itand put it onto an SD card. Then SSH into your Arduino with your terminal program using ‘ssh root@yuns:ip:address’ to gain access. Then go to your SD card on the Yún with ‘cd /mnt/sda1/your_folder_on_sd_card’ and use the package manager to install the desired *.ipk files (e.g. opkg install your_ipk_file_name).
I have to say it didn’t work well, but I just ignored some nasty error messages and in the end it still worked … be brave!

Step2: Test whether the MJPEG Streamer works with your camera by plugging it into the Yún and typing ‘dmesg’ or ‘lsusb’ into your terminal window. It will show you status messages and a list of connected (recognized) USB devices.
If it shows up you are lucky and can proceed by typing in this cryptic shell command:

 mjpg_streamer --input \"input_uvc.so --device /dev/video0 --fps 1 --resolution 640x480 -yuv\" --output \"output_file.so -f /mnt/sda1/images  -d 300000 -s 1\" 

If you read it slowly you can decipher two parameters, the “–input” and “–output” tags. Input is specifying everything relevant to how the USB camera pictures will be read (resolution, framerate…) and “–output” what will be done with the photos on your Yún. In this case the output just consists of a output_file tag that writes photos with a delay of 30 seconds (-d) to the SD card at ‘/mnt/sda1/images’, keeping only the most recent photo (‘-s 1′). I added this last tag to make it super easy for me to identify the latest picture that I want to upload to my webspace. If you want to record an archive on your SD card you would delete that tag of course. If you want to read more about the MJPEG streamer parameters, take a look at this website here. To test the grabbing capability quickly, you might want to try to access the images via your browser (‘your_yuns_ip_adress:port/?action=stream’) after you specified an optional -www tag (see website for documentation), you can see a constant stream of photos coming from your camera. The maximum framerate at 640×480 I could achieve was 5 (fps). You might get more out of it, if you play around (compression, … ), but for me it was okay like that.

Step3: Now you program your Arduino. I provided my code down here, or you can download it as a .txt file here. It’s quite short, the only complicated things were the shell commands. Turns out there is a serial connection between the Arduino and the Linux side on the Arduino Yún that can be initialized with the “bridge” library. You can just send a shell command to the Linux side and retrieve output via this serial connection. A simple ‘.trim()’ command to delete trailing whitespace cost me more hours than originally planned for the whole project. I highlighted the shell commands down here … The second shell command (line 31) just returns the name of the latest photo. The last statement is a CURL command that makes it SUPER EASY! to upload any file from your Arduino to a remote host. Check the CURL documentation for more awesomeness! I commented out the “Serial.prinln()” commands because they are just to see parts of the code in action via the serial monitor …
There are some gallery scripts that automatically generate a photo gallery based on the type and number of images in a specific folder on your server – I used the “Uber Gallery” script. It is very easy to use!

So far so good, hope you enjoyed this little tour!

// upload from yun SD card to FTP
// initialize Camera grabbing op with mjpg_streamer!

#include <Process.h>
char c;
String cc = "";
int led = 13;

void setup() {
  // Initialize the Bridge and the Serial
     delay(60000);
     //Serial.begin(9600);
     //while(!Serial);  // wait for Serial port to connect.
     //Serial.println("Save pictures\n");
     Bridge.begin();
     Process q;
     q.runShellCommandAsynchronously("mjpg_streamer --input \"input_uvc.so --device /dev/video0 --fps 1 --resolution 640x480 -yuv\" -o \"output_file.so -f /mnt/sda1/images  -d 300000 -s 1\"");

     pinMode(led, OUTPUT);
     for(int i = 1; i<20; i++){
     digitalWrite(led, HIGH);
     delay(100);
     digitalWrite(led,LOW);
     delay(100);
     }
    }

void loop(){

    Process p;
    p.runShellCommand("cd /mnt/sda1/images/ && ls");
    while (p.running());
    while (p.available()) {
      c = p.read();
      cc = cc + c;
    }
    cc.trim(); // IMPORTANT !!!

    digitalWrite(led, HIGH);
    //Serial.println(cc);

    p.runShellCommand("curl -T /mnt/sd/images/" + cc + " ftp://ftp.server.com" + cc + " --user user:password ");

    digitalWrite(led, LOW);
    //Serial.println("Waiting 10 sec!");
    //Serial.flush();
    delay(300000);
    cc = "";

}
  • Andrew Ward

    OK… VERY NICE… But what kinds of USB cameras are compatible with Yun / Linino?

  • Olivier

    Thank you for this tutorial. Do you know where I could find the libraries for the i586 architecture ? in .IPK format ? Thanks.

    • Horst Obenhaus

      sorry, no! You would have to search for yourself … but I’m sure you already did that ,-) good luck!

  • RLodhiya

    Hi,

    Thank you for this tutorial. It’s just Brilliant. I have built my own version of time-laps gallery using Yun and usb camera. And its working, you can visit http://yungallery.ravilodhiya.com/ this will update every hour and collect picture from my garden.

    Ciao.
    RL

    • Horst Obenhaus

      AWESOME!