Saturday, October 15, 2016

PI Critter Cam Day 13: Pics on Startup

Today, we're going to get our PI set up to start looking for critters and take pictures at startup.  So, when I plug it into the battery, the PI will boot and be ready for action.  

We're using the pi-timolo package now to watch for and take pictures of these critters.  Included in the pi-timolo package is a bash script (program that runs in the Raspian terminal) that you can set to run on startup.  This is according to the pi-timolo's wiki.  To run this guy on startup, you stick a path to the program in the file rc.local

To alter rc.local, use the Terminal and type in sudo nano /etc/rc.local

This brings up the terminal's file updating/ word processing program.

Scroll down to the line right above exit 0 and stick in the filepath and file name.

/home/pi/pi-timolo/pi-timolo.sh



After that, save the file with a ctrl + x, confirm you want to use the same file name with an Enter and you are done.  

Now, I'm going to put a big pile of birdseed in front of my pi, plug it in and see what I get at the end of the day.  

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.