OhioJoe

rpi-ms-camera.py

Feb 21st, 2017
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. # Raspberry Pi motion sensitive camera
  2. # this program is started when the wifi interface is up and running
  3.  
  4. # import required libraries
  5. import os, picamera, sys, time, smtplib
  6. import RPi.GPIO as GPIO
  7. import subprocess
  8.  
  9. from email.MIMEMultipart import MIMEMultipart
  10. from email.MIMEBase import MIMEBase
  11. from email.MIMEText import MIMEText
  12. from email import Encoders
  13. gmail_user = "[email protected]" #Sender email address
  14. gmail_pwd = "raspberryPi" #Sender email password
  15. to = "[email protected]" #Receiver email address
  16. subject = "Security Breach"
  17. text = "There is some activity in your home. See the attached picture."
  18.  
  19.  
  20. # GPIO pin for the PIR sensor
  21. PIR_GPIO_PIN = 4
  22.  
  23. # camera mode
  24. PHOTO = "Photos"
  25. VIDEO = "Videos"
  26.  
  27. # location on raspberry pi to store photos and videos
  28. #LOCAL_DIRECTORY = "/home/pi/python_programs/camera_output/"
  29. # Testing RAM disk
  30. LOCAL_DIRECTORY = "/mnt/rd/"
  31.  
  32. # location of the program used to upload files to dropbox
  33. DROPBOX_COMMAND = "/home/pi/Dropbox-Uploader/dropbox_uploader.sh"
  34.  
  35. # video recording time in seconds
  36. RECORDING_TIME = 15
  37.  
  38. # time to wait after taking photo or video in seconds
  39. WAIT_TIME = 1
  40.  
  41. # generate a file name based on the local date and time
  42. # year-month-day-hour-minutes-seconds-timezone
  43. def generate_file_name():
  44. return time.strftime("%Y-%m-%d-%H-%M-%S-%Z", time.localtime()) # e.g., 2014-09-14-15-23-45-PST
  45.  
  46. # when motion is detected, take a photo or record a video
  47. # based on what mode was specified by the user
  48. def motion_detected(pir_sensor):
  49. fname = LOCAL_DIRECTORY + generate_file_name()
  50. print "rpi-ms-camera: Motion detected!"
  51. if camera_mode == PHOTO:
  52. fname = fname + ".jpg"
  53. snap_photo(fname)
  54. msg = MIMEMultipart()
  55.  
  56. msg['From'] = gmail_user
  57. msg['To'] = to
  58. msg['Subject'] = subject
  59. msg.attach(MIMEText(text))
  60. mailServer = smtplib.SMTP("smtp.gmail.com", 587)
  61. mailServer.ehlo()
  62. mailServer.starttls()
  63. mailServer.ehlo()
  64. mailServer.login(gmail_user, gmail_pwd)
  65. mailServer.sendmail(gmail_user, to, msg.as_string())
  66. # Should be mailServer.quit(), but that crashes...
  67. mailServer.close()
  68. print "Email Sent"
  69.  
  70. else:
  71. fname = fname + ".h264"
  72. record_video(fname, RECORDING_TIME)
  73. msg = MIMEMultipart()
  74.  
  75. msg['From'] = gmail_user
  76. msg['To'] = to
  77. msg['Subject'] = subject
  78. msg.attach(MIMEText(text))
  79. mailServer = smtplib.SMTP("smtp.gmail.com", 587)
  80. mailServer.ehlo()
  81. mailServer.starttls()
  82. mailServer.ehlo()
  83. mailServer.login(gmail_user, gmail_pwd)
  84. mailServer.sendmail(gmail_user, to, msg.as_string())
  85. # Should be mailServer.quit(), but that crashes...
  86. mailServer.close()
  87. print "Email Sent"
  88.  
  89. upload_to_dropbox(fname)
  90. os.remove(fname)
  91. print "rpi-ms-camera: " + fname + " deleted."
  92.  
  93. # take a photo and store it a file with a unique name
  94. # based on the current date and time
  95. def snap_photo(file_name):
  96. #camera.rotation = 180
  97. camera.resolution = (1024, 768)
  98. camera.capture(file_name)
  99. print "rpi-ms-camera: Photo taken."
  100.  
  101. # record video for the specified number of seconds and
  102. # store it in a file with a unique name based on the
  103. # current date and time
  104. def record_video(file_name, rec_time):
  105. camera.resolution = (650,480)
  106. camera.start_recording(file_name)
  107. print "rpi-ms-camera: Video recording started."
  108. camera.wait_recording(rec_time)
  109. camera.stop_recording()
  110. print "rpi-ms-camera: Video recording stopped."
  111.  
  112. # upload the file to the specified folder in dropbox
  113. # and delete the file after the upload completes
  114. def upload_to_dropbox(local_file_name):
  115. dropbox_file_name = os.path.basename(local_file_name)
  116. print "rpi-ms-camera: Uploading " + dropbox_file_name + " to Dropbox."
  117. upload_command = DROPBOX_COMMAND + ' upload ' + local_file_name + ' ' + dropbox_file_name
  118. # print "rpi-ms-camera: " + upload_command
  119. subprocess.call([upload_command], shell=True)
  120.  
  121. # create a file with the IP address of the raspberry pi
  122. # and upload it to dropbox so the user can easily get the
  123. # address if ssh is needed to connect to it
  124. def upload_ip_address():
  125. print "rpi-ms-camera: Uploading IP address to Dropbox."
  126. ip_file_name = LOCAL_DIRECTORY + "IP-" + generate_file_name() + ".txt"
  127. ip_command = "hostname -I > " + ip_file_name
  128. subprocess.call([ip_command], shell=True)
  129. upload_to_dropbox(ip_file_name)
  130. os.remove(ip_file_name)
  131.  
  132. # call the dropbox-uploader script for the first time to get it to
  133. # ask the user for the API keys needed to configure the uploader.
  134. # this is used as part of the setup process
  135. def first_time_config():
  136. subprocess.call([DROPBOX_COMMAND], shell=True)
  137.  
  138. # call the dropbox uploader command to make sure it's working properly
  139. # by uploading a file with the IP address of the Raspberry Pi.
  140. # this is used as part of the setup process
  141. def test_dropbox():
  142. print "rpi-ms-camera: Testing Dropbox connection."
  143. upload_ip_address()
  144. print "rpi-ms-camera: Check your Dropbox app folder for the uploaded file"
  145.  
  146. # Main program
  147.  
  148. # the switch specified on the command determines whether
  149. # a photo or video should be captured and uploaded
  150. # the program can be started one of three ways:
  151. #
  152. # 'sudo python rpi-ms-camera.py -p' snaps photos
  153. # 'sudo python rpi-ms-camera.py -v' capture video
  154. # 'sudo python rpi-ms-camera.py -firsttime' runs first time configuration
  155. # 'sudo python rpi-ms-camera.py -test' tests to make sure upload is working
  156.  
  157. # determine what command line parameters were specified
  158. if len(sys.argv) > 1:
  159. if sys.argv[1] == "-p":
  160. camera_mode = PHOTO
  161. elif sys.argv[1] == "-v":
  162. camera_mode = VIDEO
  163. elif sys.argv[1] == "-firsttime":
  164. first_time_config()
  165. sys.exit()
  166. elif sys.argv[1] == '-test':
  167. test_dropbox()
  168. sys.exit()
  169. else:
  170. print "Invalid option specified"
  171. sys.exit()
  172. else:
  173. print "Valid options are:"
  174. print " -p Snap photos when motion is detected"
  175. print " -v Record video when motion is detected"
  176. print " -firsttime Run to configure the Dropbox application"
  177. print " -test Used to test the connection to Dropbox"
  178. sys.exit()
  179.  
  180. print "rpi-ms-camera: Raspberry Pi motion sensitive camera started."
  181. print "rpi-ms-camera: " + camera_mode + " will be captured when motion is detected."
  182.  
  183. # setup raspberry pi camera
  184. camera = picamera.PiCamera()
  185. camera.hflip = False
  186. camera.vflip = False # True
  187.  
  188. # setup GPIO pin for the PIR (motion) sensor
  189. GPIO.setmode(GPIO.BCM)
  190. GPIO.setup(PIR_GPIO_PIN, GPIO.IN)
  191.  
  192. # upload the IP address of the Raspberry Pi to Dropbox so the
  193. # user can use "ssh" to connect to it if needed
  194. upload_ip_address()
  195.  
  196. # main loop to detect motion and snap pictures or record videos
  197. while True:
  198. try:
  199. print "rpi-ms-camera: Waiting for motion."
  200. GPIO.wait_for_edge(PIR_GPIO_PIN, GPIO.RISING)
  201. motion_detected(PIR_GPIO_PIN)
  202. print "rpi-ms-camera: Sleeping for " + str(WAIT_TIME) + " seconds."
  203. time.sleep(WAIT_TIME)
  204. except:
  205. print "rpi-ms-camera: Stopping due to keyboard interrupt."
  206. camera.close()
  207. GPIO.cleanup()
  208. break
Advertisement
Add Comment
Please, Sign In to add comment