-
Notifications
You must be signed in to change notification settings - Fork 0
/
owl_time_lapse.py
executable file
·75 lines (62 loc) · 1.8 KB
/
owl_time_lapse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import pickle
import flickr_api
import picamera
from datetime import datetime as d
import os
import sys
import logging
GPIO.setmode(GPIO.BCM)
PIR_PIN = 4
GPIO.setup(PIR_PIN, GPIO.IN)
motion_detected = False
def motion_cbk(PIR_PIN):
global motion_detected
motion_detected = True
logging.basicConfig(filename='time_lapse.log', level=logging.DEBUG)
c = pickle.load(open('credentials.p', 'rb'))
API_KEY = c.get('API_KEY')
API_SECRET = c.get('API_SECRET')
USER_ID = c.get('USER_ID')
PHOTO_SET_ID = c.get('PHOTO_SET_ID')
DEFAULT_INTERVAL = 3 #in minutes
flickr_api.set_keys(api_key = API_KEY, api_secret = API_SECRET)
a = flickr_api.auth.AuthHandler.load('/home/pi/timelapse/owl_auth_token')
flickr_api.set_auth_handler(a)
user = flickr_api.test.login()
photosets = user.getPhotosets()
for p in photosets:
if p.id == PHOTO_SET_ID:
break
if len(sys.argv) == 2:
interval = int(sys.argv[1])
else:
interval = DEFAULT_INTERVAL
camera = picamera.PiCamera()
camera.resolution = (2592, 1944)
img_idx = 0
try:
GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=motion_cbk)
while True:
n = d.now()
if motion_detected:
try:
camera.led = True
fname = n.strftime('%Y%m%d%H%M') + '.jpg'
camera.capture(fname)
photo = flickr_api.upload(photo_file=fname)
p.addPhoto(photo_id = photo.id)
os.remove(fname)
camera.led = False
motion_detected = False
except:
print("Encountered an exception")
e = sys.exc_info()[0]
logging.error(e)
camera.led = False
time.sleep(interval*60)
except KeyboardInterrupt:
print("Quitting...")
GPIO.cleanup()