May 212013

I recently had an email from one of my readers enquiring about making timelapse videos with the Raspberry Pi.

Since I already had a webcam connected to one of my Pis, I set about making it take timelapse shots.
There are a myriad of ways to accomplish this, but I decided to use motion as it took the least configuration to get it to do what I wanted it to do.

Getting motion was as easy as apt-get install motion on my Raspbian powered Pi.
I also needed mencoder to encode the resulting images into a video. Mencoder is also found in the repositories, so a quick apt-get later and I also had mencoder installed.

In order to get timelapse shots, I had to setup motion to take pictures at intervals.
On line 295 in the default motion.conf, there is this following line

snapshot_interval 0

Change that to the number of seconds between each snapshot.

snapshot_interval 5

I’m also allowing remote access to the webcam so that I can check in on it.
On lines 413 and 429, change the webcam_localhost and control_localhost to off.

After changing those, I restarted the motion daemon for the changes to take effect.
Once motion is started, you will start seeing files in /tmp/motion (or wherever you decided to save the files)
The ones ending in -snapshot.jpg are the ones that we will be using for the timelapse movie.
If you have motion detection activated, you will see other files in the directory but we can ignore those.

To create the movie, we are going to use mencoder. This part could also be done with ffmpeg, which I may cover in a later post.
I wrote up a small script to run the mencoder command –

mencoder mf:///tmp/motion/*-snapshot.jpg -mf w=320:h=240:fps=25:type=jpg -ovc copy -oac copy -o output.avi

The resolution (-mf w=320:h=240) should match the resolution setup in motion.
When the script is run, it will take all files in /tmp/motion/ that end in -snapshot.jpg and make a movie out of them. The output file is set by the -o switch, in this case I’ve used output.avi.
Setting this script to run once a minute for example, will keep the timelapse video up-to-date to the last minute.

Once the file has been created, you’ll be able to view the file on another computer very easily.
The only issue with this timelapse movie, is that it will keep getting longer and longer and longer, as there is nothing cleaning up the old files.

So what we’ll do, is add an extra line into the script to remove any files older than a certain time, and that way we can control the length of the timelapse movie.
If we wanted to make the timelapse movie 10 minutes only, we’d add the following line before the mencoder line

find /tmp/motion -name "*.jpg" -type f -mmin +10 -delete

Resulting in this

find /tmp/motion -name "*.jpg" -type f -mmin +10 -delete
mencoder mf:///tmp/motion/*-snapshot.jpg -mf w=320:h=240:fps=25:type=jpg -ovc copy -oac copy -o output.avi

That way, whenever the script is run, we’ll have a 10 minute long timelapse video !

May 162013

So lately, I’ve been trying to use one of my Raspberry Pis as a WiFi bridge.
That is connecting the Pi to a WiFi network, and sharing it out via the ethernet port.
I was trying to do that with the RT5370 USB sticks that I was also using to broadcast hotspots.

With the default drivers in Raspbian however, trying to add wlan0 to a bridge results in this message

can't add wlan0 to bridge br0: Operation not supported

According the the drivers, wlan0 can’t do it.
The only way to add the RT5370 NIC to a bridge is to activate 4addr (4 address frame) mode.

iw wlan0 set 4addr on

However, once 4addr mode is on, traffic between the WiFi device and the AP seemed to stop altogether.

The solution to this issue, is to use the RALink drivers. The vendor drivers does not seem to use the Linux WiFi stack however, which means that those drivers can not be used to broadcast a hotspot using hostapd. So if you’re using a Pi as a WiFi repeater, you may have issues there, though that will be something I’m going to experiment with later as well.


Before we can actually compile the drivers, we’re going to need to grab the kernel sources.
I’m using Raspbian Server Edition, which uses the 3.6.11+ kernel. We’ll need the matching source from github.
We’ll change to the /usr/src directory first, then wget the sources from github, and then untarball it with these commands.

*Note* I’m doing it as root, so you can either prepend sudo to these commands, or run sudo bash before running these commands.

cd /usr/src
tar -xvzf rpi-3.6.y.tar.gz

Once the source is extracted, we’ll need to copy the current kernel configuration into the source directory.

cd linux-rpi-3.6.y
gzip -dc /proc/config.gz > .config

And then we need to create the files and symlinks neccesary for compiling external modules

make modules_prepare
ln -s /usr/src/rpi-3.6.y /lib/modules/3.6.11+/build

Compiling the Vendor drivers

In order to use the Vendor drivers, first we’ll need to get them.
The drivers can be found here.
Download the ones for the RT5370 and transfer it to the Pi as we’ll need to compile the drivers ourselves.

Once we have the drivers on the Pi, we’ll move them to /usr/src and untar them to keep things nice and neat. Assuming the drivers are in /home/pi
*Note* Again I’m doing everything as root.

mv /home/pi/2011_0719_RT3070_RT3370_RT5370_RT5372_Linux_STA_V2.5.0.3_DP0.bz2 /usr/src
cd /usr/src
tar -xvjf 2011_0719_RT3070_RT3370_RT5370_RT5372_Linux_STA_V2.5.0.3_DP0.bz2
cd 2011_0719_RT3070_RT3370_RT5370_RT5372_Linux_STA_V2.5.0.3_DP0

Once it’s untarred, we’ll need to edit the file ‘os/linux/’ to enable WPA support and to allow network managers to control the device.
So find these lines, and change the n to y, then save and close the file.


Now, we should still be in the /usr/src/2011_0719_RT3070_RT3370_RT5370_RT5372_Linux_STA_V2.5.0.3_DP0/ directory.
Time to compile the drivers !
To compile them, we just need to run the ‘make’ command in the drivers directory as root.
It took me just shy of 12 minutes to compile the drivers on a Class 4 SD Card on a non-overclocked pi.
Once they are compiled, running ‘sudo make install’ will install the drivers into the right spot

To make sure the drivers are installed properly, we’ll load the module to make sure no errors come up.
*Note* Run this as root again

modprobe rt5370a

If no errors come up, then that’s a good sign. Check that it’s been loaded by running lsmod and checking the output.

root@raspberrypi:/home/pi# lsmod
Module Size Used by

rt5370sta 786186 0

You should see the module rt5370sta in the list there.
Lastly we’ll disable the rt2800 usb module and enable autoloading of the new rt5370sta module.
We’ll need to edit /etc/modprobe.d/raspi-blacklist.conf and add the following line to the end

blacklist rt2800usb

And then we’ll need to edit the /etc/modules file to add the new module we’ve just compiled. Just need to add the module name to the end of the file


After the file has been modified, reboot the Pi and when it comes back up, run ifconfig -a and you should see something similar to the following –

eth0 Link encap:Ethernet HWaddr b8:27:eb:xx:xx:xx
RX packets:354 errors:0 dropped:0 overruns:0 frame:0
TX packets:181 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:30609 (29.8 KiB) TX bytes:25948 (25.3 KiB)

lo Link encap:Local Loopback
inet addr: Mask:
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

ra0 Link encap:Ethernet HWaddr 00:0f:54:xx:xx:xx
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:854641 (834.6 KiB) TX bytes:44800 (43.7 KiB)

Notice the ra0 rather than wlan0. This shows that the Pi is using the new rt5370sta module rather than the old rt2800usb one. You can still use the ra0 interface to connect to wireless networks like normal.