Sunday, 16 November 2014

Part 2: How to make your own Raspberry Pi Trail Camera 'PiTrailCam': Basic design model

In part 1 of this series, I outlined how a Trail Camera is made, and what might be used to make a home-brew version.  

I've put together a basic functional trail camera, a very early prototype if you like. 
I'm testing with a standard Raspberry Pi camera, not the IR version.  I do have an IR version, but haven't got around to using it yet.  For now, I'm limited to daylight triggering since I've not yet incorporated my IR illuminators since the box (tupperware - no expense spared) was too small to incorporate all the required bits.

This shows the prototype build so far (lack of hot glue gun skills notwithstanding).  This will be hardwired into my home network, and uses power-over ethernet (PoE) hence the wire going to into it.

Simple front view showing PIR and RasPi camera with LDR detailed in insert

PiTrailCam ver1 - Inside
This is a cut-down version of the version I had planned out on my desk, which included a relay that triggered a separately powered IR LED array, which unfortunately does not fit in the box.  Version 2 will probably be in a wooden box that is a bit more roomy and durable, and can accommodate a relay switch for the IR LED arrays.

Kit used in RasPi TrailCam version 1
Raspberry Pi model B
Raspberry Pi camera + supplied ribbon cable
Power over ethernet & connectors
I'm planning on directly splicing a hacked microUSB cable to the TP-Link power-out but bought the wrong have to make do with more bulky cables following this guide
  Micro USB (male) to USB cable (female)
  USB (male) to barrel power connector
Small prototyping breadboard
Female to male breadboard wires (various)
PIR motion sensor: Components, wiring and code described here
Mechanism to measure light level: code + components from here
Not strictly necessary in this build, but will be used to measure ilght level to activate IR led array in version 2:
  1x 2.2 kOhm resistor
  1x 1uF capacitor
  1x Light Dependent resistor

Prototype board layout as follows.  Note, I significantly slimmed this down to a small prototype board, but tend to use a larger breadboard with GPIO breakout at the 'drawing board ' stage.  this also shows a possible layout that will incorporate a relay to operate a pair of IR LED arrays:

For now, I'm just using the PIR (motion sensor) and LDR (measure light level) aspects .

First imageSome things I'm very happy with, others not I'm less so..

The observant among you will spot something wrong with this image...
Whats really cool is that I've got the time and date stamp included at the top (erm, bottom?) of this image.

Timstamp - just to prove it....
How the image capture works
I've coded the scripting side of things in Python.  I'm relatively new to this, so its a fun way of learning a new programming language, and to be fair most of this is mercilessly cribbed from other websites/blogs.

The Raspberry Pi camera is a described here, and can be used to capture video or stills, or both at the same time (may come back to this).  You'll mostly see the two programs Raspistill and Raspivid referred to which are used to capture still images and video respectively.  There are also several third party libraries built for it.  For this application I've used the PiCamera library.
My limited Python skills will likely show here, but as I understand it this offers a way for python to directly access the camera hardware, versus using calls to external software (such as to Raspistill and Raspivid).

In your python code, you have the option of either calling Raspistill as follows:
os.system ("raspistill -o /mnt/SHARE/captures/TrailCam.jpg")

or using PiCamera module, where you can see I've added a camera.hflip and camera.vflip command to fix the upside down and back to front image above.
def recordImage2():
 timestamp ='%Y%m%d%H%M%S')
 print "Motion Detected: " ,  
 with picamera.PiCamera() as camera:
  camera.led = False
  camera.resolution = (2592, 1944)
  camera.framerate = (1, 1)
  camera.vflip = True
  camera.hflip = True
  camera.quality = 100
  camera.exposure_mode = 'auto'
  camera.awb_mode = 'auto'
  camera.image_effect = 'none'
  camera.color_effects = None
  camera.annotate_bg = True
  camera.annotate_text ='%Y-%m-%d %H:%M:%S')
  camera.capture_sequence(['/mnt/savelocation/TrailCam_'+timestamp+'_image%02d.jpg' % i for i in range(3)])

By calling recordImage2() on a PIR activation event causes 3 images (image1, image2, image3) to be saved to a network location that I've mounted to a folder in /mnt, and am currently saving files there.  Not sure how that will pan out if I switch to video though.

How to get text overlay working with picamera python library
I'm quite pleased that I have been able to get the text overlay working.  The next thing to do is see if I can get a dark background behind the time & datestamp, as date and time in white over a white sky isn't much use to anyone.

At first I could not image overlay to work, and could not see why the instructions here did not work.  Turns out I was running an version 1.5 of PiCamera, and need at least the current version (1.8 at this time).  You can tell which version you're running by following these instructions here.  You can update that by doing sudo update then sudo upgrade, you may need to update the Raspberry Pi's firmware : sudo rpi-update.

How to mount network drive to Raspberry Pi
For info, to setup network mounting you need to edit /etc/fstab as follows:

sudo nano /etc/fstab

Then add the following line:
//XXX.XXX.XX.XX/SHARE/TrailCamPi /mnt/SHARE cifs username=RasPiUser,password=RasPiPwd,uid=1000,_netdev 0 0

Ctrl & O, then Ctrl & X exits the nano text editor.
At a reboot, (or sudo mount -all) the network folder will be mounted at /mnt/SHARE

/XXX.XXX.XX.XX/SHARE/TrailCamPi = IP address of target PC with save file destination folder.  Although its not strictly necessary, and you could setup a network share with no user password protection, I've set mine up with a dedicated user RasPi user + password on the destination machine, and pass those credentials with the mount command.

I'll sign off with another badger video from my commercial Trail cam, captured a couple of days ago:

Next step is to swop out the standard RasPi camera to the PiNoir, and add some LED arrays.