forked from seanbechhofer/raspberrypi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpirCamera.py
More file actions
executable file
·84 lines (75 loc) · 2.7 KB
/
pirCamera.py
File metadata and controls
executable file
·84 lines (75 loc) · 2.7 KB
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
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
"""
Code that uses external trigger (e.g. PIR) to take photos and
upload to twitter
"""
import RPi.GPIO as GPIO
import argparse, os
import time, datetime
from twython import Twython, TwythonError
import ConfigParser
CYCLE=2
PIN = 7
WIDTH=1280
HEIGHT=960
GPIO.setwarnings(False) # Turn warnings off
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIN, GPIO.IN)
config = ConfigParser.ConfigParser()
config.read('/home/pi/conf/config.cfg')
APP_KEY = config.get('KEYS','twitter.consumer.key')
APP_SECRET = config.get('KEYS','twitter.consumer.secret')
OAUTH_TOKEN = config.get('KEYS','twitter.access.token')
OAUTH_TOKEN_SECRET = config.get('KEYS','twitter.access.token.secret')
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
def takePhoto():
"""Take a photo with the name based on the timestamp"""
now=datetime.datetime.now()
photo_name=now.strftime('%y%m%d-%H%M%S.jpg')
os.system("raspistill -t 0" +
" -w " + str(WIDTH) +
" -h " + str(HEIGHT) +
" -o " + photo_name )
return dict(time=now,name=photo_name)
def tweetPhoto(photo_info):
"""Tweet a photo"""
photo = open(photo_info['name'], 'rb')
message = 'Photo: ' + photo_info['time'].strftime('%H:%M:%S on %d/%m/%y')
print message
twitter.update_status_with_media(status=message,
media=photo)
# Delete photo_name
def main():
parser = argparse.ArgumentParser(description='PIR Triggered camera')
parser.add_argument('-p','--photo', action='store_true', default=False,
dest='photo',
help='Take Picture')
parser.add_argument('-t','--tweet', action='store_true', default=False,
dest='tweet',
help='Tweet Picture')
parser.add_argument('-c','--cycle',
type=int,
default=5,
help='Cycle time')
parser.add_argument('-d', '--delay',
type=int,
default=10,
help='Delay after triggering')
parser.add_argument('-v','--verbose', action='store_true', help='verbose')
args = parser.parse_args()
while True:
if args.verbose:
print GPIO.input(PIN)
if GPIO.input(PIN):
if args.photo:
photo_info = takePhoto()
if args.verbose:
print photo_info
if args.tweet:
tweetPhoto(photo_info)
if args.verbose:
print 'Tweeted!'
time.sleep(args.delay)
time.sleep(args.cycle)
if __name__ == '__main__':
main()