This project describes how a webcam for observing the bees with a Raspberry Pi and a camera module can be built easily and cheaply.
Installation und Programmierung
The Raspberry Pi requires an operating system that is usually installed on an SD memory card. There are different operating systems to choose from, such as Raspbian, Pidora, Ubuntu and a few more. For our bee webcam we want to use Raspbian, the operating system officially supported by the Raspberry Foundation. Raspbian is based on the Linux distribution Debian and is optimized for the hardware of the Raspberry Pi.
Install Raspbian on an SD memory card (Windows)
The Raspian operating system can be written to the SD card with a Windows PC in just a few steps. The operating system is not simply copied to the SD card, but written to the memory card with an image burner (e.g. Win32DiskImager).
- Download Win32DiskImager
- Insert the SD card in the card reader and check whether it is recognized by Windows Explorer.
- Download the image of the Raspbian operating system and unzip it with 7-zip . The image must have the extension ".img".
- Run the Win32DiskImager program and select the path and the image file in the "Image File" field. Select the drive letter of the SD memory card in the "Device" field.
- Now click on the "Write" button to start the writing process. The process may take a long time.
After the process is complete, you can eject the SD card and insert it into the SD slot of the Raspberry Pi. The Raspberry Pi can now be started.
Activate the camera
So that we can use the camera module, we must first activate it in Raspbian. To do this, we open the raspi-config configuration tool.
$ sudo raspi-config
Select the menu item 5 Interfacing Options and in the following screen P1 Camera is set to " enable" .
Preparatory work for the live stream
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 the V4L driver (Video4Linux) 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 should list / dev / video0.
The free Video4Linux recording program Motion contains a web server that can stream a live stream from the camera module into the network. Motion in a version higher than 4.2 is required so that the video stream can be transmitted via a secure connection (TLS - Transport Layer Security or https). The current 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 the necessary dependencies are installed, which may then take time. a little longer.
So that we can configure Motion correctly, let's 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)
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
In order to improve the image quality and the frame rate, the following settings can also be changed:
Width 640 Width of the video (pixels) height 480 Height of the video (pixels) frame rate 24 Received frame rate per second threshold 5000 Sensitivity of motion detection (number of pixels) ffmpeg_output_movies offNo save videos when movement is detectedoutput_pictures offNo save images when movement is detected Livestream) stream_motion onWithout movement only 1 frames per second (livestream) stream_maxrate 24Max. Frames per second (live stream)
The changes are saved in the nano editor with ctrl-o.
Now we have to create the folder / home / pi / Monitor and assign the appropriate read / write rights:
$ 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 live stream of the bee webcam
We can now easily test the live stream of the camera in a browser (Chromium, Firefox, Chrom, ..). In the browser window we enter the corresponding IP address and the port: http://192.168.178.39:8081.
Secure transfer with Hypertext Transfer Protocol Secure (https)
Since I wanted to integrate the live stream into my website (https), it is necessary to also transmit the video stream with https. So I still have to create certificates for an encrypted transmission and activate the TLS stream in Motion. So that "self-signed certificates" can be created 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
Motion must now be restarted:
$ sudo service motion restart
The following address must now be entered in the browser window: https: // IP address: 8081
Make the video stream visible outside the home network
Of course, the bee webcam only makes sense if I can watch the video stream outside of the home network. Adjustments to the router are now necessary for this. I have to set up port forwarding for the device. I show the settings here using the example of a FritzBox 6890 LTE.
To test the configuration, it is important that I access the video stream from a device that is not connected to the home network!