Monday, 2 April 2018

Wireless access point bird box - Satellite box

I've planned a 'hub and spoke' arrangement of bird boxes.  I previously described the 'Wireless Access point' box (aka 'WAP box'), this post details a 'spoke/satellite' box.

Robin box in the undergrowth
This is a ground-level open-fronted box designed with robins in mind.  Last year, a robin family successfully raised a brood of three chicks about a meter away from this new box at ground level.  That nesting site has since disintegrated, so this is hoped to be come its successor.

The camera setup is designed to be generic so that I can remove it and swop it into other boxes designed with similar dimensions, in that way I can maximise the number of boxes, while not having to equip every one with a camera.

There is a ledge at the top that holds the camera section.  This consists of a Raspberry pi Zero-W, an 8 megapixel v2 'Pi-Noir' camera (sensitive to IR as well as visible light).  Underneath are 4x each of white and IR LEDs for illumination.  Power is via a 12v feed from the WAP-box, dropped to 5v via a recom r-78b5.0 1.5 switching regulator.  LEDs are powered form the same 5v source, and switched via a transistor array (ULN2003AN), on the R side of the picture below.  This allows low, medium and high setting on each of the IR and visible light, white LEDs.  The unoccupied left side slot is wired for a L293DNE to switch an IR-cut filter (to be added at some point later, for a how-to see this post).

The white LEDs are wired in parallel with a 50ohm resistor for each LED.  The IR ones are also in parallel, but with no extra resistors.

The circuitry is arranged on a commercial prototyping board designed with the same dimensions as the Pi zero-W.  The board layout is as follows:

Protoboard layout, same footprint as Pi Zero/W
I also added pins for a DS3231 real-time clock module, since the Pi is only able to get time from the network, and wanted to plan for occasions when it loses network connection (there's not enough headroom in the box to fit them on the actual GPIO pins).
As with most of my other boxes, video capture is via motion capture software pikrellcam with captured footage/images saved to a mounted network drive (i.e. off the box).

This clip shows the box in situ cycling through various illumination settings:

A couple of observations from the transition from 'testing to the real world...'
Pi Zero-W wifi reception once closed into a wooden box drops off somewhat.  Running the following command gives some idea of reception levels:  iwconfig wlan0 | grep -i --color quality
Output is: Link Quality=44/70  Signal level=-66 dBm

The impact of this is that I cant set PikrellCam to record video captures direct to the network drive, as it all keels over.  Handily there is an archive option, so I just need to keep an eye on it and archive to the network when need be.  I also dropped the quality of the live stream which seems to have stabilised it.  Just need some visitors now....

Thursday, 15 March 2018

Wireless access point bird box

My current nestbox cameras are mostly raspberry pi-based with either a webcam or raspberry pi (RasPi) camera module to record and/or to stream digital video to a web-browser or CCTV software on a separate PC.  Power is via 'Power over ethernet' (PoE) where one ethernet cable provides power and a means to transmit the digital video.  Cable runs are approx 50m and 80m.

Using this setup, I'm limited to one camera per cable run:

Solution: The Wireless Point access bird box or 'WAP-Box'

I've taken an existing raspberry Pi (Raspberry Pi 2, model B)-based bird box, added a wifi dongle and set it up as a wifi access point (as per these instructions).  This is the 'WAP-Box', which helpfully can still be used to run its camera as well as being a wireless access point for other nearby devices.

In this setup, 48v PoE ethernet cable goes to the base of the tree.  At the tree base is a junction box where the cable is split out to ethernet-only and power, dropped to 5v using a TP-Link PoE splitter.  The 5V output is then used to power a  Mini USB hub using a  female USB to female barrel socket adapter, and the barrel connector supplied with the TP-link PoE splitter.  The usb hub can be used to power up to 4 devices. (note that this PoE kit also includes the power injector used in the house).

One of these boxes needs to be the 'WAP-box', which is a based around a wired ethernet Raspberry pi 2B with wifi dongle.  A PoE injector is used in the junction box to recombine 5V from the USB hub with the ethernet-only cable coming out of the TP-link PoE Splitter.  I hacked this cable from a pack of micro USB cables (keep the micro end to be used later) and a DC barrel connector.  I couldnt find a link for the PoE injector I used, but it came with a print server ages ago and had been in the 'may come in useful' drawer.  The 5v PoE cable then supplies power & data to the WAP-box up the tree. I opted to separate the usb hub from the WAP-box as I didnt fancy climbing a tree each time I wanted to connect/disconnect a device.

At the WAP-box, the 5v PoE cable is split into ethenet-only and 5V using a conventional PoE splitter and the otther half of the hacked micro usb spliced into a DC barrel connector to power, and supply wired ethernet to the the Raspberry Pi

So... we now have a wireless access point up a tree.

The additional 'Satellite NestBoxes' take a 5v feed via cables made from a length of black 2-core bell wire and the same microusb and DC barrel connectors linked to above.  The cables power a Raspberry Pi Zero-W + Raspberry Pi v2 IR camera module combo in each box which will also have a couple of IR LEDs to for illumination, powered direct from the 5V feed (not via the Pi ZeroW). The satellite boxes are pre-configured to connect to the WAP-box, and thus can save any captured video direct to my home network, so I dont need to worry about the little Raspi ZeroW's running out of space on their SD cards.

This all currently works on the bench with 4 concurrently running camera setups using PikrellCam software, and will be going up in the next couple of weeks once their various boxes are completed.  In testing, the Pi Zero-W can 'see' the WAP-box up to about 30m away - my main concern is that if the 5v cable runs are too long the voltage drop may lead to instability of the Pi Zero-W's, so that is likely to limit the cable run distance rather than the strength of the WiFi signal.  I also need to have a think about the current load on the 48v power injector at the house end...

More to follow once some boxes are up...

Of note is that the Raspbery Pi foundation recently released a new version of the highest performing version, called the Model 3B+.  This is interesting this context as it could potentially make a better WAP-box core since it has a faster ethernet connection.  Currently, simultaneously live streaming 4 Raspi-zeros via the existing Pi2 Model B is a bit choppy, which may be due to a bottleneck at the WAP-box's ethernet connection... or this could just be me using this as an excuse to play with one...

Saturday, 24 February 2018

Coding for day or night events

This post was promoted by a post on the  GarysShed blog after a discussion on triggering events based on whether it's day or night time.

To be fair, the whole post is an excuse to include following video from the BBC's 'Walk on the Wildside' series

I needed to modify the behaviour of the illumination LEDs in one of my Raspberry Pi-based  birdboxes based on whether it is light or dark.  This box has IR (Infra-Red) as well as visible light LEDs, I wanted the IR to operate all the time, and the standard LEDs to come on (and IR LED go off) only in the event of a daytime  motion 'event', which could be triggered by any of:  Entrance hole IR beam being broken, PIR insider the box triggered or motion detected on camera.

An IR-cut filter over the camera can then be switched on or off depending based on this too - IR LED off when visible LEDs are on.  (An IR-cut filter turns an IR-sensitive camera into a daylight-only sensitive camera).

The idea was that I didn't want visible LEDs coming on during the night, mainly to disturb any occupants, I also didn't want this happening as it looks a bit odd from the outside:

Possible solutions:
1) Analyse camera image for brightness
2) Use a light-dependent resistor (LDR) to sense ambient light and modify LED behaviour based on this.
3) Implement a software-method to predict day vs night based on longitude and latitude

1) Analyse camera image for brightness.  Borrowed code from here and here and here
convert test.png -colorspace hsb -resize 1x1 txt:- | sed -n 2p | sed 's/.*(//; s/).*//'

If run in the terminal, will output something like: 81.857%,38.706%,17.7462% where these figures are the HSB (hue, saturation, brightness) values from a 1x1 pixel converted version of the original image (test.png in this example)

2) Use LDR to monitor light.  There's loads of examples out there how to do this, a randomly selected recent one here

At one time I did a merge of method 1 & 2 to which I've uploaded to a github repository Ambient-Light-Monitoring, the script is called  This reads actual light level via an  LDR, and also takes image with a Raspberry Pi Camera and measures image brightness.  All these variables are then written to a file for playing with which could be applied with in a variety of contexts..

3) Software method
I've tried two approaches, both rely on feeding the application the current location via longitude and latitude.

3.1) SunWait.  A stand-alone program, an example use is described in a blog post here.  Essentially you run a crontab job every ?minute that writes to a file to indicate the current light/dark state.  You can then read that file into your application and base the outcome on whether its day (..light) or night (...dark).  I could not get it to work..

3.2) Python module: pyephem, website here
This is a python module, so is used within a python script.  You need to install python-dev first or the install of this module will fail. The module can then be installed with the pip python module installer
sudo apt-get install python-dev
pip install ephem

This approach worked for me.  Longitude and Latitude can be generated on various websites, eg here, pyephem also uses height above sea level which I think I got off google earth.  This would be aprticularly relevant if you're using it at the top of a mountain....
Refer to the example script in the above mentioned githib repository

3.3) ?Pikrellcam motion capture application
I use the awesome motion capture application PiKrellCam in my birdboxes.  In its .config file is the facility to define location by longitude and latitude.  I spotted a recent forum post querying if it was possible to switch an IR-cut filter on or off by using the day/night status within Pikrellcam to trigger a command to the GPOI pin(s) controlling the IR cut.  See my IR-cut post on how to wire such a thing up.

Thursday, 31 August 2017

Control BirdBox LED lights via a simple webpage

This year's 'Double Camera' bird box has a variety of illumination options...

I wasn't sure how best to setup the internal lighting, so I, well threw a selection of LEDs at it in three separate circuits/loops, These are on 12v circuits, and are isolated from & switched by the (5v) raspberry pi via a ULN2003AN transistor ic.  I used all 7 base input channels, with the collector outputs switching the 12v LED loops through a selection of resistors as follows:

Loop 1) 4x 'Warm' LEDs: LOW (10k ohm) and HIGH (330 ohm)
Loop 2) 3x 'White' LEDs: LOW (10k ohm) and HIGH (330 ohm)
Loop 3) 36 IR LED array: LOW (10k ohm), 'MED' (5k ohm potentiometer) and ('HIGH' 330 ohm)

+ An opaque perspex 'windows' at either end for the natural light options..

Once the birdbox was in-situ, I wanted a simple way to fiddle about with the lighting, and came across Matt Richardson's excellent Flask  tutorial, specifically his script example.  This creates a webserver within python that allows me (and the kids...) to turn LEDs on, off up or down via a simple webpage:

The tutorial is an excerpt from his Getting Started with Raspberry Pi: An Introduction to the Fastest-Selling Computer in the Worldbook

Just for fun - someone's at home....
I've adapted this for my needs as follows:

import RPi.GPIO as GPIO
from flask import Flask, render_template, request
app = Flask(__name__)



# Create a dictionary called pins to store the pin number, name, and pin state:
pins = {
   25 : {'name' : 'LED_IR_High', 'state' : GPIO.LOW},
   8 : {'name' : 'LED_IR_Med', 'state' : GPIO.LOW},
   7 : {'name' : 'LED_IR_Low', 'state' : GPIO.LOW},
   16 : {'name' : 'LED_Warm_High', 'state' : GPIO.LOW},
   12 : {'name' : 'LED_Warm_Low', 'state' : GPIO.LOW},
   21 : {'name' : 'LED_WHITE_High', 'state' : GPIO.LOW},
   20 : {'name' : 'LED_WHITE_Low', 'state' : GPIO.LOW}

# Set each pin as an output and make it low:
for pin in pins:
   GPIO.setup(pin, GPIO.OUT)
   GPIO.output(pin, GPIO.LOW)

def main():
   # For each pin, read the pin state and store it in the pins dictionary:
   for pin in pins:
      pins[pin]['state'] = GPIO.input(pin)
   # Put the pin dictionary into the template data dictionary:
   templateData = {
      'pins' : pins
   # Pass the template data into the template main.html and return it to the user
   return render_template('main.html', **templateData)

# The function below is executed when someone requests a URL with the pin number and action in it:
def action(changePin, action):
   # Convert the pin from the URL into an integer:
   changePin = int(changePin)
   # Get the device name for the pin being changed:
   deviceName = pins[changePin]['name']
   # If the action part of the URL is "on," execute the code indented below:
   if action == "on":
      # Set the pin high:
      GPIO.output(changePin, GPIO.HIGH)
      # Save the status message to be passed into the template:
      message = "Turned " + deviceName + " on."
   if action == "off":
      GPIO.output(changePin, GPIO.LOW)
      message = "Turned " + deviceName + " off."
   if action == "toggle":
      # Read the pin and set it to whatever it isn't (that is, toggle it):
      GPIO.output(changePin, not GPIO.input(changePin))
      message = "Toggled " + deviceName + "."

   # For each pin, read the pin state and store it in the pins dictionary:
   for pin in pins:
      pins[pin]['state'] = GPIO.input(pin)

   # Along with the pin dictionary, put the message into the template data dictionary:
   templateData = {
      'message' : message,
      'pins' : pins

   return render_template('main.html', **templateData)

if __name__ == "__main__":'', port=90, debug=True)

Make it executable as follows:
chmod 755

Add the following toward the end of /etc/rc.local to make it run at boot:
python /home/pi/Documents/BirdCam2/flask/WebLamp/ &

The IR sensitive camera is a v2 Raspberry pi Pi Noir (Camera 1).  Unsurprisingly, the IR HIGH setting to just too bright, with the MED setting being the sweet spot.  IR_Low (10k ohm) barely registers... At the moment I've got it set to IR_Medium.

I haven't made much use of the non IR LEDs yet, but the LifeCam Cinema webcam (Camera 2) does not see very well without them being on, which is bit of a shame.  Maybe a ver 2018 box could have bigger natural light windows (Or I'll take a hacksaw to this one...).

We've seen a fair number of wasps taking an early interest and this Hornet (I think) taking a look... This is using the IR LED array and Pi Noir v2 camera

The LifeCam version looks like this.. the lesson is that I need a motion trigger on the non IR LEDs as there is only natural daylight illuminating this:

Some thoughts...
36 IR leds is excessive.
The raspberry v2 Pi Noir daylight light sensitivity is not good (with IR cut, so conceivably this is reducing the 'useful' daylight sensitivity).
I dont use the 'LOW' setting on the WARM or WHITE leds
LifeCame Cinema needs more natural light for unassisted daylight video (bigger windows)
Need an 'on-motion' event to turn on non-IR leds.

Monday, 5 June 2017

ZeroView IR-Cut hack

This is hopefully the beginning of something beautiful...  This a ZeroView, which is a neat way to attach a Raspberry Pi zero-W + camera module to a window:

I've modified modified the ZeroView (+IR camera module) to squeeze in an infra-red cut filter (IR-cut) between the ZeroView and the PiZero, making a wifi enabled 8MP camera that can see in the dark (+IR illumination)...

This is still very much a work in progress as I need to add an IR illumination source too.  Some incarnation of this will find its way into a nest box or a trail camera.  As-is it does stay stuck to a window, but the addition of the perfboard does make it a bit top-heavy.  I'm sure it can be refined a bit...

IRcut OFF (top) & ON (bottom)

Here is a video showing filter on and off.  This was captured using PikrellCam:

Filter is switched using a L293DNE, connected in the above example as follows:

Sample python script:
import RPi.GPIO as GPIO
import time
import datetime as dt
import os



for IRcutPin in IRcutPins:

#Default states:
global IR_cutState

def separator():
    print "---------------------------"

def IRcut_DayTime():

    print "IR cut DAY"
    print "============="
    print "IRcutEnable ("+str(IRcutEnable)+") = HIGH"
    print "IRcut1A ("+str(IRcut1A)+") = LOW"
    print "IRcut2A ("+str(IRcut2A)+") = HIGH"
    print ""
    global IR_cutState

def IRcut_NightTime():    

    print "IR cut NIGHT"
    print "============="
    GPIO.output(IRcutEnable,GPIO.HIGH) #was LOW
    GPIO.output(IRcut2A,GPIO.LOW) #was HIGH
    print "IRcutEnable ("+str(IRcutEnable)+") = LOW"
    print "IRcut1A ("+str(IRcut1A)+") = HIGH"
    print "IRcut2A ("+str(IRcut2A)+") = L"
    print ""
    global IR_cutState

def IRcut_STOP():    

    print "STOP"
    GPIO.output(IRcut2A, GPIO.LOW)

while True:

Sunday, 28 May 2017

Getting your Bird Box camera online: Analogue vs Digital cameras

Live birdbox camera web streaming is a great way to involve others in the development of your nesting birds.  I've attempted to collect together the various options to do this, together with how I streamed my 2017 Robins

Yes dear, I know digital cameras can be expensive, but you can DIY for a lot less...

This post aims to touch on the following...
1) Review of analogue versus digital cameras for nest boxes: Commercial and home-brew
2) How to convert your analogue camera to digital
3) Putting your birdbox on online

You can potentially spend a lot on this, or alternatively go with my approach using a low cost, home brew setup using a Raspberry Pi + webcam combo, or use the Raspberry pi foundation camera module.

Getting started
The first thing you'll need to get your bird box camera online is a digital video source. 

Many commercial birdbox cameras use analogue cameras and are designed to connect via a wired or wireless connection (2.4Ghz radio) to a SCART/composite video in your TV or via BNC type-connector to a video recording system (capture card to PC), or digital video recorders (DVRs) sold for CCTV.  As-is, analogue cameras won't let you do live streaming, you'll need to convert your analogue video to digital, or start off with a digital camera in the first place.

Analogue camera kits are available from a variety of sources, e.g.  HandyKam, UK wildlifecameras and others (I've borrowed their images, so its only polite to link out to them...).  These are mainly described as connecting to a TV (or DVR).

Examples of analogue cameras, and what to expect in a kit.

How to convert analogue video to digital video...
To get your analogue camera online, there are a couple of options to convert it to a digital form.

Analogue capture to PC: There are lots of bits of kit that can convert an analogue video signal to digital.  Some sit outside your PC.  Alternatively use a dedicated video capture card in your PC. I picked up a couple of Picolo Pro 2 PC capture cards on ebay a few years ago and have used one to for a few seasons to capture an analogue camera using iCatcher PC software in a tawny owl-come-squirrel nest box (replaced a few yrs ago with a home-brew digital setup).

Standalone IP converter.  Directly connect an analogue video source to the internet.
There are some scenarios where these may be more appropriate e.g. Inaccessible sites e.g. Norwich Cathederal Peregrines (note this does not work in chrome) or in schools where there may not be much in the way of IT support, or the person setting it up does not want to  bother with a PC component. These are expensive, and for this reason I've not tried one (would like to...).  This Axis IP converter is one example available from HandyKam, and once purchased, the per camera cost drops since several analogue cameras can be converted the same time (depending on your kit that is).

Alternatively you can just generate a digital source in the first place...

IP (IP='Internet Protocol') cameras.  The cheapest ones are generally more expensive than analogue cameras, however as the camera itself generates the digital feed, you don't need as much intermediary 'stuff' to get your video stream online, offsetting the higher camera cost.  Video quality is often better and does not degrade like you may get with a long CCTV-analogue cable, particularly if used with analogue radio transmitters, especially if like me the local 2.4Gz spectrum is already pretty much full (neighbour's chicken cams!).

IP camera options
Commercial IP cameras: There are lots of examples at the 'cheaper' end of the professional spectrum (however still in the several £100's range). Kate MacRae 'WildlifeKate' uses a Vivotek IP8152 in her Tawny owlbox

This example is approx £240 - It wont fit into a typical small bird next box however!

Hobbyist IP camera kits: 'Birdbox IP' camera kits: e.g. this kit from HandyKam: 'DigiNestCam HD camera kit - Bird box, 2MP IP camera with IR.' This would be a good place to start if you're after an a commercial kit (£179.95).   The advantage of this kit is that the PoE (power over ethernet) and cabling is sorted out for you.  This alternative is similar with additional work needed by the user for powering it.  For the DIY-er, the new Raspberry Pi camera board is 8MP, so likely improving on the resolution of these.

Domestic IP cameras: ?May be worth looking at but these offer a range of options not really required (e.g. facial recognition, subscription monitoring), some of which are still expensive so are probably best avoided.

MAKE YOUR OWN <- this is what I've done using a Raspberry pi (credit card-sized PC) + camera comes in at approx £50 -ish to make a live-streaming capable device.  This is the cheapest & most flexible option, but relies on a bit of technical know-how and setup time (but not a lot...).  Note there is additional cost to sort out PoE and any cabling.

Analogue vs IP cameras for wildlife nest monitoring

Analogue camera
IP camera
Cheaper (under £100),
More expensive (several £100)
Alternatively go the home-brew Raspberry pi route = much cheaper (under £50)
Ease of use
Many commercial kits available Handycam, UkWildlife
Limited kits for domestic nature watching use, only a few kits available, this one does the PoE thinking for you...
May be easier to fit into a nest box
Commercial cameras are usually bulky, may be more appropriate for larger nesting sites, BUT Raspberry pi solution = small.
Wired Connection options
Via CCTV cable (often comes with kit).  I've had mine work over 50+ meters extending with (variable) success to 200m over wireless 2.4Ghz.
Usually power over ethernet (PoE), quite cheap to do .. will need to learn how to wire a cat5 ethernet cable if going the home-brew Raspberry pi route (not difficult).
Wireless options
Greater signal range via 2.4Ghz transmitter/receiver pairs.  Limited by number of channels available.  Prone to interference (e.g. microwave)
Transmit over wifi.  Transmission framerate may drop over increasing distance, no drop in video quality.
Signal strength (wired)
May degrade considerably over distance.  
Does not degrade over distance (within domestic context...)
Viewing video
Direct connect to TV SCART/ composite video or via analogue video capture to PC
Via router to PC connected to same network, or direct to internet.
Recording video
Without digital conversion, using commercial CCTV digital video recorders (DVRs).  If using digital conversion, all recording options under 'IP camera' are also valid
On PC using dedicated CCTV software e.g. iCatcher console, (£139 for 2 camera licence), or Zoneminder (FREE, runs on linux PC/Raspberry pi, more fiddly to setup).
Online streaming
Needs convertor if you want to get this online (+££)
Can stream via PC or direct from camera (if supported)

So, the point of this is was to describe/document how I got my Robins online.

I used a Raspberry pi + webcam combo setup as described here to generate a video stream (motion jpeg in this case).  I then used a program called ffmpeg on a second Raspberry pi to convert this to something that YouTube could use and stream to its live events facility.  You could do all this on the same Raspberry Pi but I had a second, more recent version handy (more oomph).

I'll cover this in more detail in a subsequent post...

Monday, 8 May 2017

Robin nest 2017 - a dramatic end?

Part of the early morning routine of late has been to 'check the robin chicks'.  They were getting quite big, and looked to be nearing fledging.... However, Saturday morning's camera view looked like this....

Something has moved the camera (08:13, Sat 7 May in the above screenshot).  As the three chicks have grown there has been a bit of camera jostling going on, but these have only been nudges, so a dramatic shift of perspective cant have been due to the chicks or adult birds.  For info, the above view is pointing out of the hole the adults have been feeding the chicks through, with the nest being a 90 degree rotation to the left.... the obvious question is '...are the chicks still there?

All motion is captured for this nest, we can 'rewind' and capture the 'event'.... so here it is:

Some screen captures identify the culprit....

Top: ?black and white hair
Middle: Looks suspiciously like a cat's paw poking through the entrance hole
Bottom: Is that a whisker?

Culprit = Cattus domesticus

So we immediately assumed that our robin chicks had met a furry end, even after my best attempts to cat proof the nest site, however if we go back to the previous evening, there are no chicks... so did the cat find an empty nest site?
Have the chicks flown the nest??

A collective sigh of relief all round as it turns out that the last two Robin chicks fledged at approx 5pm the previous day, so had a narrow escape.
Chick 2 fledges 
Chick 3 gone a few minutes later

The first chick fledged a full 24 hrs before chick 2+3 , however the adults kept up the feeding on the nest until approx 1 hr before the other two fledged.

Interestingly within a few hours of the nest being vacated... we spotted someone else possibly looking for a home.....

Maybe we will get a bumblebee nest after all....