Raspberry Pi

This is the toy I wish I had when I was 12. Back then, any computer part used to cost a small fortune.

The current model B offers:

  • 512Mb RAM
  • ARM11 CPU (ARM v6 instruction set)
  • Broadcom VideoCore IV @250MHz GPU
  • 2 USB ports
  • 100Mbps Ethernet
  • HDMI 1.4 output
  • Audio on jack or via HDMI

For 36€ on Amazon, it's a bargain. Do not forget to add to your cart:

  • A fast SD card with enough space, for instance, SanDisk SDSDU-008G-U46 class 10 8Gb. The SD card is a bottleneck so it's no place to save a few euros on a slower model.
  • Micro USB power supply. I bought a 5V 1500mA to be able to plug a USB disk (or more) without the need for an external power supply.

Optional: a box. Buy it or DIY. Professional style, even more sturdy design or hacked from the kitchen. You can even steal some Legos from a kid. It's really up to you.

For the moment, I left it nacked, standing next to a wireless access point.

Raspbian

You have the toy. Minimal assembly required. You need an OS. For general purpose, you can go for Raspbian, a GNU/Debian derivate for the Pi.

Follow the instructions. It boils downs to these steps:

  • Format your SD card using an appropriate tool (regular format commands won't work)
  • Download NOOBS (New Out Of Box Software). Choose "offline and network install" if you plan to fiddle with the material and if you suspect you'll be re-installing from scratch soon
  • Unpack NOOBS to your SD card
  • Plug the SD card into the Pi slot
  • Plug the Pi to an HDMI monitor or TV
  • Plug a USB keyboard (a USB mouse is nice as well, as some options are difficult to access or are even unreachable during the installation procedure without a pointing device)
  • Plug the power cord (no switch)
  • Follow the steps on the screen

Do not forget to enable ssh(d) if you plan to keep your PI away from your screen and to access it from your computer (recommended).

Fixed IP

Do not unplug the monitor just yet. By default, Raspbian will use DHCP to acquire an IP address on your network. Since my Pi won't be connected to a screen nor to a keyboard, I prefer a fixed IP so that I don't have to nmap my network looking for the precious circuitry.

pi@raspberrypi ~ $ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether b8:27:eb:10:a2:75 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.64/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever

pi@raspberrypi ~ $ netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0

Currently, the Pi dynamicly assigned address is 192.168.1.64 (actually, it's the first available DHCP address in my network, that's how it's configured here).

So I have:

  • broadcast address 192.168.1.255
  • gateway address 192.168.1.1 (my main Scarlet router)
  • netmask set to 255.255.255.0
  • network is 192.168.1.0

I want to assign 192.168.1.6 to my PI. Make sure the address is compatible with your network settings and of course avoid using an address already in use.

Backup your current network interface configuration file:

pi@raspberrypi /etc/network $ sudo cp interfaces interfaces.ori

Edit it (I use vi because I was traumatised during my studies but feel free to alleviate your pain by launching nano instead)

pi@raspberrypi /etc/network $ sudo vi interfaces

auto lo
iface lo inet loopback

# iface eth0 inet dhcp
iface eth0 inet static
address 192.168.1.6
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

Save and quit (:wq). Inspired from this page. Reboot: sudo reboot.

After a few seconds, you should be able to connect to your Pi at its new address:

youri@gyros ~> ssh pi@192.168.1.6
pi@192.168.1.6's password:
Linux raspberrypi 3.10.25+ #622 PREEMPT Fri Jan 3 18:41:00 GMT 2014 armv6l
Last login: Tue Feb  4 11:24:42 2014 from 192.168.1.65

The camera

To watch what's going on in my backyard, I'll plug an old Linksys Wireless-G Compact Internet Video Camera WVC54GC. It used to be the only cheap IP camera, below 100€ back then. With its limited power, its limited firmware and a staggering 320x200 resolution, don't expect anything fantastic here. But it was a sturdy a slick design for a sweet price. It still runs fine after many years.

It is an IP camera, meaning you don't have to (and you actually can't practically) plug it directly to the Pi. Plug it anywhere in your network next to an ethernet cable, or run it wirelessly (less reliable and less performant).

Again, setting a fixed IP is a nice thing:

Don't bother setting up the clock; it will be lost as soon as you unplug the camera and the poor thing is to dumb to resync on its own. We'll let avconv add the timestamp (more on that later).

The camera ships with an ActiveX to view the stream. If you don't use IE, you're out of luck. If you're not on Windows, you're out of luck (or are you?). You can still access it from any decent player, be it mplayer or VLC:

File -> Open Network

Stream visible at http://192.168.1.xxx/img/video.asf

Make sure you change the default username/password and to only allow access to authentified user. By default, anyone on the network (and so possibly anybody in the neighborhoud if wifi security is disabled) can access the video stream. In User, select "Only users in database" next to "Allow access by" to restrict access. You can access the stream with:

http://username:password@192.168.1.xxx/img/video.asf

AFAIK, there is no way to turn wifi off on that thing, and the best available version of security (if you have one of the latest firmware) is WPA/TKIP. Fortunatelly, it seems that wifi is turned off - or at least not responding - when a network cable is plugged to the camera. Nice side-effect.

This is what you can get from the camera (original resolution):

Told you, it's old and cheap. You can actually see the camera reflection in the window as it is tilted downwards. Not an ideal position. If you record from behind a window, it's better to keep the camera level but then I'd be recording the tree canopy and the sky.

Recording

Use avconv (formerly ffmpeg) to record from the camera. I've decided to capture 10 images per minute rather than the video stream to avoid clogging the network. Remember that our "IP" camera is accessed on the network.

For increased flexibility, I've stored the command in a tiny script.

pi@raspberrypi ~ $ cat capture.sh
#!/bin/bash

TS=`date +"%Y%m%d%H%M"`
FOLDER="/home/pi/ipcam/$TS"

mkdir $FOLDER
avconv -i http://username:password@192.168.1.xxx/img/video.asf -vsync 1 -r .1 -an -vf "drawtext=fontfile=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf: text='%d/%m/%Y %H\:%M': fontcolor=white@0.8: x=7: y=460" $FOLDER/frame%d.jpg

Frames will be stored in a sub-folder in /home/pi/ipcam. Because my avconv version has no -n option to avoid overwrite, I create a new folder based on the current time each time the script is started. That way, images are never overwritten in case the Pi is rebooted.

-r .1 means "0.1 FPS", equivalent to 10 frames per minute. The drawtext will add a timestamp to each frame (remember, you can't rely on the camera to do it for us). Nevermind the y=460 outside the image boundary (320x200). The program is lenient and the timestamp will be visible at the bottom of the frame. You must specify a fontfile path as there is no default value.

chmod u+x capture.sh

Test it: ./capture.sh. You'd want it to run it automatically everytime the Pi is started.

pi@raspberrypi ~ $ sudo ln -s /home/pi/capture.sh /etc/init.d/capture
pi@raspberrypi ~ $ sudo chmod 755 /etc/init.d/capture
pi@raspberrypi ~ $ sudo update-rc.d capture defaults

Reboot: sudo reboot. Your ipcam folder should be filling up. How much? Pretty much. 140MB/day or 984Mb/week or approx 4Gb/month. I don't want to keep more than 1 day worth of image (or garbage), so let's add a cron to clean up on a daily basis:

pi@raspberrypi ~ $ cat cleanup_cam.sh
#!/bin/bash

FOLDER="/home/pi/ipcam"
find $FOLDER -type f -name "*.jpg" -mtime +1 -exec rm {} \;
find $FOLDER -type d -empty -exec rmdir {} \;

pi@raspberrypi ~ $ sudo crontab -l
0 1  *   *   *     /home/pi/cleanup_cam.sh

Deleting files on a SD card is no small task. Deleting 3 days of frames took more than 2 minutes!

pi@raspberrypi ~ $ time ./cleanup_cam.sh
real    2m18.666s

Wow, that's slow. The Pi is doing well considering the constrained CPU. If you do a top:

 PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND
3069 pi        20   0 38472  11m 3628 R  22.7  2.6   1285:39 avconv
4386 pi        20   0 10732 7268  768 S  13.6  1.6   0:10.99 find

That's about 23% for recording and 14% for finding and deleting older images... Recording the video stream was about 45% alone (that's still ok).

Random tips

Always shutdown your Pi cleanly, aka do not unplug the power cord but instead, issue the following command:

pi@raspberrypi ~ $ sudo shutdown now

Or, to reboot:

pi@raspberrypi ~ $ sudo reboot

Not sure Raspbian offers the latest software packages from GNU/Debian. Even after apt-get update && apt-get upgrade, I'm still running a slightly older version of avconv:

pi@raspberrypi ~ $ avconv
avconv version 0.8.6-6:0.8.6-1+rpi1, Copyright (c) 2000-2013 the Libav developers
built on Mar 31 2013 13:58:10 with gcc 4.6.3
Hyper fast Audio and Video encoder

Although the Debian wheezy package is libav-tools (6:0.8.9-1) [security].

But that may be considered "acceptable" for a toy.

Next time: Raspberry Pi as a media center with HiFi capabilities.