Skip to content

Commit fb42565

Browse files
authored
tweetcam.py
Take a photo on GPIO input and post to Twitter
1 parent 1d21f9c commit fb42565

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

tweetcam.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python2.7
2+
# TweetCam - Take a photo on GPIO input and post to Twitter
3+
# Based on tweetpic.py by Alex Eames http://raspi.tv/?p=5918
4+
# Modified by Nick Poole http://sparkfun.com
5+
import RPi.GPIO as GPIO
6+
import tweepy
7+
from subprocess import call
8+
from datetime import datetime
9+
10+
GPIO.setmode(GPIO.BCM) #we want to reference the GPIO by chip number
11+
12+
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) #our shutter switch is on pin 18
13+
GPIO.setup(4, GPIO.OUT, initial=GPIO.HIGH) #our status LED is on pin 4, turn it on
14+
15+
while 1: #loop forever
16+
17+
while GPIO.input(18): #wait for the shutter button. do nothing.
18+
pass
19+
20+
i = datetime.now() #take time and date for filename
21+
now = i.strftime('%Y%m%d-%H%M%S')
22+
photo_name = now + '.jpg'
23+
cmd = 'raspistill -t 500 -w 1024 -h 768 -o /home/pi/' + photo_name
24+
call ([cmd], shell=True) #shoot the photo
25+
26+
# Consumer keys and access tokens, used for OAuth
27+
consumer_key = 'copy your consumer key here'
28+
consumer_secret = 'copy your consumer secret here'
29+
access_token = 'copy your access token here'
30+
access_token_secret = 'copy your access token secret here'
31+
32+
GPIO.output(4, 0) #turn off status LED to signal the user to wait
33+
34+
# OAuth process, using the keys and tokens
35+
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
36+
auth.set_access_token(access_token, access_token_secret)
37+
38+
# Creation of the actual interface, using authentication
39+
api = tweepy.API(auth)
40+
41+
# Send the tweet with photo
42+
photo_path = '/home/pi/' + photo_name
43+
status = 'Photo auto-tweet from Pi: ' + i.strftime('%Y/%m/%d %H:%M:%S')
44+
api.update_with_media(photo_path, status=status)
45+
46+
GPIO.output(4, 1) #turn on status LED for OK
47+

0 commit comments

Comments
 (0)