# Pir_camera.py
#
# Polling script to take and upload images when Pir sensor is triggered
# Rob Davis 2015
#
# major revision, checks state.txt on dropbox as to operational nature 2016/05/01
import RPi.GPIO as GPIO
import time
import sys
import os
import picamera
import datetime
import dropbox
#urllib3 added to fix ssl warnings when using the Dropbox libraries
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
# Never upload your secret keys to github!
client = dropbox.client.DropboxClient('-------YOURSECRETKEYGOESHERE-------')
liveOutput = False
#Create a camera interface with the half the maximum 5MP resolution
cam = picamera.PiCamera()
cam.resolution = (1296, 972)
#Which GPIO pin are we using?
sensor = 4
#Set the GPIO pin ready for input
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)
#simple state variables
previous_state = False
current_state = False
state = "+"
capturing = False
captureCount = 0
#Upload a file name to a path on DropBox
def UploadFile(filename, uploadPath):
fileHandle = open(filename, 'rb')
response = client.put_file(uploadPath + filename, fileHandle)
#really should check the response, but, it always works ;)
os.remove(filename)
#Get a unique filename based on the datetime
def GetFileName(count):
return datetime.datetime.now().strftime("%Y%m%d%H%M%S_" + str(count) + ".jpg")
#Get a path based on the date, so a new folder each day
def GetUploadPath():
return datetime.datetime.now().strftime("%Y%m%d/")
#Get state flag from dropbox
def GetState():
state = True
value = "on"
with client.get_file('/status.txt') as fileHandle:
value = fileHandle.read(3)
fileHandle.close()
print "state",value
state = value == "on"
return state
#Get time plus check delay (in seconds)
def NewCheckTime(delay):
return time.time()+delay
if len(sys.argv) >= 2 and sys.argv[1] == "-v" :
liveOutput = True
isRecording = True
#seconds until next check
delayUntilNextCheck = 60
lastRecordingState = NewCheckTime(delayUntilNextCheck)
#Main loop in a try/catch to allow for CTRL+c termination
try:
while True:
time.sleep(0.01) # very short sleep, 100th of a second
previous_state = current_state # back up current state
current_state = GPIO.input(sensor) # get new 'current' state
if current_state != previous_state:
new_state = "HIGH" if current_state else "LOW"
if new_state == "HIGH":
state = "+"
capturing = True
else:
state = "="
capturing = False
captureCount = 0
else:
state = "_"
if capturing:
state = "[o]"
captureCount += 1 # allows for multiple frames per-second
if time.time()>lastRecordingState:
isRecording = GetState()
lastRecordingState = NewCheckTime(delayUntilNextCheck)
if isRecording:
filename = GetFileName(captureCount)
uploadPath = GetUploadPath()
cam.annotate_background = picamera.Color('black')
cam.annotate_text = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cam.capture(filename) # get a photo from the camera
UploadFile(filename, uploadPath) # upload the photo to a 'todays' folder on DropBox
if liveOutput :
sys.stdout.write(state) # print out the state without a LF
sys.stdout.flush() # flush to stdout or it will sit waiting for a LF
except KeyboardInterrupt:
print "User Quit, doing GPIO clean up"
GPIO.cleanup()
/usr/local/lib/python2.7/dist-packages/urllib3/util/ssl_.py:97: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
sudo pip install --upgrade ndg-httpsclient
sudo apt-get update
sudo apt-get install python-dev libffi-dev libssl-dev build-essential
wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
sudo pip install --upgrade ndg-httpsclient
sudo pip install urllib3[secure]
sudo pip install --upgrade urllib3[secure]
/usr/local/lib/python2.7/dist-packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
sudo pip install --upgrade pyopenssl ndg-httpsclient pyasn1
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
email
root
flog archives
Disclaimer:
This page is by me for me, if you are not me then please be aware of the following
I am not responsible for anything that works or does not work including files and pages made available at www.jumpstation.co.uk
I am also not responsible for any information(or what you or others do with it) available at www.jumpstation.co.uk
In fact I'm not responsible for anything ever, so there!