Mittwoch, 21. Januar 2015

#Raspberry Pi Timelapse project


Someone gave me a "Magic Garden" as a present this Christmas! So I headed to my local dealer to get me a Pi Cam to play around with it as I now really really had a valuable yet scientific job to do!

Here's the result:





And this is what I've done:

I cut a little slot into the pi case for the flat cable. Then I found some parts of a lamp and a lighter and a film canister - screwed and glued it together to have a solid and flexible stand for it. While writing these lines I am thinking abount some buttons to start my scripts and shutdown the device ...


Next thing was to follow this tutorial to get that thing working: python-picamera-setup
As the pi runs headless through its life, I wanted it to either copy the pictures to a dropbox folder or to a usb stick plugged into the pi. 
For the dropbox part I followed raspi.tv here: how-to-use-dropbox-with-raspberry-pi
Some googling helped me to find out how to mount a usb stick to my pi how-to-mount-a-usb-flash-disk-on-the-raspberry-pi Most important was to change the fstab: adding the sticks uuid and so on:


Using the above mentioned tutorials I came out with two little python scripts:
(note to myself: needs some errorhandling if the dropbox upload fails)


-----------------tinkerCam.py-----------------
#! /usr/bin/env python
# taking a picture and uploading it to dropbox
# uses Andrea Fabrizis Dropbox Upload www.andreafabrizi.it

from subprocess import call
import time
import datetime
import picamera


pictureDir = '/home/pi/tinkerCam/pictures/'
ts = time.time()
fileNameShort = 'tinkerPic_' + datetime.datetime.fromtimestamp(ts).strftime('%d-%m-%y_%H-%M-%S') + '.jpg'

uploader = '/home/pi/Dropbox-Uploader/dropbox_uploader.sh'
upconfig = '/home/pi/Dropbox-Uploader/.dropbox_uploader'

camera = picamera.PiCamera()
fileName = pictureDir + fileNameShort

camera.capture(fileName)

cmd = uploader + ' -f ' + upconfig + ' upload ' + fileName + ' /tinkerCam/' + fileNameShort
call([cmd], shell = True)

#copy file to usbStick
stickDir = '/media/stick/tinkerCam/'
cmd = 'mv ' + fileName + ' ' + stickDir+fileNameShort
print cmd
#call([cmd], shell = True)

#clean up
cmd='rm ' + fileName
call([cmd], shell = True)
print 'finished' 



-----------------tinkerTimelapse.py-----------------
#! /usr/bin/env python
from subprocess import call
import time
import datetime
import picamera

uploader = '/home/pi/Dropbox-Uploader/dropbox_uploader.sh'
upconfig = '/home/pi/Dropbox-Uploader/.dropbox_uploader'
usbPath = '/media/stick/tinkerTimelapse/'
dropboxPath = '/tinkerCamTimelapse/'
tempPath = '/home/pi/tinkerCam/'

def toDropbox(fileNameShort):
cmd = uploader + ' -f ' + upconfig + ' upload ' + tempPath + fileNameShort + ' ' + dropboxPath + fileNameShort
call([cmd], shell = True)
#clean up
cmd = 'rm ' + tempPath + fileNameShort
call([cmd], shell = True)

def capturePic(frame, fileNameShort):
with picamera.PiCamera() as cam:
time.sleep(2)
picPath = tempPath + fileNameShort
print 'capturePic:', picPath
cam.capture(picPath)

def constructFileName():
ts = time.time()
fileNameShort = 'tinkerPic_' + datetime.datetime.fromtimestamp(ts).strftime('%d-%m-%y_%H-%M-%S') + '.jpg'
return fileNameShort

#user interface
picDays = raw_input('How many days will be captured?: ')
framesPerHour = raw_input('How many pictures per hour?: ')
frames = int(framesPerHour) * 24 * int(picDays)
saveTo = raw_input('Save to? [dropbox, usb]: ')

#capture images
for frame in range(frames):
start = time.time()
fileNameShort = constructFileName()
fileName = tempPath + fileNameShort
capturePic(frame, fileNameShort)
if saveTo == 'dropbox' or saveTo == 'd':
toDropbox(fileNameShort)
if saveTo == 'usb' or saveTo == 'u':
cmd = 'mv ' + tempPath + fileNameShort + ' ' + usbPath + fileNameShort
call([cmd], shell = True)
print 'Picture taken: ', frame
time.sleep(int(60 * 60 / int(framesPerHour)) - (time.time() - start))

print 'finished' 

WTF - "solving three cubes while juggling them"



I found this video here at bo(lots of os)m ....