Introduction: YouTube Streaming Bird Box

About: I like to work with wood, electronics and sometime mechanics.

This Instructable shows my approach on getting a live YouTube video stream from a bird box outside our house.

To achieve this I used a Raspberry Pi nano W microcomputer and night vision camera. I swapped the original 3.6mm lens with a 2.1mm lens to get a bit wider field of view inside the bird box. If I'd use the original lens, the bird box had to be quite tall, and I didn't want that. This hardware is then mounted to a custom 3D printed mounting plate which againg is mounted inside a water tight electronics box with a transparent lid.

The Bird box it self is designed by me and fits birds like blue tits

Supplies

Raspberry Pi zero w
8GB Micro SD card (or bigger)
ZeroCam Night Vision
2.1mm M12 CCTV lens without IR filter
100x68x50mm Waterproof Electronics Box w. transparent lid
Custom 3D printed mounting plate (alternatively use padded double sided tape)
PG7 Cable Gland
5m USB cable (for power)
2m 148x19mm wood plank
Screws or nails

Step 1: Assembeling the Hardware

To securely mount the components inside the box I am using a 3D printed a mounting plate. There is an .STL file attached which you can use to 3D print. I use some small 2mm wood screws to mount the camera, IR lights and Raspberry Pi to the mounting plate. (first removing the screws connecting the IR lights to the camera board). You probably have to drill the holes to correct size on the 3D printed part.

The flat cable connecting the camera to the Pi should be included in the camera package. If you buy an alternative camera you might also need to buy the cable that fits the Raspberry Pi nano W. (The full size Raspberry Pi has a wider cable connection socket)

Then drill the hole for the cable gland on the center of one of the long sides of the box. Drill it as high as possible to avoid crashing the inner nut with the Pi. Test fit the mounting plate with the electronics and also the transparent lid before drilling.

Cut of the female plug of the USB cable and remove a few centimeters of the outer shell and shielding. Most likely there are one black and one read wire inside. These should be GND (0v) and +5v. Strip those two and measure the voltage using a multimeter to be 100% sure. Put the cable through the cable gland and solder the red and black cable to the Raspberry Pi pin 4 (red, +5v) and 6 (black, GND)

Then I mount the plate to the box using two 3mm screws and tighten the cable gland. Just leave the lid off now, because we have to put in the SD card, adjust the focus of the lens and intensity of the IR leds.

If you don't have access to a 3D printer you can simply mount the Pi and camera using padded double sided tape. It might be a good idea to make some spacers to raise the camera to get the lens as close as possible to the lid to prevent the side of the box distorting the image.

Step 2: Prepare the YouTube Stream

To be able to stream to YouTube you have to:

Create a YouTube channel
Enable live streaming
Create a live stream using streaming software

On the last step you will get a streaming key. This key is to be used in the next step.

Step 3: Installing the Software

Raspberry Pi's uses a Linux operating system called Raspbian which has to be installed on the SD card.

To install Raspbian follow this tutorial:

https://www.raspberrypi.org/documentation/installa...

Before putting the SD card into the Raspberry Pi, follow this tutorial to tell it which WiFi to connect to.

https://www.raspberrypi.org/documentation/configur...

In addition to the wpa_supplicant.conf file that you put on the SD card, create and upload an empty file called SSH to the same location. This will enable SSH when you boot the Raspberry Pi which we need to be able to connect to the Pi from our PC/Mac

There are also two more files we want to add to the SD card at this stage. Create a folder called files and put the following files there:

stream_youtube.sh with the following content: (Swap YOUR_STREAMING_KEY with the streaming key from step 2)

#!/bin/bash
WIDTH=1280
HEIGHT=720
FPS=25
BITRATE=6000000
YOUTUBE_KEY=YOUR_STREAMING_KEY
LOGLEVEL=1

echo "Starting stream ..."
raspivid -t 0 -w $WIDTH -h $HEIGHT -fps $FPS -b $BITRATE -awb greyworld -o - | \
  ffmpeg -nostats -loglevel $LOGLEVEL\
  -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le  -i /dev/zero \
  -i - -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental \
  -f flv rtmp://a.rtmp.youtube.com/live2/$YOUTUBE_KEY
echo "Stopping stream ..."

Note: raspivid has been removed in Rasperry Pi OS from Bullseye onwards. You can use libcamera-vid in stead. The code will look like this:

...
libcamera-vid -t 0 --width $WIDTH --height $HEIGHT --framerate $FPS --bitrate $BITRATE --inline 1 -o - | \
...

And stream_youtube.service with this content:

[Unit]
Description=YouTube streaming service

[Service]
User=root
WorkingDirectory=/root
ExecStart=/root/stream_youtube.sh
Restart=always

[Install]
WantedBy=multi-user.target

Now, insert the SD card in the Pi and plug in the USB cable to power it.

Wait for some minutes for it to boot and find the IP address it has gotten from your WiFi router using the user interface of the WiFi router.

If you are on Windows, download PuTTY

Now open PuTTY on windows and connect to the IP, or use the terminal on mac/linux (ssh pi@[ip address])

The default username is pi and password is raspberry

Now we are going to enable the camera and set up how the Pi boots (disabeling graphical user interface)

Type the followingin the terminal/PuTTY window:

sudo raspi-conig

Go to: System options enter Boot / Auto login enter Console enter

Then go to: Interface optionsenterCamera enter Yes enter OK enter

It is recommended to change the password. Go to System options enter Password enter OK enter

Now we are going to move the two streaming files we put on the SD card earlier to its correct folders and making them runnable: (type all the commands terminal. Lines with leading # are comments)

# become root (administrator user)
sudo su

# Fix the line endings if the files were created in windows
cd /boot/files
sed -i -e 's/\r$//' stream_youtube.sh
sed -i -e 's/\r$//' stream_youtube.service

# Move streaming-file for YouTube
cd /root
mv /boot/files/stream_youtube.sh .
chmod 755 stream_youtube.sh

# Move service file
cd /etc/systemd/system 
mv /boot/files/stream_youtube.service .

# Reload service daemon to include the new service
systemctl daemon-reload

# Enable the service / make it autostart at next boot
systemctl enable stream_youtube.service

# Reboot the Pi
reboot

The Raspberry Pi will now restart and the stream will start when it has booted. The YouTube stream should be ready to ready to recieve the stream from the Pi at this stage. Have the YouTube Studio window open showing the stream and metadata.

You should see a red LED on the camera board light up when the camera is active.

To check if the stream service is started from the console you can use the following command

top

This will show a list of running processes. You should se ffmpeg on top using about 30% CPU

To control the streaming service on the Pi use the following commands:

# Enable the service / make it autostart at next boot
systemctl enable stream_youtube.service

# Disable the service / make it not autostart at next boot
systemctl disble stream_youtube.service

# Start the service now
systemctl start stream_youtube.service

# Stop the service now
systemctl stop stream_youtube.service

# Check the status of the service
systemctl status stream_youtube.service

If you need to change the content of any file use the nano editor. (ctrl+x to exit)

sudo nano filename

Step 4: Building the Bird Box

The bird box can of course be built in many different ways, but here is my take on it. The hole in my box is 30mm. The size dictates which birds will use it so choose a hole size that matches the birds in your area.

I have used pressure treated 148x19mm wood which is screwed together.

The camera box is mounted in the ceiling and the distance from the camera lens to the bottom of the box is just under 20cm.

The camera box is fixed to the ceiling use four 3-4cm cuts of wind bracing strap or similar. To be able to fix the wind bracing strap pieces using wood screws into the box, just fill the holes on the backside with hot glue and screw into the glue. Turn the wind bracing strap inwards and bend them slightly out.

There might be a collision between the cable gland and the ceiling. I removed some wood using a chainsaw. I also used the chainsaw to make a notch for the cable through the side wall.

At this stage you will need to start the stream to be able to adjust the focus of the lens and decide the position/angle of the camera box. Adjust the focus roughly. Experiment with the angle of the camera to cover the entire bottom of the bird box. when you are happy with the coverage screw the wind bracing strap to the ceiling. Try to tuck it as far in between the box and the ceiling as possible to avoid collisions with the front wall. Now with the camera box fixed to the ceiling fine adjust the focus, and also adjust the sensitivity of the IR lights. There is a tiny potentiometer on either. When you are happy with all adjustments put on the lid of the camera box. Remember the rubber sealing. I found the IR lights to distort the image a bit because of glare in the lid so I made a shade of electrical tape surrounding the lens fixed to the inside of the transparent lid

After putting on the roof and screwing it to the front plate I added some acrylic sealant along the joint between roof and back plate to prevent water leaking in

The bird box is done and can be placed outside! Good luck bird watching!

STEM Contest

Participated in the
STEM Contest