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.

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()

        # This will fail if the user enters an invalid authorization code
        access_token, user_id = flow.finish(code)
        print "failed authorization, restart"


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

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

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

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

sensorPin = 7

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:
    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'
            print (fileName)
        elif currState and currentTime > daylight and currentTime <= nightlight:
            print "Time is: "+ currentTime + " it is still daylight"
            cam.exposure_mode = 'auto'
            print (fileName)
            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..."
            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

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.