1

This program is coded within Python.

As I wanted to get my raspberry to be aware of the current time(night or day) and being able to upload a video that is using night vision from the normal cam.exposure_mode = "night", my program keeps not filming in night vision some how. I have tried to do this beside python, by command line, and this seems to work with night vision:

raspistill -ex night -o test.jpg

Would be awesome with some help, of what is going wrong in my code.

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import picamera
import datetime
import subprocess
import dropbox
import os

# Get your app key and secret from the Dropbox developer website
app_key = 'keyHERE'
app_secret = 'secretHERE'

def getFileName():
    return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")

def dropboxAuth():
    accessTokenFileOverwrite = open("accessToken.txt", "w+")

    flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
    authorize_url = flow.start()

    # Have the user sign in and authorize this token
    authorize_url = flow.start()
    print '1. Go to: ' + authorize_url
    print '2. Click "Allow" (you might have to log in first)'
    print '3. Copy the authorization code.'
    code = raw_input("Enter the authorization code here: ").strip()

    try:
        # This will fail if the user enters an invalid authorization code
        access_token, user_id = flow.finish(code)
        accessTokenFileOverwrite.write(access_token)
    except:
        print "failed authorization, restart"
        accessTokenFileOverwrite.close()
        os.remove("accessToken.txt")

    accessTokenFileOverwrite.close()

def dropboxUpload(fileToUpload):
    if not os.path.isfile("accessToken.txt"):
        dropboxAuth()

    #get access token from file
    accessTokenFileRead = open("accessToken.txt", "r")
    access_token = accessTokenFileRead.read()
    accessTokenFileRead.close()

    # make client
    client = dropbox.client.DropboxClient(access_token)

    #upload file
    fileToUploadObject = open(fileToUpload, "rb")
    response = client.put_file(fileToUpload, fileToUploadObject)
    fileToUploadObject.close()


sensorPin = 7

GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

prevState = False
currState = False

cam = picamera.PiCamera()

currentTime = datetime.datetime.now().strftime("%H:%M")
nightlight = "18:00"
daylight = "04:45"

while True:
    time.sleep(0.1)
    prevState = currState
    currState = GPIO.input(sensorPin)
    fileName = getFileName()
    if currState != prevState:
        newState = "HIGH" if currState else "LOW"
        print "GPIO pin %s is %s" % (sensorPin, newState)
        if currState and currentTime < daylight and currentTime <= daylight:
            print "Starting Recording..."
            print "Time is: "+ currentTime + " meaning we will use night vision!"
            cam.exposure_mode = 'night'
            cam.start_preview()
            cam.start_recording(fileName)
            cam.wait_recording(20)
            dropboxUpload(fileName)
            print (fileName)
        elif currState and currentTime > daylight and currentTime <= nightlight:
            print "Time is: "+ currentTime + " it is still daylight"
            cam.exposure_mode = 'auto'
            cam.start_preview()
            cam.start_recording(fileName)
            cam.wait_recording(20)
            print (fileName)
        else:
            cam.stop_preview()
            cam.stop_recording()
            print "Stopped Recording"
            print "Sending Mail Notification..."
            subprocess.call("mail -s 'Motion Detected!' sample@email.com < /home/pi/message.txt", shell=True)
            print "Complete"
            print "Uploading footage to Dropbox..."
            #dropboxUpload(fileName)
            print "Complete"

Thank you! :)

  • Do you get an error message? if so what? Do you get a video file? if so what's wrong with it? – Chris H Jun 24 '16 at 15:59
  • There is no error, the only thing I see on the video is that it does not use the "exposure_mode = 'night' " - this means that it will never record in night mode, but treat the video as day light. So the only failure I get is that exposure_mode is not working as expected. – R3verse Jun 29 '16 at 10:14
  • And you get "...we will use night vision" as expected? – Chris H Jun 29 '16 at 10:15
1

if currState and currentTime < daylight and currentTime <= daylight is probably your problem. I suspect you mean if currState and currentTime <= daylight or currentTime > nightlight. If you're testing in the evening you'd never trigger the nightlight setting with your conditional.

  • I will get back to you to check if this works when I get home. But that does still not resolve the exposure_mode = "night" function for the video to record in night ( meaning night vision - because I have a camera that supports that ). – R3verse Jun 29 '16 at 10:15
  • Do you mean that a very simple python program with no conditionals still won't do night mode? (should be around 6 lines I think, I don't have a PiCam here) – Chris H Jun 29 '16 at 10:19
  • Yes that is true. I cannot get it working with that code I wrote above. Please let me know if you find a solution. – R3verse Jun 30 '16 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.