Showing posts with label RaspberryPi. Show all posts
Showing posts with label RaspberryPi. Show all posts

Saturday, 2 November 2019

Birdbox camera dashboard now with live weather data

I added a live weather widget to my grafana birdbox camera monitoring dashboard, in case I can't be bothered to look out of the window.

Dark Sky weather data 
I did an earlier post that described getting my camera feeds into this setup here.
I used info on Michael Green's blog post here as inspiration for my setup.

This is the updated version, not a lot going on apart from the wind rattling the windows...

The weather bit is in the top left corner, it pulls weather data from an online weather visualisation app called Dark Sky.  It's added via a short piece of modifiable html code into an iframe panel on the grafana dashboard that runs in a docker container on a Raspberry pi.  It was a bit of a pain to get running , so I'm going to document it in case anyone else wants to do this.
Note that the 'accelerometer' style graphs are populated by a temperature and humidity sensor in one of the bird boxes themselves.

Steps / considerations
I'm running grafana version v6.3.6 in a docker container on a Raspberry pi.  If you go to the grafana website you'll see a darksky plugin that can also be used... I didn't go that route, instead opted for (what I thought) would be simpler, namely adding a short bit of html to a text panel in html mode (see my original post on this) , this proved not to be the case...  The iframe embed method is described in this helpful blog post.

In theory, you just add the iframe html into a new grafana panel and set it to txt/html mode.
It's quite configurable, e.g. the colour of the text and background/font used and temperature units used - I'm in the UK so have set it to degrees celsius.
You'll need to substitute the XXXX and YYYY for your latitude and longitude so that the weather data is relevant to your location.  I sourced my location via this handy postcode to lat/long converter

<iframe id="forecast_embed" frameborder="0" height="245" width="100%" bgColor="transparent" src="//forecast.io/embed/#lat=XXXX&lon=YYYY&units
=uk&color=#ced6cb&text-color=#ced6cb"></iframe>

The fiddly bit.

So in theory its as simple as dropping in a bit of html - no it isnt...
There's a configuration option that needs to be changed, otherwise an ifram wont render, and you just get a string of text.  In the grafana server admin screen, if you scroll down to the [panels] section there is a setting called 'disable_sanitise_html = false', this needs to be changed to ' = true' or your iframe wont load.  You would think that changing it directly here would be possible?  ...but it isn't

Note - I've updated it in my version.  the default is 'false'
How to fix it depends on how you're running garafana.  If you've installed grafana locally, then you just need to directly edit the grafani.ini file, which is located in /etc/grafana/grafana.ini.

In my case I've set it up in a docker container which makes editing the file a bit more tricky- editing it directly does not work either but I'll describe the process as its handy to know how to connect to a running docker container and modify ts contents:

SSH into the Rassberry pi running Docker, and by definition your grafana container
Find the container ID of the grafana container
docker container ls -a

Connect to the container as root
docker exec -it <container-name> bash

To edit the grafana.ini file, you'll need to install a txt editor inside the docker container first
apt-get update
apt-get install nano

Then edit the file in nano, updating the disable_sanitise_html = false to true
nano /etc/grafana/grafana.ini

Save the file, exit the container.

SO.. that should fix it?  WRONG.  The config screen in the web interface as described above will still show this setting as = false.  Loads of forums recommend restarting the docker container, which has not effect either.  Running the command 'service grafana-server restart' within the docker container also has not effect.

The solution..
The trick is to pass 'disable_sanitise_html = true' to a docker image as an environmental variable at the point that a docker image is spun up into a container.  To make this work, I committed my existing grafana container back to an image file (this is essentially making a backup/snapshot of a container),  then re-launched the new local image file with the environmental variable added:

#run newly saved image file with environmental variable set
docker run -d -p 3000:3000 -e GF_PANELS_DISABLE_SANITIZE_HTML=true  grafana:1_Nov

Note that I wanted to keep my modification to the stock docker image for my modified version of grafana.  If you want to apply it to the stock grafana image on dockerhub.com then use this command to spin up a new container:

#or add environmental variable option to the image on docker hub:
docker run -d --name=grafana -p 3000:3000 -e GF_PANELS_DISABLE_SANITIZE_HTML=true grafana/grafana:6.3.6

I've squished the DarkSky iframe down a bit on my dashboard, if you make it bigger within its grafana panel, you get this which has some pretty cool animated effects too



Sunday, 20 October 2019

Birdbox camera dashboard + environmental monitoring

I made a CCTV-type monitoring screen to present multiple birdbox cameras together + environmental monitoring data, using a Grafana / Docker container on a Raspberry pi...

The screenshot below shows all five of my currently active bird boxes.  The foot in the top Left box  belongs to a blue tit that spends quite a lot of time in there the moment (roosts too).  The temperature graph and widgets are populated by a temp + humidity sensor in the bottom R box that logs to its own internal environment every minute - the data really is an in-box box environment monitor.



EDIT Nov 2019: New blog post describing addition of web-sourced weather data to grafana

Prerequisites.. quite a lot of groundwork.  This project relies on various stuff already existing:

(1) Multiple birdbox camera streams
Most of mine use Raspberry Pi ZeroW mini computers + v2 camera modules running motion capture & streaming software called pikrellcam.  The top L one is a motion jpeg stream from a webcam on an early first raspberry pi model that has been going strong since 2014, the camera window could do with a clean though, our early evening roosting blue tit is present in this one:



(2) Some environmental data.
The aim is to present environmental data relevant to to the camera feeds.  The bottom R bird box has a sensor that logs its internal temp & humidity every minute to a mysql database, and is used to populate the temperature plot:

Temperature plot over time, current humidity and temps also showing

Equally there are plenty of free sources of weather data that could be used to provide a feed - it would be fun to see what the weather is locally too.

(3) A 'video dashboard'
The main point of this post.  The dashboard runs on the newest model from the Raspberry pi foundation, the Model 4B (4Gb ram version).  This has faster wired networking, more ram available, and generally more processing ooph.  I would NOT advocate putting one in a bird box unless you're planning on keeping roosting birds warm over the winter.  Mine sits 'headless' (ie with no monitor/keyboard etc) in the lounge behind the TV.  I've set it up with fixed IP address (wired, not wireless), and is accessed via SSH from a laptop.  I'm not going into detail for that as there are countless 'how-to's' out there.  Lets call this machine 'Hub-Pi'.

Hub-Pi setup as follows

Docker
Docker provides a seamless(ish) way of running 'OS-level virtualization software in packages called containers'.  Basically there are a lot of freely available pre-configured software 'images' that allow rapid deployment of software without having to fiddle about with setup.  I've used a Docker image of a dashboard application called Grafana.

Install Docker on Hub-Pi:  I used the guide on this site, up to the bit about 'swarming' (ignore from then).

Docker works by first downloading a specified image.  It then creates a live 'container' from that which is what you 'do stuff with'.  The 'docker run...' command specifies how the resulting container is configured.  If a container is deleted then all configuration data within the container is lost and you need to re-create a container from the original image and loose all your work in the process.  You can 'commit' a container back to a fresh image at any time,  again Google is your friend.

Install Grafana Docker image: Docker images are supposed to run on any hardware running Docker... sort of.  A base image is built with respect to the architecture of the CPU of the machine it runs on.  This basically means that Raspberry Pis are different to PCs (Arm vs  AMD64).  I'm not totally clear on the difference, but an image built for one may not work on the other.  Many images are built for both CPU architecture and Docker intuitively picks the right one to run.  Mostly.

On writing this, the most recent Grafana image is 6.4.3.  The Arm (is for Raspberry pi) version is somehow broken, so I had to revert back to version 6.3.6, which runs fine.  The Docker command to pull down the right Grafana image and create a Docker container that runs it is...


docker run -d --name=grafana -p 3000:3000 grafana/grafana:6.3.6

If you omit the :6.3.6 bit you'll get the current image which may be fixed in a future version, but didi not work for me.

This command is saying:

  1. Pull down the v6.3.6 of the Grafana image from DOckerHub.
  2. Run it (make container) and forward port 3000 to the host computers port 3000.
  3. Assuming the container runs, if you navigate to the Pi's IP address in a web browser that you fixed earlier, using port 3000 (http://HubPiIPaddressHere:3000), if its connected to a screen & keyboard use localhost:3000 directly then you get to the Grafana main login screen where you can assign a new password
Addition of camera feeds to Grafana
To make a camera feed in Grafana, got to..
Add Panel, then choose 'Text' from the visualisation options.
Based on the two types of video feds that I currently have, I edited the text in html mode as shown below.

My network video streams are of two flavours:
(1) Using webcams to generate a motion JPEG (MJPEG) stream, add the following to the html:
<img src="http://XXX.XXX.X.XX:YYY/?action=stream">
where XXX.XXX.X.XX is the IP address of the remote source and YYYY is the port it streams from

(2) Using Raspberry Pi v2 camera modules and pikrellcam software, add the following to the html:
<a href="http://XXX.XXX.X.XX" target="_blank"/a>
<img src="http://XXX.XXX.X.XX/mjpeg_stream.php"">
Where XXX.XXX.X.XX -  the IP address of the remote Pi running pikrellcam.  This also adds a convenient hyperlink to the video in the dashboard to the main pikrellcam page for that camera where motion captured video can be reviewed.

Once a video feed is added you can manually drag it about and resize it to fit your screen. Simple.

Potential developments...
This is an early version.  I plan to add in local weather data, e.g. from https://www.wunderground.com/.  I have entrance hole activity counter on one of my boxes (soon three), so it would be good to see a plot of activity associated with each video feed too.

UPDATE Nov 2019: see a new post which discribes the addition of a live weather feed
https://nestboxtech.blogspot.com/2019/11/birdbox-camera-dashboard-now-with-live.html



Saturday, 15 June 2019

How to make a mini bird box monitor

It's all very well having a bird box camera that you can view on a PC/ipad/etc, I wanted an unobtrusive static monitor, so I made one

My little Elf Helpers demonstrating their bird box monitor

Some prerequisites....

  1. Bird box camera live streaming to your local network that can be viewed via a web-browser on a PC / ipad / other tablet computer.
  2. In my case I use Pikrellcam software running on a Raspberry Pi-equipped bird box with raspberry pi camera module (see a recent blog post on the nest that is showing on this ).
  3. In the picture above I've used a HyperPixel 4.0 screen from Pimoroni, which is plugged into a Raspberry Pi 3 Model A+, in a case to protect it.  Pimoroni have comprehensive setup instructions.

My normal way of monitoring this box on a PC using pikrellcam looks like this...


To get a minimal interface, you need to switch the Raspberry to 'kiosk mode'.  This means that when it boots up, it loads a specific website and removes extraneous stuff from the active window such as mouse cursors, scroll bars and distracting error messages.  This is the same approach that you get in applications such as photo booths, cash dispensers etc.

I modified a method from the  https://pimylifeup.com/raspberry-pi-kiosk/ website.  There are probably other ways of achieving the same end.  I'm also assuming that you're familiar with entering commands via SSH.  Google it if you don't know how.

Brief summary...

Make a file called kiosk.sh
nano /home/pi/kiosk.sh

Install dependencies
sudo apt-get install xdotool unclutter sed

Add this to the kiosk.sh file.  Substitute 'http://xxxurlToYourWebCam' with your local URL
#!/bin/bash

xset s noblank
xset s off
xset -dpms

unclutter -idle 0.5 -root &

sed -i 's/"exited_cleanly":false/"exited_cleanly":true/' /home/pi/.config/chromium/Default/Preferences
sed -i 's/"exit_type":"Crashed"/"exit_type":"Normal"/' /home/pi/.config/chromium/Default/Preferences

/usr/bin/chromium-browser --noerrdialogs --disable-infobars --kiosk http://xxxurlToYourWebCam  &

Interestingly if you're doing his setup over SSH, if you run sh kiosk.sh from an SSH terminal window on another machine the kiosk wont start.  Probably as the active SSH session cant find a screen.  If you run it directly on the Pi with connected keyboard it works fine

Most of the following is copied verbatim from 'how to' the link above, with a couple of changes

Run script from command line using service file
sudo nano /lib/systemd/system/kiosk.service

Add the following text to this file:
Note that using the hyperpixel, this only works if 'Type=simple' is changed to 'Type=forking' (as suggested in the commentary at the end of the above article)
[Unit]
Description=Chromium Kiosk
Wants=graphical.target
After=graphical.target

[Service]
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/pi/.Xauthority
Type=forking
ExecStart=/bin/bash /home/pi/kiosk.sh
Restart=on-abort
User=pi
Group=pi

[Install]
WantedBy=graphical.target

Enable, then start the service
sudo systemctl enable kiosk.service
sudo systemctl start kiosk.service

Check status like this
sudo systemctl status kiosk.service

There's loads more detail on https://pimylifeup.com/raspberry-pi-kiosk/ but I've just listed the modified version I did to make it work.

One more thing....

There's some sort of bug/security procedure combo affecting the chrome web browser boot up time, such that it wont load chrome browser until it generate sufficient 'Entropy' in particular when there is no keyboard connected.  Seriously.  I think this has something to do with random number generation & cryptography and reflects how 'secure' the machine is.  Or something.

Anyway, the effect is that our kiosk wont load for several minutes after boot, which is a pain especially if you need to do a quick demo.

There's a solution detailed here:
https://www.raspberrypi.org/forums/viewtopic.php?t=237069
I'm not vouching for the relative safety or what it does (use at your own risk etc...)

sudo apt-get install haveged
sudo systemctl enable haveged
sudo systemctl start haveged

After all that I have a stand-alone Pi with screen that boots directly to my bird boxes webpage :)



Sunday, 27 January 2019

LED backlights for birdbox illumination

All my birdboxes have Infrared (IR) and white light illumination, using 5mm LEDs.  The IR ones that I use give nice diffuse lighting, however the  white light ones tend to give a 'spotlight' effect.  I spotted these LED backlights recently from Pimoroni and once installed, they give a nice diffuse 'daylight' effect:

Underside of roof camera insert.  The circular black thing is an IR-cut module.

This year's new small bird boxes are equipped with two each.  They are wired in parallel to 5V with a 50 ohm resistor for each one, as well as another 50/100/200 ohm resistors between the ground and the uln2003 i/c transistor that is used to switch them for low/med/and blinding light levels.  Default is medium.

This picture shows the underside of the roof 'insert' for this small birdbox.  To the top and bottom are 5 IR LEDs in total.  These are wired in parallel, the only resistors are between the ground and the uln2003 (same arrangement as above there).  100 ohm seems the sweet spot.


The IR LED setting is still a bit spotty for my liking.. but I haven't found an IR led panel.... 
Last year's version of this used 4 IR LEDs, on in each corner, leaving a dim bit in the middle... which is exactly where all the action is... I fixed this this time by adding a fifth LED that points a the middle specifically.  The camera is rotated by 90 degrees to include the entrance hole in the image as well.  A 3mm green led indicator is added via one of the GPIOs (+10k ohm resistor) so that if I cant connect to it, it's obvious from the ground that there is/isn't power.



The diffuse white LED is an improvement on a previous version of this that uses 5mm white LEDs:

Nasty spotlighting in a previous version of the 2019 build, will be re-made with LED panel...

This is the business side of things:


Apart form the modifications described above, it's exactly the same setup as last season's box: Raspberry pi ZeroW (wifi), 8 MPixel v2 IR camera, IR-cut module. 5mm IR LEDs and White led panel (updated).  Illumination has high, medium and low (mood lighting) options.  Power is via 12v to 5v step-down. 

Video capture uses pikrellcam.  Video / image captures are stored locally, and can be archived off to a separate PC via wifi.  Local video storage overwrites if space drops under 20%.  This version uses the 'Lite' version of Raspian, so there is 13Gb available on the 16Gb micro SD card.  Based on last year's nesting activity that is good for approx 1 day of before a review is needed.  The prototyping board sitting on top of the Raspberry pi zero W is wired as described in this post (minus the rat-nest of wires for the LEDs...)

Sound...
I wanted to add a microphone this year, but had real trouble getting a USB microphone adaptor working without noise interference.  The semicircular thing poking out the micro USB socket is a microphone, but its a fairly poor one.  I will probably not turn it on in practice... ironically a usb webcam I had spare gave flawless audio, but there was no way to fit it in and it seemed a bit silly to use an additional webcam as a microphone anyway...

Friday, 11 May 2018

2018 Birch log blue tits, Nest building, egg laying and incubation days 1-13

13 days in from the start of blue tit nest building, we're up to 8 eggs.  Three eggs were laid on day 6 (ouch) then approximately one egg per day.  She started incubating in the last couple of days.

Video and images captured using PikrellCam on a RaspberryPi Zero W.  This box's build is described here.

This is a slide show of nest building and egg laying using a mixture of IR and visible light.  The pinkish cast on the visible light is due to the lack on an IR-cut filter on the camera... I'll add one for next year.



Having looked at other blue tits nests, it seems that green tennis ball fluff is quite the 'in thing'.  In this one is also rabbit hair, sheep wool, straw, moss and feathers from my neighbour's chickens...

1st eggs laid on day 6

Day 9, egg in the sunshine, infra-red illuminated

Day 13, 8 eggs, incubating most of the time now

Some video clips

Female being fed on the nest while laying (not incubating in earnest yet)

This one with the lights on

All seems to be going well, other what is probably a tick above her right eye.  Goes with the territory in these parts...

Part 2 of this series is here, unfortunately it doesn't end well.... and the tick does not help...

Sunday, 22 April 2018

Wi-fi Small Bird, 'Birch Log' Nest Box

This birdbox is a more compact version of my previous design, but basically the same kit used as in the as the robin box described previously: 8 mega-pixel digital camera, wifi IP connection, night-vision or visible light options with added illumination, and motion activated recording facility.

Update 01/01/19: Winter maintenance = addition of Infra-red IR cut here
Update 06/06/18: Chicks and a sticky end here
Update 11/05/18: Nest building, and egg laying here


This time I've equipped a cheap commercial nest box box, rather than make my own as I liked the 'natural' look of it. This box is supplied by CJ Wildlife, it's basically a hollowed out birch log with a 32mm hole at the front, which should be good for Blue Tits and Great Tits.  Total build cost is in the region of £70.

TamperProof.  Holds off any Woodpecker armed with a philips head screwdriver

My main problem with this box is that the  lid and the back baton are attached with 'TamperProof' Torx head screws.  I cant see the point since good old philips-head screws would have done just as well.  If you're going to buy this box, make sure you spend the equivalent cost on a set of appropriate screwdriver bits....  I also replaced the back batton to give an attachment point beneath in addition to the one at the top.

There is enough of a gap between the roof and the entrance hole to squeeze in the this little fella, which fits in snugly under the lid

Lid insert with Raspberry Pi Zero-W, 8MP IR camera and 4x IR and 4x White LEDs

Birch log box with camera insert
I made two changes with this design over the Robin box version of this

Top and bottom view

LEDs are added via 2x perfboards on the underside, instead of fiddly, messy wiring on the top. The 12v to 5v conversion is done via a recom switching regulator (R-78B5.0-1.5).  I had originally planned to supply this box with a 5v feed but this didnt work well, and had to supply at a higher voltage (12v) and then drop it.  The switching regulator component alone is an equivalent cost of the Raspberry Pi Zero-W, so I've used it a reusable way rather than soldering it directly in to the stripboard.

Bodge-tastic use of PCB terminal blocks...

As previously, 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 finished product


Some inside views

Top: Infrared mode, Bottom: Visible light mode
The visible light option suffers from the lack of an IR cut filter, but one can be added once nesting season is over...

and in-situ



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).
So, this didn't work well.  I ended up dropping the 48v feed to 12v, an splitting this out to a 'junction box' at the bottom of the tree as I didnt fancy climbing a tree each time I wanted to  connect/disconnect a device....  12v feeds are ...

1) Recombined with ethernet for wired, PoE and sent to the 'WAP-box' which is up a tree.  This a based around a wired ethernet Raspberry pi 2B with wifi dongle.  The 12 v PoE feed powers the Raspberry Pi another TP-link PoE splitter with voltage dropped to 5v   The power cable was hacked from micro USB cable (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.



2) The additional WiFi 'Satellite NestBoxes':
Birch log box small tit box described here
Robin box described here

These take a 5v 12v 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 have a couple of IR and visible LEDs to for illumination. 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 didnt work well due to lag / poor network connection.  In the end I ended up writing video locally to the PiZero's SD cards, and archiving off daily to the local network, which can all be automated.

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

Update:
Satellite box 1, 'Robin Box' described in this post
Satellite box 2 'Birch log box' described in this post

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 ambient_lightMonitor.py.  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
sudo apt-get install python-pip  #if pip not available, e.g. on Raspbian Lite
sudo pip install ephem
Note, the sudo bit is important otherwise if you attempt to run a python script from rc.local  containing this module (to run it at boot) the root user wont be able to find the module since without 'sudo...' it will install ephem under the user 'pi'

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 light_dark.py 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 weblamp.py 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__)

#http://mattrichardson.com/Raspberry-Pi-Flask/

GPIO.setmode(GPIO.BCM)

# 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)

@app.route("/")
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:
@app.route("/<changePin>/<action>")
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__":
   app.run(host='0.0.0.0', port=90, debug=True)

Make it executable as follows:
chmod 755 weblamp.py

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

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.

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
Cost
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...
Small=beautiful
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...