daily pastebin goal
49%
SHARE
TWEET

Raspberry Pi Motion Script

a guest Jul 25th, 2014 3,964 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2.  
  3. import RPi.GPIO as GPIO
  4. import time
  5. import picamera
  6. import datetime
  7. import subprocess
  8. import dropbox
  9. import os
  10.  
  11. # Get your app key and secret from the Dropbox developer website
  12. app_key = 'your_app_key_here'
  13. app_secret = 'your_secret_here'
  14.  
  15. def getFileName():
  16.     return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
  17.  
  18. def dropboxAuth():
  19.     accessTokenFileOverwrite = open("accessToken.txt", "w+")
  20.    
  21.     flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
  22.     authorize_url = flow.start()
  23.    
  24.     # Have the user sign in and authorize this token
  25.     authorize_url = flow.start()
  26.     print '1. Go to: ' + authorize_url
  27.     print '2. Click "Allow" (you might have to log in first)'
  28.     print '3. Copy the authorization code.'
  29.     code = raw_input("Enter the authorization code here: ").strip()
  30.  
  31.     try:
  32.         # This will fail if the user enters an invalid authorization code
  33.         access_token, user_id = flow.finish(code)
  34.         accessTokenFileOverwrite.write(access_token)
  35.     except:
  36.         print "failed authorization, restart"
  37.         accessTokenFileOverwrite.close()
  38.         os.remove("accessToken.txt")
  39.  
  40.     accessTokenFileOverwrite.close()
  41.  
  42. def dropboxUpload(fileToUpload):
  43.     if not os.path.isfile("accessToken.txt"):
  44.         dropboxAuth()
  45.  
  46.     #get access token from file
  47.     accessTokenFileRead = open("accessToken.txt", "r")
  48.     access_token = accessTokenFileRead.read()
  49.     accessTokenFileRead.close()
  50.  
  51.     # make client
  52.     client = dropbox.client.DropboxClient(access_token)
  53.  
  54.     #upload file
  55.     fileToUploadObject = open(fileToUpload, "rb")
  56.     response = client.put_file(fileToUpload, fileToUploadObject)
  57.     fileToUploadObject.close()
  58.  
  59.  
  60. sensorPin = 7
  61.  
  62. GPIO.setmode(GPIO.BOARD)
  63. GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  64.  
  65. prevState = False
  66. currState = False
  67.  
  68. cam = picamera.PiCamera()
  69.  
  70. while True:
  71.     time.sleep(0.1)
  72.     prevState = currState
  73.     currState = GPIO.input(sensorPin)
  74.     if currState != prevState:
  75.         newState = "HIGH" if currState else "LOW"
  76.         print "GPIO pin %s is %s" % (sensorPin, newState)
  77.         if currState:
  78.             fileName = getFileName()
  79.             print "Starting Recording..."
  80.             cam.start_preview()
  81.             cam.start_recording(fileName)
  82.             print (fileName)
  83.         else:
  84.             cam.stop_preview()
  85.             cam.stop_recording()
  86.             print "Stopped Recording"
  87.             print "Sending Mail Notification..."
  88.             subprocess.call("mail -s 'Motion Detected' your@email.com < /home/pi/message.txt", shell=True)
  89.             print "Complete"
  90.             print "Uploading footage to Dropbox..."
  91.             dropboxUpload(fileName)
  92.             print "Complete"
RAW Paste Data
Top