Bee webcam
This project describes how to easily and inexpensively build a webcam for observing bees using a Raspberry Pi and a camera module.

Materialliste
Project description
The Raspberry Pi requires an operating system, which is usually installed on an SD card. Several operating systems are available, such as Raspbian, Pidora, Ubuntu, and a few others. For our bee webcam, we want to use Raspbian, the operating system officially supported by the Raspberry Foundation. Raspbian is based on the Debian Linux distribution and is optimized for the Raspberry Pi hardware.
Install Raspbian on an SD memory card (Windows)
In just a few steps, the Raspbian operating system can be written to an SD card using a Windows PC. However, the operating system isn't simply copied to the SD card; instead, it's written to the memory card using an image burner (e.g., Win32DiskImager).
- Download Win32DiskImager
- Insert the SD card into the card reader and check if it is recognized by Windows Explorer.
- Download the Raspbian operating system image and unpack it with 7-zip. The image must have the extension ".img."
- Run the Win32DiskImager program and select the path and image file in the "Image File" field. In the "Device" field, select the drive letter of the SD memory card.
- Now click the "Write" button to start the writing process. This process may take some time.
Once the process is complete, you can eject the SD card and insert it into the Raspberry Pi's SD slot. The Raspberry Pi can now boot up.
Activate camera
To use the camera module, we first need to enable it in Raspbian. To do this, open the raspi-config configuration tool.
$ sudo raspi-config
Select menu item 5 Interfacing Options and in the following screen P1 Camera is set to "enable".
Preparatory work for the livestream
Before we install the driver for the camera module and the Motion tool for the stream, we first update Raspbian.
$ sudo apt-get update
$ sudo apt-get upgrade
Install V4L (Video4Linux) driver for the camera module
The V4L driver is already included in Raspbian and can be easily installed using the following command:
$ sudo modprobe bcm2835-v4l2
If the command has run through without an error message, you can now check whether the camera module has been recognized:
$ ls /dev/video*
The output must list /dev/video0.
Motion Installation
The free Video4Linux recording program Motion includes a web server that can stream a live stream from the camera module to the network. To transmit the video stream over a secure connection (TLS - Transport Layer Security or https), Motion version 4.2 or higher is required. The most recent version of Motion is 4.3.2 (pi_buster_motion_4.3.2-1_armhf.deb) and can be downloaded here.
$ sudo apt install ./pi_buster_motion_4.3.2-1_armhf.deb
With apt all necessary dependencies are installed, which may take a little longer.
Motion Configuration
In order to configure Motion correctly, we first take a closer look at the camera details:
$ v4l2-ctl -V
Video Capture Format:
Width/Height : 640/480
Pixel Format : 'YUYV' (YUYV 4:2:2)
Field : None
Bytes per Line: 1280
Size Image : 614400
Colorspace : SMPTE 170M
Now let’s edit the Motion configuration file:
$ sudo nano /etc/motion/motion.conf
To activate the live stream of our camera the following changes must be made:
daemon off -> daemon on
target_dir /tmp/motion -> target_dir/home/pi/Monitor
stream_localhost on ->stream_localhost off
To improve the image quality and frame rate, the following settings can be changed:
width 640 Breite des Videos (Pixel)
height 480 Höhe des Videos (Pixel)
framerate 24 Empfangene Bilderrate pro Sekunde
threshold 5000 Empfindlichkeit der Bewegungserkennung (Anzahl Pixel)
ffmpeg_output_movies off Keine Videos speichern bei erkannter Bewegung
output_pictures off Keine Bilder speichern bei erkannter Bewegung
stream_port 8081 Port über den der Livestream erreichbar ist
stream_quality 75 Bildqualität in Prozent (Livestream)
stream_motion on Ohne Bewegung nur 1 Bilder pro Sekunde (Livestream)
stream_maxrate 24 Max. Bilder pro Sekunde (Livestream)
The changes are saved in the nano editor with ctrl-o.
Now we need to create the folder /home/pi/Monitor and assign the corresponding read/write permissions:
$ sudo mkdir /home/pi/Monitor
$ sudo chgrp motion /home/pi/Monitor
$ sudo chmod g+rwx /home/pi/Monitor
Now we can start the service:
$ sudo service motion start
Test the bee webcam livestream
We can now easily test the camera's live stream in a browser (Chromium, Firefox, Chrome, etc.). Enter the corresponding IP address and port into the browser window: http://192.168.178.39:8081.
A secure transmission using Hypertext Transfer Protocol Secure (https)
Since I wanted to integrate the live stream into my website (https), it's necessary to also transmit the video stream over https. I therefore need to create certificates for encrypted transmission and enable TLS streaming in Motion. To create self-signed certificates on the Raspberry Pi, openssl must first be installed.
$ sudo apt-get install openssl
Now we switch to the Motion configuration folder and create the certificate:
$ cd /etc/motion
$ openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout motion.key -outform pem -out motion.pem -subj "/CN=*.com"
The following entries must now be added to the Motion configuration file:
$ sudo nano /etc/motion/motion.conf
stream_tls on
webcontrol_tls on
webcontrol_key /etc/motion/motion.key
webcontrol_cert /etc/motion/motion.pem
Motion must now be restarted:
$ sudo service motion restart
The following address must now be entered in the browser window: https://IP-Adresse:8081
Make the video stream visible outside the home network
Of course, the bee webcam only makes sense if I can also view the video stream outside of my home network. For this, I still need to make some adjustments to the router. I need to set up port forwarding for the device. I'll demonstrate the settings here using a FritzBox 6890 LTE as an example.
To test the configuration, it is important that I access the video stream from a device that is not connected to the home network!