Last active
October 12, 2015 21:08
-
-
Save jrmedd/4087649 to your computer and use it in GitHub Desktop.
Tweeting Letterbox. Blog post: http://jamesmedd.tumblr.com/post/35270913619/raspberry-pi-tweeting-letterbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tweeting Letterbox | |
# by James Medd | |
# November 2012 (original version) | |
# http://jamesmedd.co.uk/ | |
#!/usr/bin/python | |
import picamera | |
import RPi.GPIO as GPIO | |
import sys | |
import time | |
from twython import Twython | |
#OAuth details go here | |
CONSUMER_KEY = "" | |
CONSUMER_SECRET = "" | |
ACCESS_TOKEN = "" | |
ACCESS_TOKEN_SECRET = "" | |
#initialise Camera | |
camera = picamera.PiCamera() | |
#set input GPIO pin | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(11, GPIO.IN) | |
twit = Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET) | |
#timestamp, capture, and tweet an image | |
def capture_tweet(): | |
timestamp = time.strftime("%d%m%Y-%H%M%S") | |
camera.capture(timestamp + ".jpg") #save image to disk | |
image = open(timestamp + ".jpg", 'rb') | |
timestamp = "@Username " + timestamp | |
twit.update_status_with_media(status=timestamp, media=image) | |
print "Tweeted image at " + timestamp #make a record in the Python output | |
#try/except for tidy starting/stopping of script | |
try: | |
print "Tweeting Letterbox initialised. Exit using Ctrl + c." | |
#loop to check PIR state/trigger a tweet (exit using Ctrl+C) | |
while True: | |
ir_val = GPIO.input(11) | |
if not ir_val: | |
time.sleep(0.5) #wait before checking again if nothing detected | |
elif ir_val: | |
capture_tweet() #capture and tweet image if triggered | |
while ir_val: | |
ir_val = GPIO.input(11) | |
time.sleep(0.5) #wait for sensor to reset/movement to stop before checking again | |
else: | |
print "Error" | |
except KeyboardInterrupt: | |
print "\nExiting.", | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added an update to use the Raspberry Pi Camera module instead of a webcam. Also updated Twython-related code in line with their documentation guidelines etc.