Daily Archives : June 29th, 2016

Time-lapse Video with a Webcam

This project will show you how to use a standard webcam to create a time-lapse video recorder – great for watching the whole day go by in a few minutes.

For this we’ll need a Raspberry Pi, a USB webcam (and a portable power source if you want to position it somewhere outside)timelapse_stuff
I’ll be using a Pi B+ running the latest version of Raspbian Jesse, a USB Wi-Fi dongle, a Microsoft Lifecam HD and an Anker portable battery pack.

First up you’ll want to get powered up and logged in, with the USB webcam connected. To check the webcam is recognised just type lsusb

lsusb

You can see that my webcam is Device 005 on Bus 001 and is from Microsoft Corp.  So great we’re in business, now we need some software to control the webcam. For that we’ll be using fswebcam – this is a great package with tons of features, but we’ll only be using the basics. To install it just type sudo apt-get install fswebcam

fswebcam

You can try it out by typing fswebcam –r 1280×720 –no-banner image.jpg (the –r 1280×720 tells it what resolution to use for the capture, the -–no-banner tells it not to display the time banner and image.jpg is the name of the file to save the image in.

image1

You can view this image now on the Pi if you have a monitor attached or by grabbing a copy of the file (FTP, SFTP etc) onto another machine.
Now we have this working we’ll also need some software to convert all the image captures we take into a movie, for this we’ll use avconv which is part of the libav-tools package, so let’s install that by typing sudo apt-get install libav-tools

libav-tools

That’s all the software we need, it is just a matter of orchestrating things. We’ll need to:

  1. Capture a webcam snapshot to a file (with a sequential number as the filename)
  2. Sleep for X seconds
  3. If we don’t have enough snapshots yet go back to step 1
  4. Merge all the snapshots together into a time-lapse movie file
  5. Delete all the snapshot files

To write the script that does all this type nano timelapse.sh and enter the following:

#!/bin/bash
DIR=/home/pi/timelapse
COUNT=0
MAXCOUNT=30
INTERVAL=10
DATE=$(date +”%Y-%m-%d_%H%M”)

while [ $COUNT -le $MAXCOUNT ]; do

FILENAME=$(printf “%05d.jpg” “$COUNT”)
let COUNT=COUNT+10

# take the snapshot
fswebcam -r 1280×720 –no-banner $DIR/$FILENAME

# pause for a bit
sleep $INTERVAL;

done;

# now make the movie
avconv -r 5 -I $DIR/%05d.jpg -vcodec libx264 -r 30 timelapse_$DATE.mp4

# and tidy up the files
rm -f $DIR/*.jpg

Final bits required are to create the timelapse folder – type mkdir timelapse and make the script we wrote executable – type chmod +x timelapse.sh

We now have a script that will take a snapshot every 10 seconds (INTERVAL) until we have 30 of them (MAXCOUNT) and then combine them all into a movie for us. Be warned that this will take some time (30 time 10 seconds to get all the shots and then around 10 minutes of processing to combine them) but the result will be a cool time-lapse movie.
Feel free to adjust the INTERVAL and MAXCOUNT to record for longer period and with different periods between shots.

Getting Started with a Pi Zero

Managed to get hold of a Pi Zero ? Here’s how to get started with it…

The Pi Zero only has a single micro USB port for connecting peripherals. That can make it a little tricking when trying to get things set up. There’s a few bits that you will need to go along with your Pi Zero in order to set it up and get it going :

First up you’ll want to load the latest version of Raspbian on to your micro SD card (instructions here), then you’ll want to insert the micro SD card into your Pi Zero, making sure it’s pushed all the way in.

IMG_20160629_132625IMG_20160629_132649

Next, connect up the micro USB OTG (On The Go) connector to the wireless keyboard/mouse dongle and insert that into the middle of the three connectors.

IMG_20160629_132807IMG_20160629_132820IMG_20160629_132849

Lastly, for this bit, connect up the mini HDMI cable to the monitor and then connect the micro USB power cable.

IMG_20160629_132931IMG_20160629_132954
The Pi Zero should now boot and you’ll see the text boot sequence log flashing by on the monitor. Depending on which version of Raspbian you are using you will either get a shell (text based console) or the screen will blank and then start up the desktop. If it boots to the desktop, then open xTerminal, if it boot to the shell then your good to go.

What we need to do now is to configure the Wi-Fi settings. To do this enter the following :

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

This will open the existing file which will likely only have 2 lines in it. Underneath those lines you will add your Wi-Fi details. It looks like this:

network={
ssid=”MYSSID”
psk=”mypassword”
id_str=”Home”
}

Replace MYSSID and mypassword with the actual values for your Wi-Fi network.

You can multiple of these ‘network’ sections if you have multiple Wi-Fi access points that your Pi Zero will access. Just make sure that each of them has a unique id_str.
When you have added your details hit CNTL+X, then Y and then ENTER to quit and save the file. Now you can shutdown the Pi Zero and we’ll do a little ‘plug and play’

When it has shutdown pull out the wireless keyboard/mouse dongle (it may leave the OTG adapter in there, no problem). Now plug in the USB Wi-Fi adapter to the OTG and make sure it’s plugged into the Pi Zero. Reapply power (unplug and plug in again) and it should boot up as usual, but will now automatically connect to Wi-Fi.

IMG_20160629_133031

You can now access it from a remote machine via SSH. Open your SSH client and connect to raspberrypi.local (note: you need to have iTunes or other Apple software installed on the remote machine for it find the Pi using .local) Assuming all that has worked you’ll have a SSH connection to your Pi Zero and can tinker as needed. You can also, at this stage, remove the mini HDMI and run ‘headless’.

Great, we’re done, one Pi Zero set up with Wi-Fi, running headless, ready for some project.