Spying on Fish, Need some help

Hi guys,

I’m a Python coding novice. In fact, this is the first piece of functional code I’ve ever written outside of code academy so bear with me. I’ve set up this Raspberry pi to trigger an infrared camera when the beam is broken on an IR break beam sensor. By frankensteining together similar code I’ve been able to get it to start recording 30 secs of video on the trigger and make a new file video file on a USB drive with the timestamp as the title. The problem that I’ve run into now is that once the code is running it doesn’t update the timestamp file names for new detections. Because of this, instead of making a new video file for every detection it keeps the old title and overwrites the last video file. I’m thinking that I have to make the code update the time at the end of every loop cycle but I’m having a hard time figuring out how to do that. Hopefully, the forum can help. Thanks in advance guys.

# Imports
from picamera import PiCamera
import time
import RPi.GPIO as GPIO
import datetime

# Variables
camera = PiCamera()
PIN = 4 # Pin we are using to read the IR break beam switch
prev_input = 0 # Variable to track if trigger beam is broken
now = time.localtime(time.time()) #Variable plugged into asci time to allow for readable date print out
timestamp = datetime.datetime.now().strftime("%m%d%y_%H%M%S") #Variable to update name of video files with current date and time

# Setup the GPIO
GPIO.setmode(GPIO.BCM) # GPIO layout mode
GPIO.setup(PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Setup the gpio pin we are reading from as a pullup input

# Loop checking the switch
while True:
    # Read the switch value
    input = GPIO.input(PIN)

    # If the GPIO reading goes from high to low, record for 30 secs
    if input != 1:
        camera.start_recording('/media/pi/Lexar/test_video/{}.h264'.format(timestamp)) #Recording video file to Lexar thumb drive

    #Debounce wait

#code also needs to:
  #send an email to my work email with time of detection (beam is broken)
  #make log file of all detections
  #continue recording if beam is broken past initial 30 secs

Background: This build will be used to monitor Pacific Lamprey passing through a fish ladder I’ve set up at a hatchery. Everything needs to be infrared because lamprey are negatively phototaxic and will move away from light in the visible spectrum.

Not a biggy.  You assign now/timestamp once, but don’t update them. So they keep the same value, never changing.

You need to move the timestamp/now lines inside the loop. I.e. move

now = time.localtime(time.time())
timestamp = datetime.datetime.now().strftime("%m%d%y_%H%M%S")

so your code reads (my suggestion):

if input != 1:
	now = time.localtime(time.time())
	timestamp = datetime.datetime.now().strftime("%m%d%y_%H%M%S")

(properly formatted for python of course…)

1 Like