Introduction: Time Lapse Using Raspberry Pi

About: League of Legends: Pshock13 DeviantART: Narchy13

I recently went through the entire process of installing a rasbian image and setting up my pi cam to take a time lapse of pictures. The way this is setup will also continue to take photos after a power outage.

What we'll be doing...

a. install Rasbian

b. setup a samba share to save files to a NAS drive (my PC in my case)

c. make our pi cam take time lapse photos.

Prerequisites

1. Raspberry Pi (I use a B+ in my build)

2. Pi cam

3. Ethernet, keyboard/mouse, monitor (For initial setup)

4. Fake CCTV camera (Optional)

Downloads

Rasbian image

SD Formatter v4.0 (install)

Win32 Disk Imager (install)

PuTTY (install)

Once you've downloaded and installed all the necessary programs, let's get our pi up and running

Step 1: Ready the SD Card for Rasbian

So here we'll use SD formatter v4.0 to format the sd card you plan on using.

With the sd card of your choice plugged into your computer, go ahead and start up the formatter program.

From here you can select the drive from the drop down list. (be absolutely sure of the drive as we're about to erase everything on it.) Hitting the OPTION button will bring up a few extra options that you can set...for the most part a quick format will be just fine...if for some reason you are unable to install the rasbian image on the card later, you may want to try an overwrite erase (takes a little longer)

Go ahead and hit FORMAT once you've gotten all you settings down. (depending on the size of your SD card, this part could take a while)

So, while we wait...let's go ahead and unzip that rasbian image you downloaded earlier. (make sure you know where you unzip it to, obviously we're gonna need it here soon)

Once your card is formatted and you've unzipped your image...we're gonna write that image to the card, using none other than Win32 disk Imager!

Open up the imager program and hit the little blue folder there. This will open up an explorer for you to navigate to that .img of rasbian (you remember where you put it right?) Once again, make sure you have the correct drive selected and hit WRITE. Again, the size of the drive will effect the time it takes for this to finish.

Step 2: First Boot of Rasbian

We're ready to boot into rasbian for the first time. Exciting!

Eject the sd card from your computer and plug it into your raspberry pi. Make sure you're plugged into a monitor of some sort (I used my TV) you'll also want to have a keyboard and mouse as well as a hard wire ethernet (you may not need the ethernet wire on the pi3 as it has built in wifi though I'm unsure). Speaking of which...If you have to use a wifi dongle for your pi, DO NOT I repeat DO NOTplug the dongle in just yet. (We'll get to it, I promise).

Once you've made it to the raspberry pi desktop, we're gonna open up a terminal session so that we can get that wifi working. Now type in sudo raspi-config to bring up the configuration tool. You're gonna want to enable SSH so we can ditch the keyboard/mouse and monitor. Select option 9.Advanced Options using the arrow keys and [enter] and then select option A4 SSH and enable. This will allow us to connect from our PC. (sweet). While we're back at the main selection of the config tool, go ahead and select option 6. Enable Camera. And exit the tool.

Now in the terminal we're going to edit a few files that will allow the wifi dongle to work. So first we'll type

sudo nano /etc/network/interfaces

sudo - grants us temporary super user privileges to execute the rest of the command

nano - is a small text editing program

/etc/net..ces - this is the path to the file we're editing.

There should already be a few lines of code in this file. Edit yours to match this.

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf<br>iface default inet dhcp

As you can see in my snapshot, I also had allow-hotplug wlan1. While I'm not sure if it makes any difference, I also copied the rest of the code under that one as well (everything seems to be working fine for me.)

Go ahead and hit [ctrl] + [x] and nano will ask if you want to save...simply hit [y] and then [enter]. And we're done with that file.

Now, we're going to edit that wpa_supplicant.conf file that we put in that interfaces file. So go ahead and type

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

This file should have a few lines in it as well. Here's where we add the information that is going to tell the dongle how to connect to the router. Copy the following text into your wpa_supplicant.conf file after the last existing lines. Be sure to replace your ssid and password as well as the other settings the apply to your router


network={
ssid="YOURSSID"
psk="YOURPASSWORD"
# Protocol type can be: RSN (for WP2) and WPA (for WPA1)
proto=WPA
# Key management type can be: WPA-PSK or WPA-EAP (Pre-Shared or Enterprise)
key_mgmt=WPA-PSK
# Pairwise can be CCMP or TKIP (for WPA2 or WPA1)
pairwise=TKIP
#Authorization option should be OPEN for both WPA1/WPA2 (in less commonly used are SHARED and LEAP)
auth_alg=OPEN
}

Save your file with [ctrl] + [x], [y], [enter]

Before shutting down and moving on, type

sudo ifconfig

and find the ip address of your pi. We'll be using this in the next step.

Now type

sudo shutdown now

You're Ready for the next step.

Step 3: SSH From Here on Out

You can now unplug the keyboard/mouse, monitor, and ethernet from your pi. And plug in your wifi dongle and power on your pi.

'But you told me to unplug everything, how can I do anything else?' -some of you.

Well this is where PuTTY is gonna come in handy, you should already have it installed like I told you to earlier. With PuTTY open you'll want to input the ip address of your raspberry pi that we got in the last step. Leave the port as 22, as that is the default port used by SSH. (you can change this for added security, but we don't need it for our purpose now). I would go ahead and add a name for the connection in the box under 'saved sessions' and hit SAVE. This way you can load your connection settings quickly anytime you wanna connect to your pi.

When you open the connection to your pi it'll ask you to login as: ...this would be pi. After hitting enter, it will prompt you for a password...granted you haven't taken it upon yourself to go and change it, the default password is raspberry.

WooHoo! You just made it into your pi through SSH. Now we can control the pi from the comfort of our desktop.

Step 4: Setting Up Samba

You'll only need this step if you plan on having your photos saved directly to your PC rather than your Raspberry Pi (I recommend it considering the limited space the Pi provides)

Now I'm gonna get a little lazy here cause I'm gonna send you over to the Urban Penguin for setting up the Samba server on the Pi. He's really good at explaining what each part of a command does as he goes along which is nice for those just learning (like myself) and it also allows you to tweak some things to your liking. So go set up your samba server with the Urban Penguin and when you're good and ready, come back so we can finish up. Don't worry, I'll wait till you're done.

------------

So, how was that?? Not so bad, right? You got a samba server running? Well lets move on.

Now, we'll need to make a share folder on PC for our photos to be saved to. Mine will be named picam. Open up the folder properties and navigate to the sharing tab and hit Advanced sharing. In the new dialog that pops up, we want to check the box to share this folder and then hit the permissions button. In the permissions dialog you want to hit add and then type 'everyone' (literally, everyone, no quotes) and hit OK. Back in the permissions dialog, select full control for everyone. BOOM! Now your folder is shared and anyone on the same network can add/delete/modify anything in that folder (not exactly the safest thing...but again, for our purpose here, it'll do). Go ahead and exit out of all those windows by hitting the OKs.

Mounting the share folder

This is the part were we mount (or connect) the two folders we made, the one from setting up samba and the share folder we have on our PC.

Two options for mounting

Guest Share

sudo mount -t cifs -o guest //WindowsPC/share1 /mnt/mountfoldername

Tip: If your share has space then run the following command, notice the quotes

sudo mount -t cifs -o guest "//WindowsPC/Share 1" /mnt/mountfoldername

Password Protected Share

sudo mount -t cifs -o username=yourusername,password=yourpassword //WindowsPC/share1 /mnt/mountfoldername

Note: You'll notice later on that I use my PCs ip address in place of it's name. I find it to be more reliable and less hassle since my PC name has a hyphen in it. If you don't know your ip address, open up a command prompt and type ipconfig. Your ip address will be listed as the IPv4 address.

After mounting, use df -h to test if the mount was successful. It should return something similar to this:

Filesystem Size Used Avail Use% Mounted on
rootfs 7.3G 2.0G 5.0G 29% 
dev/root 7.3G 2.0G 5.0G 29% /
tmpfs 19M 228K 19M 2% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 37M 0 37M 0% /tmp
tmpfs 10M 0 10M 0% /dev
tmpfs 37M 0 37M 0% /run/shm
/dev/mmcblk0p1 56M 36M 21M 64% /boot
//WindowsPC/share1 1.9T 1.5T 390G 80% /mnt/mountfoldername

Note the last line. Now if we reboot, or lose power...we also lose the mount and would have to manually mount the folder again. Let's fix that.

We need to edit /etc/fstab. So run the command

sudo nano /etc/fstab

And we'll edit the file accordingly. Add your login information after the last line in the file

For Guest Login

//WindowsPC/Share1 /mnt/mountfoldername cifs guest 0 0

For Password Protected Login

//WindowsPC/Share1 /mnt/mountfoldername cifs username=yourusername,password=yourpassword 0 0

You'll notice in my fstab file I also added domain, iocharset, file_mode, and dir_mode as I was having some trouble writing to the mounted folder despite having set everything to guest mode. Adding those things fixed that problem for me.

[ctrl] + [x], [s], [enter] to save and exit. sudo reboot -f and once you log back in type df -h and see if your mount stayed through the reboot.

Step 5: Testing Raspistill and Mounted Folders

Before we move on any further, we're going to test to see that a few things are working. From the rasperry pi's terminal that you have open in PuTTY, type the follow using your share names.

raspistill -o /mnt/sambasharename/picturename.jpg -n -t 1000

Okay, let's start with 'what the hell did you just have me type in?!'

raspistill -this is used to take still photos with the raspberry camera (there is also a raspivid command, I bet you can figure that one out)

-o /mnt... -the '-o' flag is our 'output' followed by a space and the destination/name of the still photo.

-n -this notes 'no preview' and is somewhat pointless without a direct monitor as we won't see a preview any way

-t 1000 - the '-t' flag is time or rather how long you want the camera to wait before snapping the photo. (useful for selfies!) and the 1000 is the amount of time in milliseconds...so, 1 second.

You could also run a timelapse using the raspistill command with something like this

raspistill -o/mnt/picam/%04d.jpg -t 30000 -tl 1000

This will run the camera for about 30 seconds taking a picture every 1 second (in theory). All the while saving the photos in /mnt/picam as 0001.jpg, 0002.jpg, 0003.jpg...and so on.

As you can see in the snap shot, I ran two variations of a time lapse. In the first one, I ran it to take a picture every second. But as you can see under that, it skipped frames...so i actually ended up with files name 001, 004, 007, and 010. This is because the pi isn't quite powerful enough to take and save a picture within a second. And so comes the second command...note the only difference is the number after '-tl'. Putting a 0 basically tells it "just take another picture whenever you are ready buddy, no pressure" ...and thus I ended up with files that were name in sequence without any skipped.

So, what if we want to run a time lapse over the course of a week?? I certainly don expect you to use -t 60480000. This would mean you can't let the pi lose power, and if it does, you have to run the code again to start it back taking pictures. WELL!!! Have I got the solution for you. Follow me to the next step.

Step 6: Writing Scripts

We're gonna go ahead and write a little script and have the raspberry pi execute it whenever it turns on.

Writing the script

I'm going to place my script right in side of /mnt/picam just to keep everything in one place. (You can save yours where ever you'd like, just take note of where you have it). So, I'm going to

cd /mnt/picam 

in order to be in the folder. And then

sudo nano scriptname.sh

In your nano editor (which should be completely blank) we're going to add our script

#!/bin/bash
#This line is commented out by the pound symbol at the beginning of the line.
#this next line sets a variable for us to use later
DATE=$(date +"%Y-%m-%d_%H%M")
#this is the actual command used for taking a picture
raspistill -o /mnt/picam/$DATE.jpg -t 1000

Save and exit.

In the script, we've made it so pictures will be saved based on WHEN they were taken. This is a much better method than using %04d, especially if you plan on doing a very long span of time for your time lapse.

Make the script executable by using

sudo chmod 755 scriptname.sh

You can test that the script works by typing

/mnt/picam/scriptname.sh

Quick, go to your share folder on your PC...is there a photo in there with a name similar to '2016-05-05_0620.jpg' ? I hope so, that means it worked! Moving along, we're almost finished.

Step 7: Adding Your Script to Cron

What in the world is cron?!

Well, once again someone else has this better explained already so I'm going to point you in the direction of cogNiTioN's Intro to cron. You only need to read until he start's talking about multi user cron and then you can come back here.

-----------

Now that you have an under standing of cron you should be able to setup your cron tab the way you like. In my snap shot you'll see that I actually have my script in there twice, one set to run every 15 mins. and another to run once every minute. You should also take note that one of those scripts is commented out using #. Therefor it isn't currently set to run at all. This is good to know if you ever want to stop the script from running.

Maybe one pic every minute isn't enough for you...well I find that cron is limited in that fact, but if your script was

raspistill -o /mnt/picam/$DATE.jpg -tl 0 -t 60000

effectively running a 60 second timelapse...with crontab starting it once every minute....yeah you get it.

EDIT 07/02/17: Your output file name should be something along the lines of /mnt/picam/$DATE%03d.jpg. Without adding the "%03d" the raspistill command will just overwrite using the same name (time). I've also found that adding "%S" into the DATE=$() at the top of your script does not help. Again it only overwrites all the photos, leaving you with one in the end

Keep in mind, the fast you have you pi taking pictures, the sooner it will fill the space on whatever drive your share is mounted to. Just keep an eye on your storage. Anyway...that's pretty much it as far as the software side goes.

Step 8: Final Steps

For my setup I decided to get a dummy camera to house the entire thing. This works really nicely as it was also able to hold up through a pretty tough storm the first night I had it all set.

I had to remove the LED, wires, and battery housing to make room enough for the pi. I also grabbed a piece of scrap plastic and put a small hole, just big enough to fit the camera's lens through. I screwed the camera to the plastic and then glued the plastic inside the case of the fake security cam. As far as the pi goes, it is only in there with some packing styrofoam to keep it from being knocked around. I also decided to super glue the power cord directly to the pi. Not sure if it is possible for the glue to short the pi and I don't recommend this method. However, it works for me so that the power doesn't get pulled.

That's all I have for now...let's see what you were able to time lapse with this.