Monday, October 10, 2016

PI Critter Cam Day 12: Enclosure

I've been slowly and methodically planning the enclosure for the critter cam.  I want something that can live outside, can hold a battery, and,obviously, the Pi's enclosure, but with the wifi dongle plugged in.  My ultimate goal is to have the Pi take pictures and post them automatically to Twitter.

Since I'm attempting to do this on the cheap, my enclosure plans started with a leftover container about the size of a few servings of mac and cheese, which should be the equivalent of all the components.  It's got latches on the side, which I'm hoping will keep it enclosed enough to keep the rain out.  


Due to my frugality, I'm attempting to use a freebie battery to power the Pi before investing in something more substantial.  This battery is small, but is intended for cell phones, which means you can plug the Pi into it via the mini USB port.  That really simplifies things significantly.  I tested it out and the Pi was able to boot  and take a picture with it as a power source.  Awesome!

Lastly, it has a tiny vent hole on the top that's about the same size as the camera lens.  To utilize this hole, but prevent rain from getting in, I bought some tack stuff via Amazon.  Here's what the completed enclosure looks like.


In order to fully utilize this case, however, I still need to figure out how to have the motion detector initialize on startup and figure out how to test this thing outside.  I'm thinking a tray with some birdseed might be a good test.  Until next time...

Tuesday, August 30, 2016

PI Critter Cam Day 11: More Motion Detection

Ok, it's been 4 weeks.  So, the software should now be installed.

We'll test it out using the Github instructions.

To do this, we simply enter these two lines into the terminal one at a time:

cd ~/pi-timolo 
./pi-timolo.py

And our screen shows the process checking for motion at approximately one second interval.  That must be these "failed to set port info" lines.

If we wave our hand frantically in front of the camera, we see that the message changes (Found Motion).  It then tells us it captured a picture and added white text.


If I check out the /home/pi/pi-timolo/ folder, I see there are pictures of my waving hand!   So, I think we have a successful test.

Now, what do I do with this?  Well, I need an enclosure and I need to figure out how to make the PI run this program at startup.  I should probably also figure out how to make it stop also if I need to.  Maybe a button or a switch to kill the python switch?

Our initial box will just be an indoors one for further testing.  I think I've got some tupperware that I can use...

Tuesday, August 2, 2016

PI Critter Cam Day 10: Motion Detection

After a bit of a hiatus, I am back.

I think I'm going to attempt to use pi-timolo after reading this guy's blog, which appears to be very similar to what I want to do.  He's capturing hedgehogs and I'm capturing whatever comes along.  Other than that, our goals are very similar.

So, tonight, we're going to try and install the software.

Using his GitHub instructions, I installed the software, but this must've kicked off a system wide update as it's updating a ton of stuff.  I'm going to leave it running overnight and see what we end up with tomorrow...


Sunday, June 12, 2016

PI Critter Cam Day 9: Turn the Delete Files Process into a Function

While I'm at it and before I move on to figuring the motion sensor part of our project, I'm going to really quickly turn the portion of the code that checks for and deleting old files into a function with the python code.  This way, I can keep it separate from the rest of the process and call it wherever I want.

It's pretty easy to do this.  You can just define it like so.
def delete_old_files():
    #create our expiration date
    current_date = datetime.datetime.today()
    #The variable representing the duration we want to subtract to create our expiration date.
    y = datetime.timedelta(weeks = 2)
    #And create our expiration date variable.
    exp_date = current_date - y
    #Create a list of picture files in our critter folder and delete the pics that are older than our expiration date.
    for x in os.listdir(critter_path):
        
        if datetime.datetime.fromtimestamp(os.path.getmtime(critter_path + x)) < exp_date:
            os.remove(critter_path + x)
            print('removing ' + critter_path + x) 
        else: print('not old enough to delete: (' + critter_path + x + ')')

and when you want to use it, you just put in the following:  delete_old_files()

I'm not really sure where I want to do this check.  Having it as a function allows me to put off the decision.

Thursday, June 9, 2016

PI Critter Cam Day 8: Looping through our pictures and deleting old ones

Our slow progression continues. We're still building a critter cam with our raspberry pi that'll take pictures of critters in my backyard when detected.

Today:  going through all the pictures already taken and deleting ones that have reached a certain age.  We're doing this to avoid the tiny hard drive on the Pi from getting too full.

In Python, there's an os package that allows you to create a list of files and folders.  If we create a list of files in our critter cam folder, we can iterate through it, checking the age of the pictures and deleting the ones that are too old.

First, we create a list of files in our critter folder using the listdir function.

critter_path = '/home/pi/Pictures/critter_cam/'
os.listdir(critter_path)

Next, we go through the list and check out how old the file is (building on what we learned last time by using the datetime module):

import datetime

for x in os.listdir(critter_path):
print(datetime.datetime.fromtimestamp(os.path.getmtime(critter_path + x)))

Building on that, if the file is older than two weeks, delete it.

for x in os.listdir(critter_path):
if datetime.datetime.fromtimestamp(os.path.getmtime(critter_path + x)) < exp_date:
os.remove(critter_path + x)
print('removing ' + x)

Now let's put it all together.

#import our needed modules
import os.path, datetime

#folder where we keep our critter pics
critter_path = '/home/pi/Pictures/critter_cam/'

#create our expiration date
current_date = datetime.datetime.today()
#The variable representing the duration we want to subtract to create our expiration date.
y = datetime.timedelta(weeks = 2)
#And create our expiration date variable.
exp_date = current_date - y

#Create a list of picture files in our critter folder and delete the pics that are older than our expiration date.
for x in os.listdir(critter_path):
if datetime.datetime.fromtimestamp(os.path.getmtime(critter_path + x)) < exp_date:
os.remove(critter_path + x)
print('removing ' + x)

Awesome!  Next up, we'll start working on getting the Pi to take a picture automatically when it sees an animal through the camera.

Friday, June 3, 2016

PI Critter Cam Day 7: Is the File Old Enough to Delete?

As promised, I'm working on figuring out whether a critter_cam pic is old enough to be deleted.

To figure this out, we need to figure out our expiration date.  We did that last time.

To get the age of a file, you've got to use the os.path module, which comes with the PI version of Python.

That module comes with a function that pulls the creation date and the modification date for you.

os.path.getmtime('/home/pi/Pictures/critter_cam/2016_05_23_19_41_16.jpg'))

HOWEVER, it'll return it in a long decimal value representing the date and time.  NOT HELPFUL.

The datetime module, however, can handle this for you.  It has a fromtimestamp function that'll turn it into a legit datetime format you can compare your expiration date to the file date.

All together now:

#import our needed modules.
import datetime, os.path

#create our expiration date
current_date = datetime.datetime.today()
#The variable representing the duration we want to subtract to create our expiration date.
y = datetime.timedelta(weeks = 2)
#And create our expiration date variable.
exp_date = current_date - y

#get the modification date for the picture file and convert it to a legit datetime value.
file_date = datetime.datetime.fromtimestamp(os.path.getmtime('/home/pi/Pictures/critter_cam/2016_05_23_19_41_16.jpg'))

#And see if the file_date is old enough to be deleted
#If it returns TRUE, we should delete it.  
exp_date > file_date

Next time, we'll put in a loop to check all the files in our critter cam folder and delete the ones that meet this criteria.

Monday, May 30, 2016

PI Critter Cam Day 6: Calculating Date Values

My goal for this go round is to figure out the age of a Raspberry PI file through Python and figure out if it's old enough to delete based on the current date and whatever parameter defines a file as being too old.

To do this, we're going to need the Python TIME module again.

I'm thinking that I can use one of the time functions to subtract my age parameter from the current date and create a variable consisting of my "expiration" date.  Any critter pic with a creation date older than this date should be deleted.

I actually need the datetime module to perform the math with the dates.  It doesn't appear that the TIME module allows for math with time components?

So, I have to collect the current date as a variable:
current_date = datetime.date.today()

And a variable (or, specifically a duration to the datetime module) representing the amount of time to take away to create an expiration date:
y = datetime.timedelta(weeks = 2)

The datetime module allows you to subtract a duration object from a date object.  The expiration date will be as follows:
exp_date = current_date - y

As usual, I didn't get as far as I had hoped.  Next time, we'll work on getting the file dates from the critter pics and doing the compare.