Guest User

Untitled

a guest
Mar 7th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. from picamera import PiCamera
  2. from ftplib import FTP
  3. import os
  4. import time
  5. import datetime
  6.  
  7. camera = PiCamera()
  8.  
  9. # Depends on how your camera is set up...my Pi is turned sideways
  10. camera.rotation = 270
  11. camera.hflip = True
  12.  
  13. # Load FTP server credentials
  14. try:
  15. upload_ftp = os.getenv('CAMERA_FTP_SITE')
  16. upload_user = os.getenv('CAMERA_FTP_USER')
  17. upload_password = os.getenv('CAMERA_FTP_PASSWORD')
  18. except:
  19. raise Exception('Environmental variables with FTP info cannot be loaded')
  20.  
  21. ftp = FTP(upload_ftp, upload_user, upload_password)
  22.  
  23. # And run forever
  24. while True:
  25. t = datetime.datetime.utcnow().hour
  26. # Save bandwidth by not uploading during very night times
  27. if ((t - 7) > 5) or ((t - 7) < 1):
  28. camera.capture('/tmp/cam.jpg')
  29. try:
  30. f = open('/tmp/cam.jpg', 'r')
  31.  
  32. # First we upload the image to a temporary name, then rename it on the server.
  33. # This prevents someone loading the image online as corrupted during upload.
  34. ftp.storbinary('STOR new-cam-temp.jpg', f)
  35. ftp.rename('new-cam-temp.jpg', 'new-cam.jpg')
  36.  
  37. except Exception, e:
  38. print('Exception: %s' % (e,))
  39. try:
  40. ftp.close()
  41. ftp = FTP(upload_ftp, upload_user, upload_password)
  42. except Exception, f:
  43. print('Error, %s and %s' % (e, f))
  44. time.sleep(5)
  45. else:
  46. print('Not uploading during night time.')
  47. time.sleep(60)
Add Comment
Please, Sign In to add comment