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.





Monday, May 23, 2016

PI Critter Cam Day 5: Picture File Names with the Date and Time

Today, my goal is expand on the picture taking, have it take a picture with a date and time stamp file name and also delete pictures that have reached a certain age.  I'm hoping there will be lots of critters to take pictures of, but my micro SD card is only 8gb.

Since I can't store my pics on the PI long term, my ultimate goal is to have the pictures automatically post to a Twitter handle where they can be stored and archived (and shared).  That'll be further down the road.

So:

  • Capture a picture with a date and time stamp to make it unique and easier to manage.
  • Delete pictures that are older than 2 weeks.
We'll just do both in the same process for now.  It might not be ideal, but I think it'll be alright.  Trying to keep it simple.

We'll use the Python Time package for collecting the time.  

It's actually pretty straightforward manipulating the time into a string for a file name using the strftime function:

time.strftime('%Y_%m_%d_%H_%M_%S')

I can just stick it in like so.  I'm also going to add a folder so everything won't write to a root directory:


#Needed packages...
import picamera
import time

#create object and initialize camera.
cam = picamera.PiCamera()

#take picture!
cam.capture('/home/pi/Pictures/critter_cam/' + time.strftime('%Y_%m_%d_%H_%M_%S') + '.jpg')

Note:  you have to create the actual folder via the File Manager.  Python will not create it automatically by referencing it in the file name.

And it seems to work.  Here's the pic in the folder with the date and time stamp.


We ran out of time regarding deleting old files.  That'll have to wait until next time.

Thursday, May 19, 2016

PI Critter Cam Day 4: Camera Introduction and Keyboard Layout

Today, we make the camera work!

So, we've got it connected and we've got the picamera Python module.

I create a file with the Text Editor and put in the following.

#Needed packages...
import picamera
#create object and initialize camera.
cam = picamera.PiCamera()
#take a pic! 
 cam.capture("test.jpg")
Save it to the desktop as 'test.py.'  You can then double click the file on your desktop to run it.

(Yet another) Interesting tidbit about the PI.  It defaults the keyboard to use the British layout.  This'll cause [SHIFT] + 3 to enter a BRITISH pound, not the number pound (#).

To fix this, you can go to the Menu --> Preferences --> Keyboard and Mouse. Select the Keyboard tab, then press the Keyboard Layout button.

From that window, select the first US layout.  This is what I chose:
















Hopefully, this will be the layout I'm used to.

So, I didn't get a lot worked out after having to figure out the keyboard layout problem.  Tomorrow, more Python.

Wednesday, May 18, 2016

PI Critter Cam Day 3: Correcting IP and Screenshots.

We left off from last time trying to get the PI to connect to the internet.

I've forced my router to assign the PI to a static IP address.  I can see the PI now, but it's still not connecting to the internet.

Ah, I have to put the static IP address for the PI here, not the router's address:

And we have the internet!  In fact, I'm writing this on the PI. :)

Obviously, I also figured out screenshots.  You use Scrot, which comes out of the box with Raspian.  It takes some getting used to, but has lots of features, which you can see with it's help (scrot -h).

Here's what I used to get the active window with a 10 second delay (scrot -d10 -u).  



Thanks to this guy for getting me started with the screenshot.

So, now that we're off that tangent, we can start with the camera again.  Next, we'll work on seeing if we can get the camera to respond to commands through Python...

----------   UPDATE   --------------

I was still having trouble, until I saw the PI had no default gateway.  I was wondering where that was supposed to go!

Anyway, I found via stackexchange how to add it.  Here's a screenshot:







Tuesday, May 17, 2016

PI Critter Cam Day 2 Internet Issues...

Just when I thought I was rolling along, I run into a brick wall.

My pi can't connect to the internet anymore.

I tried resetting the pi, the router, but nothing seems to work.

I can type ifconfig into the terminal to get the IP address and can see it connected to the router.

When I hover over the wireless icon in the top right corner, it says "Configured."

I can even ping my router and other computers on my network, but not anything outside.  The screen states "Destination Host Unreachable."

I'll have to regroup and try and figure this out next time...

Sunday, May 15, 2016

Start of a New Project: PI Critter Cam

So, I'm starting a new project that I will inevitably not finish.  This one involves the use of a Raspberry PI.  My primary goal is to learn more about electronics and programming and how they interact.  My secondary goal is to build an outdoor camera that will take pictures of critters, particularly critters that are in the woods behind the house that my dog has taken a keen interest in.

So far, all of the components for the beginnings of the project have been ordered (I think) and I've got the Raspberry PI working.  In fact, I'm writing this on it!  I've got a long way to go, though.  A lot this is pretty new to me.

Will I get to the end of this project?  We'll see.  History is certainly not on my side, but I'll hopefully have fun along the way, regardless of where I end up.

The first step (after getting the PI to work as a computer):  getting the camera connected.  Here's it hooked up.


and I installed the Python module through the terminal.

I typed in:  sudo apt-get install python3-picamera.  I'd show a screenshot, but it looks like I have to install a PI program to do that.  I'll need to do a little research there.

Anyway, my next step will be to test it out, see if I can get Python to take a picture...