-1

I have made a raspi0 PIR cam and am trying to get it to write to my network storage device. The .py writes to the drive but just keeps re-saving over the same file unless I ctrl C and manually restart.

from picamera import PiCamera
import time
from time import sleep
import os
import sys
from datetime import datetime
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

pir_sensor = 17 #GPIO physical pin 11

GPIO.setup(pir_sensor, GPIO.IN, GPIO.PUD_DOWN)

current_state = 0

filename_part1="surv"
file_ext=".mp4"
now = datetime.now()
current_datetime = now.strftime("%d-%m-%Y_%H:%M:%S")
filename=filename_part1+"_"+current_datetime+file_ext
filepath="/mnt/local_share/Surveillance_Captures/"

def capture_video():
 camera.start_preview()
 camera.start_recording('/home/pi/python_code/capture/newvideo.h264')
 camera.wait_recording(10)
 camera.stop_recording()
 camera.stop_preview()

def remove_file():
 if os.path.exists("/home/pi/python_code/capture/newvideo.h264"):
  os.remove("/home/pi/python_code/capture/newvideo.h264")
 else:
  print("file does not exist")
  
camera=PiCamera()

while True:
 i = GPIO.input(17)
 if i==1:
  print("Motion Detected")
  capture_video()
  sleep(2)
  res=os.system("MP4Box -add /home/pi/python_code/capture/newvideo.h264 /home/pi/python_code/capture/newvideo.mp4")
  os.system("mv /home/pi/python_code/capture/newvideo.mp4 "+filepath+filename)
  print("File Moved")
  print("Scanning Sector")
  sleep(2)
  remove_file()
KSW74
  • 1

1 Answers1

1

Because you put filename=filename_part1+"_"+current_datetime+file_ext out of while loop, so the filename never change.

while True:
    now = datetime.now()
    current_datetime = now.strftime("%d-%m-%Y_%H:%M:%S")
    filename=filename_part1+"_"+current_datetime+file_ext
zhenhua32
  • 256
  • 1
  • 6
  • zhen, thanks so much, you just cured hours of frustration, next time i will try something that may be challenging for you :) – KSW74 Jul 26 '21 at 13:45