Monday 28 April 2014

Recipe for a Webcam Server using Debian

An old "XP vintage" PC
Sorry for this if you're only a casual reader, this is more aimed at people wanting to make the same thing: a kind of self-uploading security camera thing using webcams and old PC.  Hopefully this guide is easy enough to follow for anyone!

I meant to write this up a few months ago, but there wasn't really an audience or platform to do it.  I guess my blog is the best place for it.  I basically wanted to combine and use a few old bits at home and around the office to make a computer that would take pictures from web cams and upload them to the web.  Here's my recipe based on a very hastily put together method, which possibly could've been a lot more secure, a lot more failure resilient and a lot more efficient - maybe suggest changes and I'll link it through.

Ingredients


Time
Probably a couple of hours in all


Skill level
This could be done by anyone, if your ingredients are similar to mine


Overview
The idea is to have a headless (i.e. no need for keyboard/mouse/monitor) computer running Debian that regularly captures webcam images and uploads these via FTP as a cron.  This means I can have it running under a desk, doing it’s job without any input from me.


Notes
Please note this guide is not designed as a “best practice guide”; it is designed to tell you how we did it so you might have a go at doing something similar.


The PC I used was a 64bit machine, mostly working in that all of it worked apart from the optical disc drive - I had to use a USB 'pen' drive to install Debian.


Debian is a funny beast and you might be better using Ubuntu (derived from Debian, but has more non-free sources).



A very old webcam that worked
I also discovered webcams aren’t exactly plug and play - especially the older ones we were using.  I bought a modern one with a fisheye lens, and had three old ones laying around at home and the office.  The only one that we couldn’t get to work was a Microsoft one of a 10 year vintage.

Method
  • You should have a burned disc of Debian, from the link above - it includes the non-free firmwares so you won’t be caught out by hardware needing them.  I'd advise at this stage to have the cameras plugged into the USB ports while installing, plus you’ll need a monitor and keyboard connected to do the installation of Debian.  After this, we’ll move to Terminal App in OSX/Putty on Windows.  Also, make sure your box will boot to an OS without a monitor and keyboard before leaving this stage - use BIOS settings to confirm “halt on errors” or the like is not set.
  • Install Debian as one big drive, set up users and note these down - you’ll need these.  Make sure you uncheck installing a user interface (you won’t need it!) and you have selected to be an SSH server.
  • Once you’re back and installed the base system, you’ll need to fix the IP and connect via SSH.
    • Change to root user:su
    • Open interfaces in nano:nano /etc/network/interfaces
    • Change this to be:# This file describes the network interfaces available on your system
      # and how to activate them. For more information, see interfaces(5).

      # The loopback network interface
      auto lo
      iface lo inet loopback

# The primary network interface
#allow-hotplug eth0
#iface eth0 inet dhcp
auto eth0
iface eth0 inet static
       address
192.168.0.100
       netmask
255.255.255.0
       gateway
192.168.0.1
Change the last three lines to match your set up - the address line should be the address you’d like the system to stick to for SSH access.
  • Save the file (ctrl+O), and exit nano (ctrl+X)
  • Try and connect via SSH on your normal system, on OSX open terminal app and type:ssh username_you_setup@the_ip_address_of_system
    • you should get a question about security keys - this is fine, just accept and type in the password you set up debian with.
    • if all is well, you should be logged in as a system user on your debian box.
  • We need to check which (if any) of the webcams are working.  These are set up as devices and we’ll see a numerical list of video capturing devices.
    • So, change to root if you’re not already:su
    • Change directory to the devices directory:cd /dev/
    • And list the devices:ls -la
    • You should see a list of “video” devices, numbered from 0.
    • If you’ve got less video devices than cameras attached, one or more do not work - this guide will not tell you how to get a camera working, you’re probably best to buy a newer one and check if it works with Linux in general.
  • Okay, we’ve got some cams working but we need to make them stick to particular references: as the system boots, the device’s video number may change and if you’ve got a motley crew of cams like us, they’ll all need different settings in a later stage.  We need to use rules for udev, which is a facility that reacts to usb and other devices being connected.  They also change when you unplug and plug, too
    • Change to the udev rules folder:cd /etc/udev/rules.d/
    • Probe the devices in turn (changing video0 to each camera):udevadm info -a -n /dev/video0
    • Copy the outputs to text editor windows for perusal and then create a new rule file:nano 00-webcam.rules
    • Okay blank file, and here’s where your mileage may be very different to mine.  Hang in there, it’ll be fine if you use a bit of common sense - but do have some patience.
      • Remember the probing output?  Okay, that’s the USB tree of how the computer speaks to the device.  The way I understand - or at least attempt to explain this - is that the top-most output is the device itself, and as you go down the entries, you’re approaching the PC itself.
      • Mine has a section like this:
        looking at device
        '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1:1.0/video4linux/video2':
           KERNEL=="video2"
           SUBSYSTEM=="video4linux"
           DRIVER==""
           ATTR{name}=="Logitech QuickCam Zoom"
           ATTR{index}=="0"
           ATTR{button}=="1"
      • udev works by matching rules to given search criteria for a device when connected - so in this case, I used one piece of information
        • ATTR{name}=="Logitech QuickCam Zoom"
      • Basically, the rough guide is you can use any information to identify a device (some devices don’t give you a name, like this one unfortunately) but not from multiple parent devices.
      • So our rule looks like this:
        • SUBSYSTEM=="video4linux", ATTR{name}=="Logitech QuickCam Zoom", SYMLINK+="webcam1"
      • To explain this, it looks on SUBSYSTEM, then looks for ATTR{name} and for this device creates a symlink (a copy of the device reference) as webcam1.  You might set up similar rules for webcam0, webcam2, etc.
      • Save the file (ctrl+O) and exit (ctrl+X)
      • We restarted at this stage and probing the devices we created (webcam0 etc) revealed our rules worked and the expected cam was on the right number.  Note: webcam0 might actually be a symlink to video3, so you’ll use the webcam0 referencing.  You might need to use different ATTR or what-have-you until the rule works.  Patience, be a good Hitchhiker and don’t panic.
  • Now we created a new user called webcam and gave it a password.  We added this user to the group “video” to allow it access to the devices.  You might not want to use this step, but adding users etc. can be found searching Google for full explanation, but here goes:
    • useradd -G video webcam
  • I restarted here to make sure everything worked and checked webcam user could log in and access video devices via the webcam symlinks.
  • Next step, install fswebcam tool and ncftp from the repository as root (there may be prerequisites, that’s fine):
    • su
    • apt-get install fswebcam
    • apt-get install ncftp
  • Okay now switch to the user webcam (or the user you’ll be running to upload the images):
    • su webcam
  • ..and make sure you’re in that home directory..
    • cd ~/
  • ..and create a new batchfile to run.
    • nano webcamrun.sh
    • contents:sleep 1
      fswebcam -c webcam0.cfg
      ncftpput -f ftp.cfg /folder/on/your_webserver live0.jpg
      rm live0.jpg
    • Replace the references “live0.jpg” and “webcam0.cfg” with the same reference as your first webcam device you created earlier.  Put the folder reference (from the root of the FTP user’s directory) to where you want each uploading.
    • Also, repeat these lines in this file for each different webcam device.
  • Now we need to write the “webcam0.cfg” (etc.) and “ftp.cfg” files:
    • FTP config file (all cams use same FTP):
      • nano ftp.cfg
      • Contents:host HOSTNAME_OR_IP
        user
        USERNAME
        pass
        PASSWORD
    • Webcam0.cfg (name multiple):
      • nano webcam0.cfg
      • Contents:device /dev/webcam1
        jpeg 95
        resolution
        640×480
        title "
        TITLE OF CAM"
        delay 1
        set "White Balance Temperature, Auto"=True
        set "Backlight Compensation"=0
        set "Brightness"=40%
        set "Contrast"=60%
        set "Saturation"=30%
        set "Gamma"=3%
        frames 10
        save "
        live1.jpg"
      • change the “webcam1”, “live1.jpg” references to match the device you want and jpg reference in the .sh batch script for upload.  This was the best settings for an old camera, have a look round for settings and play - we found these were not exactly a two minute affair to get the best result, and you can edit these files later.
  • Once you’ve written a webcam config file for each cam and an FTP config, you can test the script:
    • ./webcamrun.sh
    • If you get any errors, fix them.
  • Now, we do the cron.
    • I found it easier to switch to root, and create from there, so:
      • su
      • crontab -u webcam -e
      • Change “webcam” to the user in which you set up the .sh batch file.
      • Add the following line for every minute, of every hour, of every day:
        • * * * * * ~/webcamrun.sh
      • ctrl+o, ctrl+x
  • Done!
    • Restart and if it didn’t fail when you tested before all should be hunky dory in the cron.


A more modern webcam that we found easiest to install


Sources
While I was fighting through doing this I used several guides, but none of them supplied the whole answer, hence this guide.  The following (in no particular order) were completely indispensable though:



No comments:

Post a Comment