Thursday 14 August 2014

Timelapse Pi camera rig

My Timelapse Pi rig

I wanted a quick way of setting up the unit, framing the shot and then starting the timelapse capture process, so I used a FTF touchscreen with tactile buttons.


Here are a couple of test movies I made while in Cornwall for my hols.

Sunset at Godrevy Lighthouse 



Carbis Bay for a day

Here's a brief description of how I've set it all up.

The tactile buttons perform the following tasks:

Button 1 (left) [pin 23]: Power off/On
Button 2  [pin 22]: Blank the screen
Button 3  [pin 27]: Start the timelapse shoot
Button 1  [pin 18]: Activate the camera

Putting this all together was fairly simple, following the excellent Adafruit instructions for getting the PiTFT working and building a Pi touchscreen camera.

I then used some simple Python to add functions to the buttons. These 3 scripts are all run from /etc/rc.local

Button 1

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
prev_input = 1
while True:
input_state = GPIO.input(18)
if (input_state == False) and (prev_input):
print 'button pressed ' + str(input_state)
os.system("python /home/pi/adafruit-pi-cam-master/cam.py")
prev_input = input_state
time.sleep(0.05)

Button 2

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
prev_input = 1
while True:
input_state = GPIO.input(22)
if (input_state == False) and (prev_input):
if os.path.isfile("/sys/class/gpio/gpio252/value"):
file = open("/sys/class/gpio/gpio252/value",'r')
gpio252 = file.read()
if gpio252.rstrip('\n') == '0':
os.system("/home/pi/Python/backlightOn.sh")
else:
os.system("/home/pi/Python/backlightOff.sh")
else:
os.system("/home/pi/Python/backlightOut.sh")
os.system("/home/pi/Python/backlightOff.sh")
prev_input = input_state
time.sleep(0.05)

Button 3

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
prev_input = 1
while True:
input_state = GPIO.input(27)
if (input_state == False) and (prev_input):
print 'button pressed ' + str(input_state)
os.system("/home/pi/Python/LapseLaunch.py")
prev_input = input_state
time.sleep(0.05)
This calls another Python script that checks to see if a photo shoot is already in progress and, if not, starts the Timelapse bit.

LapseLaunch.py

#!/usr/bin/python
import commands
import time
from subprocess import call
pros = commands.getoutput('ps -A')
if 'timelapse' in pros:
print 'not running another'
else:
call("/home/pi/Timelapse/timelapse.py")
 And finally, the actual Timelapse Python:

timelapse.py

#!/usr/bin/python
import serial, time, sys
import picamera
from datetime import datetime, timedelta
with picamera.PiCamera() as camera:
    camera.resolution = (1280,720)
    camera.start_preview()
    time.sleep(2)
    #for filename in camera.capture_continuous('img{counter:03d}.jpg'):
    for filename in camera.capture_continuous('/home/pi/Timelapse/img{timestamp:
%Y-%m-%d-%H-%M}.jpg'):
        print('Captured %s' % filename)
        time.sleep(28) 
For outdoors power I use an RS Power Bank which gives around 2.5 hours of operation on a full charge.

If I need to reconfigure anything (for example, alter the frequency with which photos are taken) I connect my Android mobile via USB tethering. I've found that using ConnectBot as the ssh client (with the invaluable Hackers keyboard also installed) provides a great way to gain access without having to lug a keyboard around. 

Finally, I also have a tin with an appropriate hole for the lens,  in which I can put the whole kit and caboodle if it looks like its going to rain!