Saving pictures from a pir motion detector


#1

I just got my pir motion detector module in the mail and I have been tinkering with it lately. I have made a python3 code that will detect motion then save the picture. My problem is that I want to save the photos onto a usb drive, but I do not know how to do that from with in python3 code. The pictures are just saving under the pi directory.

This is the site I have been using to help me along the way.

And this is my code so far.



import RPi.GPIO as GPIO import time import picamera import datetime # new

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

sensor = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)

previous_state = False
current_state = False

cam = picamera.PiCamera()

while True:
time.sleep(0.1)
previous_state = current_state
current_state = GPIO.input(sensor)
if current_state != previous_state:
new_state = “HIGH” if current_state else "LOW"
print(“GPIO pin %s is %s” % (sensor, new_state))
if current_state:
fileName = get_file_name() # new
cam.start_preview()
cam.start_recording(fileName) # new
else:
cam.stop_preview()
cam.stop_recording() # new

P.S any help is great


#2

Nevermind I saved the program in the usb. Then in an LXTerminal i went to the usb location using cd /media/flash. Ran the program and they saved to the flash drive.


#3

Hi!

At the beginning of your code add the following:

import os

Then, before the ‘while’ loop, add this:

os.chdir(your directory)

where ‘your directory’ is the path to your USB drive. This will change the directory to the USB drive before any pictures are taken.

Hope this helps!


#4

Thanks a lot it works!