Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from picamera import PiCamera
- from ftplib import FTP
- import os
- import time
- import datetime
- camera = PiCamera()
- # Depends on how your camera is set up...my Pi is turned sideways
- camera.rotation = 270
- camera.hflip = True
- # Load FTP server credentials
- try:
- upload_ftp = os.getenv('CAMERA_FTP_SITE')
- upload_user = os.getenv('CAMERA_FTP_USER')
- upload_password = os.getenv('CAMERA_FTP_PASSWORD')
- except:
- raise Exception('Environmental variables with FTP info cannot be loaded')
- ftp = FTP(upload_ftp, upload_user, upload_password)
- # And run forever
- while True:
- t = datetime.datetime.utcnow().hour
- # Save bandwidth by not uploading during very night times
- if ((t - 7) > 5) or ((t - 7) < 1):
- camera.capture('/tmp/cam.jpg')
- try:
- f = open('/tmp/cam.jpg', 'r')
- # First we upload the image to a temporary name, then rename it on the server.
- # This prevents someone loading the image online as corrupted during upload.
- ftp.storbinary('STOR new-cam-temp.jpg', f)
- ftp.rename('new-cam-temp.jpg', 'new-cam.jpg')
- except Exception, e:
- print('Exception: %s' % (e,))
- try:
- ftp.close()
- ftp = FTP(upload_ftp, upload_user, upload_password)
- except Exception, f:
- print('Error, %s and %s' % (e, f))
- time.sleep(5)
- else:
- print('Not uploading during night time.')
- time.sleep(60)
Add Comment
Please, Sign In to add comment