Skip to content

Instantly share code, notes, and snippets.

@breim
Created August 11, 2015 01:10
Show Gist options
  • Save breim/d83af7f3d06b0e7e3cd2 to your computer and use it in GitHub Desktop.
Save breim/d83af7f3d06b0e7e3cd2 to your computer and use it in GitHub Desktop.
Take a photo and Tweet when moviment is detected via raspberry
#!/usr/bin/env python
# Import required Python libraries
import time
import RPi.GPIO as GPIO
import sys
from twython import Twython
import pygame
import pygame.camera
import os
from pygame.locals import *
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''
api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# Define GPIO to use on Pi
GPIO_PIR = 7
print "PIR Module Holding Time Test (CTRL-C to exit)"
# Set pin as input
GPIO.setup(GPIO_PIR,GPIO.IN) # Echo
Current_State = 0
Previous_State = 0
try:
print "Waiting for PIR to settle ..."
# Loop until PIR output is 0
while GPIO.input(GPIO_PIR)==1:
Current_State = 0
print " Ready"
# Loop until users quits with CTRL-C
while True :
# Read PIR state
Current_State = GPIO.input(GPIO_PIR)
if Current_State==1 and Previous_State==0:
# PIR is triggered
start_time=time.time()
print " Motion detected!"
os.system("fswebcam -r 640x480 -S 15 --jpeg 95 --shadow --title 'Hbreim' --subtitle 'Batcaverna' --info 'RaspBerryCam' --save home.jpg")
photo = open('home.jpg','rb')
api.update_status_with_media(media=photo, status='My RPi be tweeting images now => ')
os.system("rm -rf home.jpg")
# Record previous state
Previous_State=1
elif Current_State==0 and Previous_State==1:
# PIR has returned to ready state
stop_time=time.time()
print " Ready ",
elapsed_time=int(stop_time-start_time)
print " (Elapsed time : " + str(elapsed_time) + " secs)"
Previous_State=0
except KeyboardInterrupt:
print " Quit"
# Reset GPIO settings
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment