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....

Thursday, 27 April 2017

Bumblebee nest box

We've rescued several queen bumblebees from the shed over the last few weeks - which reminded me to complete our bumblebee nest box project, which has been sitting unfinished on the workbench over the winter...

Last season, we found two bumblebee nests, the first under a tiled roof, which survived the season, and a second, in an old badger excavation hole behind the shed.

I thought the 'Badger-hole' nest looked to be a fairly vulnerable, so in true nature 'tooth and claw' I setup my trail camera to see if anyone else also thought so...  Pending something dramatic happening to my roof, the first one stood a much better chance of survival.

and, low and behold, along came a badger:

Our solution for this years' bumblebee queens who I keep evicting from my shed, looks like this:
This is a two chamber design, based on the one in Derek Jones' 'Bird Bee and Bug houses book:

Apparently they like a room for poo and a room for general living.

I have similar preferences :)

Monday, 10 April 2017

An Unexpected Robin Nest

I recently went looking for an old nest box had fallen from its tree.  A bit of poking about in the undergrowth located it... plus a pair of nesting robins, sitting on eggs.

So we have a robin nesting on the ground in a broken old nest box, and quite vulnerable in my opinion.  In an effort to make it a bit more robust I've put some temporary 'anti-cat' protection around it.  I also popped a webcam in (operation of approx 1 minute).  I'm a bit concerned the nest is still fairly vulnerable, but at least I've warned the kids off that bit of the garden, explaining that to the dog was a bit trickier. Some more robust anti-cat measures will go up over the Bank Holiday weekend... any way here she/he is:

Fluffed-up Robin incubating eggs

click --> Follow this link for live Stream <-- click

... live stream was stopped offline when they fledged :)


20/04/17: HATCHED!
Setup a live youtube live stream, and will live stream video in the daytime.
At least 2 eggs hatched today, I think 1 last night and 2 in the day.  There are at least three chicks being fed regularly.  Two videos uploaded today:
1) Adult robin eating eggshell and discarding another piece
2) First glimpse of chicks, a few hours after hatching

22/04/17: 2 days old
Only one parent is feeding, while the other sits on the chicks.
1) Video of some sort of grub being fed

29/04/17: 9 days old
We definitely have three chicks.  I came across this interesting RSPB information page on the nesting habits of robins.  Three would seem to be a relatively small clutch size, but our lot seem to have read the manual since eyes started opening on about day 5.  All have their eyes open now (when they're not sleeping).  Until yesterday, one parents sat on the chicks with the other one feeding, but that has stopped now, presumably they're both busy collecting food.  Since the parents dash in and out to feed, its difficult to see what they're feeding, but we've definitely graduated from spiders to big fate white things (cant tell what they are).
1) Day 7 feeding
2) Day 9 feeding, and removal of faecal sac

03/05/17: 13 days old
Looking a bit more like proper birds now.  Are starting to take an interest in their surroundings, eg picking off insects that wander too close to the nest.  Adults feeding approx every 4 minutes.

Day 8
Day 9
Day 13

The dramatic end to this nest is described in this post

How this was setup....
My rough-and ready solution to documenting this is a LifeCam studio webcam (higher res/better low light than its LifeCam Cinema sibling).  This camera has up to now been monitoring the rabbit shed, so it was simple to move it.  Handily, the nest box is adjacent to the shed with power and network access (who dosen't?).  A quick network cable run to a 'stand-by' raspberry Pi + PoE splitter meant that I could house the bulk away from 'Robbie' the Robin (name was not my choice), to minimise any disturbance.  Video capture is using iCatcher console CCTV software

Robin nesting in old,. broken nest box
The Robin box is behind the upturned pot (enlarged on inset).  Electronics are housed in the box attached to the pergola (another re-purposed bird box).  I've done a bit of waterproofing with electric insulation tape to protect the webcam .  The original entrance hole is facing to the right (not used), with robin access through some other animal-caused damage to the lid region.

SO, fingers crossed, with a bit of help this family will make it through.  Of concern is all the cats, badgers, rats and mice that I've filmed on my trail camera in this exact spot...

Techy bits
Motion jpeg streaming from a compatible usb webcam guide here via a raspberry pi.
This uses an original Type B raspberry Pi.  I had to fix the focus as it wants to focus just behind the bird's head, which isn't very helpful

Code to fix the focus on Raspberry Pi.. Enter the following in a terminal window
sudo apt-get install uvcdynctrl # install package to control USB webcam
uvcdynctrl -v -d video0 --set='Focus, Auto' 0 # turn autofocus OFF
uvcdynctrl -v -d video0 --set='Focus (absolute)' 35 # fixed focus position
...more on controlling auto focus of usb webcams here

The Pi gets its power via power over ethernet (PoE) to my house (via shed).  Video stream is captured at iCatcher console on a PC as follows: On new camera setup, select 'Network Device', and enter 'Source' as follows, replacing with the IP address and port of the Raspberry pi in the custom feeds dialogue box:


I get approx 10fps at 1280x1080, which isn't bad and makes for some some nice screen grabs. 20 fps at 800x600 is also possible, but I've opted for higher res, lower framerate.

So why not use a Raspberry pi camera module?  Several reasons...

  1. This was quick to do with minimal disturbance as I could locate the Pi far away from the nest.  Rasp Pi camera cables are not very long, and more fragile than a standard USB cable.. I can access the Raspberry Pi + its power gubbins without going too near the nest.
  2. The low light performance of the PiCamera is AWFUL.  I have recently installed one of the new v2 camera modules in a custom designed box with its own lighting rig (incidentally is just above this old broken, occupied box!), and have struggled with illumination.
  3. Pi Camera module is not robust, and likes to fry itself it you handle it without taking extreme care to earth yourself first.
raspI camera has its plus points, and is good in an enclosed, pre-planned situation.  Its also cheaper than this webcam (off the shelf.. this webcam was an ebay bargain)

So fingers crossed we make it to the weekend and I can properly fence it off, in the meantime we're watching with interest...

Post Easter weekend...
While not especially pretty, the robins don't seem to mind this attempt to put off the local fox/cat/badger population

Livestream setup
I'm running ffmpeg on a second Pi which takes the video stream from the Pi attached to the pergola in the pic above, adds in a dummy audio stream to play nicely with YouTube, flips the image (camera is upside down), and reduces the width to 720 (scaled) and streams in appropriate format to YouTube.  I've anonymised it by removing its local IP address and my YouTube streaming ID.  The second Pi wasn't doing much anyway, and lives in a currently vacant birdbox.

I'll expand on this in a bit more detail in a separate post, but the command is as follows:

ffmpeg -f mjpeg -i 'http://XXX.XXX.X.XX:8080/?action=stream' -an -vcodec h264 -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -acodec aac -ab 128k -g 50 -vf 'scale=720:-1, vflip,hflip' -strict experimental -f flv  rtmp://

Friday, 7 April 2017

2017 Double camera Bird Box - Intro

Our 2017 double-camera bird nestbox went up a couple of weeks ago.  This is a two-camera setup built around a conventional wooden box design... with a Raspberry Pi 3 for a brain.  The box gives a day and night (infra-red / IR) video options (e.g. to check for roosting at night), with separate 'from above' IR and 'from the side' daylight cameras.  It has a temperature, humidity and ambient light sensors, the latter to automate interior illumination with either visible light or IR leds, all of which have high and low settings.  It's connected directly to the local network so all video storage is handled off-box (as it were).  All we need now is some birds...
2017 double camera bird box

Brain: RaspberryPi 3.  Most of the electronics gizmos interface via a female to male header + perfboard 'shield' that plugs into the GPIO header pins of the raspberry pi.
Power: This is via Power-overEthernet (PoE) using this TP-LinkPoE kit (~£20).  This allows one cable for power and network access, and provides a 12v feed for the LED illumination and IR-cut, which is also dropped to 5v for the Raspberry Pi using a RECOMR-7885.0-1.5 module (~£10)
Top-down camera (Cam -1): InfraRed Raspberry Pi v2 camera module (~£22.79).  This does higher resolution, fixed focus motion-activated video and image capture, using the excellent PikrellCam software.  Video is saved direct to my local network to overcome local storage issues.
InfraRed cut-out filter:  This switches a filter in front of Cam- ( the Pi v2 IR camera), in the daytime for normal-looking daytime images a and video.  In low light/dark, the IR filter is pulled back and IR led array activated for unobtrusive night viewing.  this is interfaced to the Pi's GPIOs via an L2930NE ic.
Side-view camera (Cam-2): MicrosoftLifeCam Cinema (~£15 ebay).  This is the 'dumb-camera', and just generates a video stream that is monitored by a windows PC running iCatcher CCTV software.
Illumination: There are 3 lighting circuits, (1)3x 'White' leds recycled from a broken torch, (2)4x  'Warm' leds (from the 'drawer of stuff'), (3) an 36 LED IR led.  The latter is coupled to a 5k variable resistor as full power causes a white-out.  These three lighting options work off the 12v feed and have low and high settings.  The 12V LED circuits are switched by the Pi's GPIO pins via a ULN2003AN ic.  I used some different types of LEDs since I didn't know what would work the best ultimately.
IR array
Temperature and Humidity: HDC1008  I think these are now discontinued, but as this was 'in the drawer of stuff', in it went.   This sits in a separate, vented compartment and logs outside temperature and humidity to a MySQL database.

Entrance hole counter: The box has a 25mm entrance hole, with in outer and inner IR detector beam, as such we can distinguish between entering/exiting/head pop in/head pop out.
Other stuff:  I've also added inside and outside PIRs as an alternative way of measuring activity - but haven't worked this bit up yet...

It's built in a modular fusion so that it can be relatively easily dismantled.  The circuitry for the entrance counter is housed separately, into a compartment in the front panel of the box, and connected via a hacked cat5 cable and jack to the Pi shield.

Some pictures...
Easy access side door.
Showing position of side camera and opaque windows for illumination

The main box area is under the wiring bit, with glass partition separating webcam section
Raspberry Pi Shield in detail

One of two vents for the HDC1008 temp and humidity sensor

In situ

As I develop more of the software side of things I'll post updates at intervals.