Blue Tits Timelapse from 2023 on YouTube

Last year I setup a RPi Zero with a NoIR camera to film inside a bird box in the garden. Whilst I managed to get some footage of eggs and the chicks fledging, this was only after the event. For some reason the WiFi connection dropped meaning I couldn’t remotely connect to the device to see if it was working. This weekend I set up a version 2 with a RPi 3B.

For the hardware I used a plywood bird box made about 5 years ago which seems to have attracted Blue Tits most years.

bird box with camera

I used a RPi 3B+ with Pi NoIR camera and an IR LED ring powered over 5V from the Pi and controlled via a GPIO pin.

Pi NoIR camera box

Pi NoIR led ring connections

I wanted to create a really simple timelapse plus be able to easily see the latest image from inside the box. The script below is a Bash script that takes an image, stores it in a timelapse archive folder and creates a webpage to show the latest image. This script is run through a crontab every 10 minutes.

Fingers crossed for another family this year.

#!/bin/bash

# first off turn on the IR LEDs'
# used wiring at https://www.lisiparoi.co.uk/img/htu/ConnectoGPIO.png
# Exports pin to userspace
sudo echo "4" > /sys/class/gpio/export  
# Sets pin 4 as an output
sudo echo "out" > /sys/class/gpio/gpio4/direction
# Sets pin 4 to high
sudo echo "1" > /sys/class/gpio/gpio4/value
# wait a couple of seconds for lights to settle
sleep 2

DATE=$(date +"%Y-%m-%d_%H%M")
libcamera-still --width 1920 --height 1080 -o /home/pi/timelapse/$DATE.jpg

# Sets pin 4 to low
echo "0" > /sys/class/gpio/gpio4/value 

sudo cp "/home/pi/timelapse/$DATE.jpg" "/var/www/html/birdbox.jpg"

# create folder on rpi to hold timelapse images
# sudo chmod 777 camera.sh
# then add this to the crontab
# */1 * * * * sh /home/pi/camera.sh

# raw image 3280x2464 - once per minute at 1.5MiB is about 2.1GiB a day - which is a lot on a 64GB card - 25 days
# smaller image 1920x1080 - once per minute at 0.4MiB is about 0.6GiB a day 


# Get disk space information
disk_space=$(df -h / | tail -n 1 | awk '{print $4}')
disk_space_perc=$(df -h / | tail -n 1 | awk '{print $5}')
filedate=$(date -d "$(stat -c "%y" /var/www/html/birdbox.jpg)")

# Define the content you want to save as HTML
html_content="<html>
<head>
  <title>Birdbox</title>
</head>
<body>
  <h1>Birdbox</h1>
  <img src="birdbox.jpg" width="100%">
  <p>Image updated: $filedate .</p>
  <p>Available Disk Space: $disk_space (or $disk_space_perc)</p>
</body>
</html>"

# Save the HTML content to a file
echo "$html_content" > index.html
sudo cp "/home/pi/index.html" "/var/www/html/index.html"