Have you bought a Raspberry Pi and a ribbon camera like the Arducam, hoping to set it up with MotionEye as discreet CCTV with motion detection? Have you found that your camera needs to be recognised under the MMAL section (or any)? Let's explore what is happening and present a potential fix.
MotionEye and MotionEyeOS are open-source interfaces for CCT and intruder detection. They work on Linux environments like Raspbian and allow for multiple camera connections. Each feed has a basic intrusion detection image recognition algorithm that can trigger alerts and notify you via mobile and email.
You can find more about the project here:
https://github.com/motioneye-project
MotionEye uses the vcgencmd get_camera
to detect the Raspberry Pi camera module, because it is set up with Raspberry Pi's legacy camera stack. Conversely, cameras like the Arducam IMX 519 and many others use the new camera driver stack based on libcamera
and V4L2
.
This post will guide you through how to install the drivers for your new camera, how to test it all works and how to set up an RTMP server to stream your camera feed into MotionEye.
For the rest of this post, I will use a Raspberry Pi Zero 2 W and an Arducam IMX 519 to illustrate the process. It should thus work similarly for any other libcamera
camera.
You can go ahead and flash the Raspberry Pi SD card with Raspberry Pi OS Bullseye or higher. Make sure you have access through SSH and you preferably VNC ( for testing the camera ).
If you would like to purchase a Raspberry Pi Zero 2 W or the Arducam IMX519, here are some affiliate links:
Raspberry Pi Zero 2 W on amazon.co.uk
If are not sure how to do that, I have created a guide that you can use to get started:
You will need to install the appropriate drivers for your camera. Arducam and other manufacturers will provide some quick start guide.
Moreover, Arducam's Quick Start Guide for the IMX519 can be found here:
https://docs.arducam.com/Raspberry-Pi-Camera/Native-camera/Quick-Start-Guide/
Sections 1 to 6 of my guide on setting up an RTSP stream with the Raspberry Pi Zero 2 W and Arducam IMX 519 also apply here:
https://albert-fit.com/how-to-stream-with-raspberry-pi-zero-2-w-and-arducam-imx-519/
The following section covers a condensed version.
Please enable the device tree overlay for the IMX519 sensor ( replace it with your sensor if you use a different camera ).
sudo nano /boot/config.txt
add dtoverlay=imx519 under the line [all]
#Save and reboot
Use wget
to download the bash script to install the necessary packages.
wget -O install_pivariety_pkgs.sh https://github.com/ArduCAM/Arducam-Pivariety-V4L2-Driver/releases/download/install_script/install_pivariety_pkgs.sh
Add execute permissions to the script.
chmod +x install_pivariety_pkgs.sh
Install the kernel driver by calling the script with the -p
argument followed by the kernel driver for your camera.
./install_pivariety_pkgs.sh -p imx519_kernel_driver_low_speed
libcamera
provides the underlying support for the camera hardware. It deals with capturing stills or video and the required camera settings. The libcamera
library also provides the input for our stream.
./install_pivariety_pkgs.sh -p libcamera
./install_pivariety_pkgs.sh -p libcamera_apps
libcamera-hello
allows you to test that the camera driver and output work as they should.
libcamera-hello
If you are running libcamera-hello
in the terminal, you should see a message similar to the one below. You should also see a camera preview if you have a monitor or VNC window.
pizero@pizero:~ $ libcamera-hello
Preview window unavailable
[0:01:11.971410388] [1085] INFO Camera camera_manager.cpp:297 libcamera v0.0.0+4367-ad9428b4
[0:01:12.114477732] [1087] INFO RPI vc4.cpp:444 Registered camera /base/soc/i2c0mux/i2c@1/imx519@1a to Unicam device /dev/media2 and ISP device /dev/media0
[0:01:12.114666065] [1087] INFO RPI pipeline_base.cpp:1101 Using configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
[0:01:12.116348044] [1085] INFO Camera camera.cpp:1033 configuring streams: (0) 2328x1748-YUV420
[0:01:12.117231638] [1087] INFO RPI vc4.cpp:572 Sensor: /base/soc/i2c0mux/i2c@1/imx519@1a - Selected sensor format: 2328x1748-SRGGB10_1X10 - Selected unicam format: 2328x1748-pRAA
#0 (0.00 fps) exp 17750.00 ag 2.00 dg 1.09
#1 (30.00 fps) exp 17750.00 ag 2.00 dg 1.12
#2 (29.99 fps) exp 17750.00 ag 2.00 dg 1.12
...
At the time of writing, the most current installation instruction set can be found here:
https://github.com/motioneye-project/motioneye/wiki/Install-on-Raspbian-Bullseye
You would be best served to check the latest installation from the installation section on Github:
Let's instruct the Advanced Package Tool (apt) to locate the motion packages in its repositories, download and install them, and any dependencies they might have.
apt-get install ffmpeg libmariadb3 libpq5 libmicrohttpd12 -y
Here, you are instructing the system to install the following dependencies:
ffmpeg
A popular multimedia framework capable of decoding, encoding, transcoding, muxing, demuxing, streaming, filtering, and playing almost any type of media (audio and video).
libmariadb3
A client library for MariaDB, a popular database management system, is a MySQL fork. This library allows applications to interact with a MariaDB database.
libpq5
A library used to enable client applications to communicate with a PostgreSQL database server.
libmicrohttpd12
A small C library that makes it easy to run an HTTP server as part of another application.
As an aside, the -y
option automatically answers 'yes' to prompts. By including this, you are telling the system to proceed with the installation without asking for confirmation.
Download the motion package with wget
and use dpkg
to install it (option -i
).
wget https://github.com/Motion-Project/motion/releases/download/release-4.3.2/pi_buster_motion_4.3.2-1_armhf.deb
dpkg -i pi_buster_motion_4.3.2-1_armhf.deb
Disable the motion service and motionEye control for now:
systemctl stop motion
systemctl disable motion
MotionEye still uses Python 2 (an older version of Python). Install python2
and python-dev-is-python2
. The latter is a transitional package that ensures tools expecting python-dev
will work as expected with Python 2.
apt-get install python2 python-dev-is-python2 -y
Install the pip package with pip
, the package manager for Python, and save it as get-pip.py
.
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
pip
.python2 get-pip.py
apt-get install libssl-dev libcurl4-openssl-dev libjpeg-dev zlib1g-dev -y
These libraries deal with network communication, data encryption, image processing and data compression.
bssl-dev
This library is part of OpenSSL, an open-source project that provides a robust, commercial-grade, full-featured toolkit for implementing the Secure Sockets Layer (SSL) and Transport Layer Security (TLS) protocols. The libssl-dev
package contains the development files necessary for building applications that use OpenSSL for SSL/TLS support. It includes headers and other necessary files for compiling applications that require encryption and secure communication capabilities.
libcurl4-openssl-dev
This package is a development version of the libcurl
library, which is used for transferring data with URLs. libcurl
supports many protocols like HTTP, HTTPS, FTP, and more. The libcurl4-openssl-dev
package includes explicitly development files for libcurl
compiled with OpenSSL for SSL/TLS support. This means it's suitable for applications that securely transfer data over networks using these protocols.
libjpeg-dev
The libjpeg-dev
package includes development files for the JPEG library, widely used for handling JPEG image data. The JPEG library is mainly known for compressing and compressing JPEG image files. This package is essential for developers working on applications that need to manipulate JPEG images, including operations like reading, writing, or transforming JPEG files.
zlib1g-dev
This package contains the development files for zlib
, a widely-used library for data compression. zlib
provides functions for compressing and decompressing data using the DEFLATE compression algorithm, which balances speed and compression efficiency. It's commonly utilised in applications requiring data compression and decompression functionalities, such as file archiving tools, network protocols, etc.
motioneye
which will automaticall pull Python dependencies (tornado
, jinja2
, pillow
and pycurl
)pip install motioneye
pillow
installation fails (hangs and ends at 99%), you can install it from official repos using :apt-get install python-pil -y
pip2 install motioneye
.From web development and template rendering with Tornado
and Jina2
to image processing with Pillow
and network operations with PyCURL
:
tornado
Tornado is a Python web framework and asynchronous networking library. It is particularly well-suited for long polling, WebSockets, and other applications that require a long-lived connection to each user. Tornado is known for its high performance and is often used in applications that need to handle a large number of simultaneous connections. Unlike traditional web frameworks that are typically synchronous and handle one request at a time, Tornado can handle many thousands of connections simultaneously, making it ideal for real-time web services.
jinja2
Jinja2 is a modern and designer-friendly templating language for Python, modelled after Django’s templates. It is fast, widely used and secure with the optional sandboxed template execution environment. Jinja2 creates HTML, XML, or other markup formats that are returned to the user via an HTTP response. It allows Python data structures to be transformed into a text-based format, making it a popular choice for web development, particularly in generating dynamic HTML content.
pillow
The pillow is the friendly PIL (Python Imaging Library) fork. PIL was originally a library for opening, manipulating, and saving many different image file formats. However, PIL is no longer maintained. Pillow was developed as a more user-friendly and maintained version of PIL. It is used for image processing, creation, and manipulation tasks, offering extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities.
pycurl
A Python interface to the libcurl library, a free and easy-to-use client-side URL transfer library. PyCURL allows Python applications to use libcurl's capabilities, such as performing and managing HTTP and HTTPS requests, uploading files via FTP, sending emails using SMTP, and more. PyCURL is known for its performance and ease of use for network programming. It is a popular choice for applications that need to interact with different types of web services or perform network operations.
Make a directory in the /etc/motioneye
folder and copy the motioneye.conf.sample
as motioneye.conf
.
mkdir -p /etc/motioneye
cp /usr/local/share/motioneye/extra/motioneye.conf.sample /etc/motioneye/motioneye.conf
create a directory for the media:
mkdir -p /var/lib/motioneye
cp /usr/local/share/motioneye/extra/motioneye.systemd-unit-local /etc/systemd/system/motioneye.service
systemctl daemon-reload
systemctl enable motioneye
systemctl start motioneye
These commands are used to reload the systemd
configuration, set the motioneye
service to start automatically at boot, and start the motioneye
service immediately:
daemon-reload
Reload the systemd
manager configuration. This reloads all unit files and recreates the entire dependency tree. It's often necessary to run this command after making changes to a service file (for example, a file in /etc/systemd/system/
) to ensure systemd
is aware of the changes. The command does not restart any services or units but reloads the configuration files.
enable motioneye
Enable a service named motioneye
. Enabling a service means that it will be started automatically at boot time. This is different from starting a service, which runs it once. By enabling motioneye
, you're ensuring that the service will start every time the system boots up. The motioneye
service is likely related to the MotionEye software, a web frontend for the motion daemon monitoring and controlling camera feeds.
start motioneye
Starts the motioneye
service immediately. Unlike the enable
command, start
doesn't configure the service to start at boot but simply starts it right now. This is useful for initiating services that don't need to run constantly or for testing purposes after configuring or troubleshooting a service.
pip install motioneye --upgrade
systemctl restart motioneye
You can test motion eye by entering the following URL string into your browser:
http://[hostname or ip]:8765
In my case this look something like this:
http://pizero.local:8765
The default username is admin
, and there is no password. It is recommended you change the username and password as soon as possible.
Create a new bash script called stream.sh
.
sudo nano stream.sh
Paste the following script:
#!/bin/bash
libcamera-vid -t 0 --width 1920 --height 1080 --nopreview --exposure long \
--sharpness 1.2 --contrast 1.4 --brightness 0.2 --saturation 1.0 --awb auto --denoise auto \
--rotation 0 --codec libav --libav-format flv -n --framerate 30 -b 3200000 --autofocus-mode auto \
--inline -o "rtmp://127.0.0.1/pi/test"
This libcamera-vid
command is used to capture the video with your Raspberry Pi. We then take and output the video to the RTMP (Real-Time Messaging Protocol) server.
-t 0
Runs the camera indefinitely (0 milliseconds).
--width 1920 --height 1080
Sets the video resolution to 1920x1080 pixels.
--nopreview
Disables the preview window.
--exposure long
Sets a longer exposure time.
--sharpness 1.2 --contrast 1.4 --brightness 0.2 --saturation 1.0
Adjusts image sharpness, contrast, brightness, and saturation.
--awb auto
Sets the automatic white balance mode.
--denoise auto
Enables automatic noise reduction.
--rotation 0
Sets the image rotation to 0 degrees.
--codec libav
Specifies the libav
codec for encoding the video.
--libav-format flv
Sets the format of the video to FLV (Flash Video).
-n
Disables the display of preview window information.
--framerate 30
Captures video at 30 frames per second.
-b 3200000
Sets the bitrate to 3,200,000 bits per second.
--autofocus-mode auto
Enables automatic focusing.
--inline
Includes inline headers in the video stream (useful for streaming).
-o "rtmp://127.0.0.1/pi/test"
Outputs the video to the specified RTMP (Real-Time Messaging Protocol) server URL.
Now you have to enable the script file to be executed:
Note: Replace pizero with your pi username.
sudo chmod +x /home/pizero/stream.sh
sudo nano /lib/systemd/system/stream.service
Paste the following in it (keeping in mind that pizero is the user in /home/pizero/stream.sh and you will need to replace with your own user):
[Unit]
Description=Custom Webcam Streaming Service
After=multi-user.target
[Service]
Type=simple
ExecStart=/home/pizero/stream.sh
Restart=on-abort
[Install]
WantedBy=multi-user.target
Set the correct permissions on the service file:
sudo chmod 644 /lib/systemd/system/stream.service
This permission setting (644) is commonly used for configuration files and scripts that should not be executed but must be readable and editable by the owner.
644
: This is the numeric mode, which represents the new permissions for the file. In Unix-like systems, file permissions are represented by three sets of digits. The first digit represents the user's permissions, the second digit represents the group's permissions, and the third digit represents others' permissions.
6
(which stands for rw-
in symbolic notation) grants the user (owner) read and write permissions but not execute permission.4
(which stands for r--
) grants read-only permission. This is applied to both the group and others.sudo systemctl enable stream.service
By running sudo systemctl enable stream.service
, you are instructing systemd to create the necessary links and configurations to start the stream.service
automatically whenever the system boots up. This is particularly useful for background services or daemons running whenever the system is operational, such as making sure your rtsp server spins up as soon as the Raspberry Pi Zero 2 W boots up.
Start the service.
sudo service stream start
Running sudo service stream start
, you are instructing the system to start the stream
service. This is typically used to activate daemons or background services that need to run continuously or are required for the functioning of other parts of the system.
It's important to note that in systems that use systemd (which has become the default in many modern Linux distributions), the systemctl
command is generally used instead of service
. However, the service
command is still present for compatibility reasons and is often a wrapper around systemctl
in such systems.
Check the status of the service
sudo service stream status
Install NGINX on your Raspberry Pi, the basic set of modules required to run it as a web server, and the RTMP module for handling media streaming.
sudo apt install nginx-common nginx-core libnginx-mod-rtmp
This package contains standard files for all NGINX packages, such as configuration files and documentation. It depends on other NGINX packages and is usually installed alongside them.
This package provides the core NGINX files. It includes the NGINX binary and essential modules required to run NGINX as a web server and reverse proxy. This is the main NGINX package you'd install to set up an NGINX server.
This package is a module for NGINX that enables RTMP (Real-Time Messaging Protocol) support. RTMP is a protocol commonly used for streaming audio, video, and other data over the internet, especially in applications like live broadcasting. NGINX can handle RTMP streams by installing this module, making it suitable for media streaming applications.
You can go ahead and open the nginx.con
f with nano.
nano /etc/nginx/nginx.conf
Paste the following at the end of the file and save it:
rtmp {
server {
listen 1935;
chunk_size 4096;
allow publish 127.0.0.1;
deny publish all;
application pi {
live on;
record off;
}
}
}
This configuration sets up NGINX to handle RTMP streams, allowing live streaming. Here's how it works:
This line begins the configuration block for the RTMP module.
Defines a server block within the RTMP context. This is similar to server blocks in the HTTP context but for RTMP streams.
This directive tells the RTMP server to listen on port 1935, the default port for RTMP.
Sets the RTMP chunk size to 4096 bytes. This is the size of the data chunks in which the stream is divided.
Allows only the localhost (127.0.0.1) to publish streams to this server.
Denies all other IPs from publishing streams to this server. Combined with the previous directive, this restricts publishing to the local machine only.
Starts a configuration block for an application named "pi". This is a specific context for RTMP streaming.
Enables live streaming for this application.
sudo service nginx restart
You can use VLC to test your RTMP Stream by navigating to File -> Open Network (ctrl + N) and entering the following connection string:
rtmp://[YOUR_HOSTNAME]/pi/test
In my case, it is:
rtmp://pizero.local/pi/test
You can now return to your MotionEye interface (https://[YOUR_HOSTNAME]:8765
) and add a new camera.
Select Network Camera
under Camera Type and add the RTMP URL as you set it up in the stream bash script.
This should work with:
rtmp://[YOUR_HOSTNAME]/pi/test
or
rtmp://127.0.0.1/pi/test
You can, of course, change this configuration later on.
Your camera will take some time to register ( RTMP does have a significant delay).
Running MotionEye on a Raspberry PI Zero 2 W with the RTMP server as a feed produces mediocre, if not poor, results.
You can run htop
in an SSH window to view the CPU load on your Raspberry Pi. In this case, we can see that MotionEye maxes out the CPU, which is also apparent from the drop in frame rate and delay in the footage.
You can also stop the motion eye
service by running:
sudo service motioneye stop
htop
#to view resoureces
We can now observe that the resources taken up by the RTMP stream are significantly lower than the stream along with MotionEye.
We can also test how MotionEye works with the RTMP feed through a virtual machine's Ubuntu 22.04.2 LTS installation. You can install a different distribution in a virtual machine or 'bare metal' on a Raspberry Pi or your server computer.
I have followed the MotionEye installation guide, which you can find here:
https://github.com/motioneye-project/motioneye/wiki/%28Install-On-Ubuntu-%2820.04-or-Newer%29
These notes only apply if you plan on testing the connection with USB OTG.
I am using Parallels, and when you connect your Raspberry Pi, you should get a similar message. Forward the RNDIS/Ethernet Gadget to your virtual machine.
If you are using Virtual Box, you can find some instructions here:
https://www.flexihub.com/usb-in-virtual-machine/virtualbox-usb-passthrough/
If you are using USB OTG you need to set:
IPV4
to Link-Local Only
IPV6
to Disable
Your USB Ethernet connection should now list as connected. You can test it by pinging your hostname:
ping [YOUR_HOSTNAME].local
#in my case: ping pizero.local
Add the RTMP connection to the MotionEye server in the virtual machine.
You will see an ability to run much higher frame rates with lower latency.
You can also recheck your resourcing by running htop
.
In conclusion, setting up a Raspberry Pi with a camera like the Arducam IMX 519 for use with MotionEye as a discreet CCTV system with motion detection can be a rewarding DIY project. This guide has walked you through the steps, from flashing the SD card with the appropriate OS, installing the required drivers for your camera, and configuring both the camera and MotionEye software. The detailed steps included testing the camera, setting up an RTMP server, and configuring NGINX to handle the RTMP streams, ensuring a smooth and efficient setup process.
You now have a functional and customisable CCTV system by overcoming the initial hurdle of camera detection and compatibility issues with MotionEye. Whether you're using it on a Raspberry Pi Zero 2 W or another setup, the flexibility and scalability of this system are evident. Additionally, you can optimise performance and frame rates with lower latency by exploring alternative setups like running MotionEye on a separate machine or using a virtual machine to achieve a higher grade stream.
Remember that DIY projects like this provide a practical solution to your security needs and offer a great opportunity to learn and experiment with technology. Whether you're a seasoned maker or a curious beginner, the journey of setting up your own Raspberry Pi-based CCTV system with MotionEye is both educational and rewarding.
This tutorial covers installing and using the Arducam IMX 519 16 mp camera along with the Raspberry Pi Zero 2 W.
Please note that the tutorial will require specific hardware. Kindly ensure that you have the necessary components to complete the tutorial successfully.
If you would like to purchase a Raspberry Pi Zero 2 W or the Arducam IMX519, here are some affiliate links:
Raspberry Pi Zero 2 W on amazon.co.uk
To follow along, it's important to make sure that you have Raspbian Bullseye with a headless install set up on your Raspberry Pi. To get started, you can take a look at the following guide
https://albert-fit.com/how-to-set-up-raspberry-pi-zero-2-w-headless-mode/
The first step is to download the bash scripts required for installing the IMX519 driver.
sudo nano /boot/config.txt
add dtoverlay=imx519 under the line [all]
#Save and reboot
To clarify, modifying the line dtoverlay=imx519 in the /boot/config.txt file on a Linux system enables and configures a specific device tree overlay, such as the imx519 sensor.
Here's a breakdown of what this means:
dtoverlay=imx519
, the configuration instructs the operating system to load the device tree overlay for this specific sensor. This ensures the kernel has the correct information to interact with and control the IMX519 sensor.wget -O install_pivariety_pkgs.sh https://github.com/ArduCAM/Arducam-Pivariety-V4L2-Driver/releases/download/install_script/install_pivariety_pkgs.sh
Here's a breakdown of each part of the command:
wget
: This is a free utility for non-interactive downloading of files from the web. It supports HTTP, HTTPS, and FTP protocols, as well as retrieval through HTTP proxies.-O install_pivariety_pkgs.sh
: The -O
option in wget
allows you to specify the name of the file where the downloaded content will be saved. In this case, the content downloaded from the given URL will be saved to a file named install_pivariety_pkgs.sh
on your local system. If you don't use the -O
option, wget
will use the filename in the URL by default.https://github.com/ArduCAM/Arducam-Pivariety-V4L2-Driver/releases/download/install_script/install_pivariety_pkgs.sh
: This is the URL of the file you want to download. In this case, it's a shell script hosted on GitHub, designed explicitly for installing Arducam Pivariety V4L2 Driver.When you run this command, wget
will connect to the GitHub server and download the install_pivariety_pkgs.sh
script, save it to a file with the same name in your current directory.
chmod +x install_pivariety_pkgs.sh
The command chmod +x install_pivariety_pkgs.sh
is used to change the file permissions of the script install_pivariety_pkgs.sh
. Let's break down this command:
chmod
: This is a Unix command used to change the permissions of a file or directory. Permissions dictate who can read, write, or execute the file or directory.+x
: This part of the command is an argument to chmod
that specifies the change to be made to the file's permissions. The +x
means "add execute permission." In Unix-like operating systems, a file must have execute permission to be run as a program.install_pivariety_pkgs.sh
: This is the file name whose permissions you are changing. In this case, it's the script that you previously downloaded using wget
.By running chmod +x install_pivariety_pkgs.sh
, you are adding execute permissions to the install_pivariety_pkgs.sh
script. This means that after running this command, you (and other users with appropriate permissions) can run this script as a program on your system. This step is often necessary for scripts downloaded from the internet, as they typically do not have execute permissions set by default for security reasons.
./install_pivariety_pkgs.sh -p imx519_kernel_driver_low_speed
The command ./install_pivariety_pkgs.sh -p imx519_kernel_driver_low_speed
executes a script with specific arguments. Let's break down this command:
./install_pivariety_pkgs.sh
:.
: This represents the current directory in Unix-like operating systems./
: This is the directory separator.install_pivariety_pkgs.sh
: This is the name of the script you want to execute../
before the script name indicates that it is in the current directory and you want to execute it. This is necessary because, in most Unix-like systems, the current directory is not in the PATH by default for security reasons. Thus, to run a script from the current directory, you need to specify the path explicitly, and ./
is the way to do that.-p imx519_kernel_driver_low_speed
:
-p
: This is an argument that the script install_pivariety_pkgs.sh
is designed to accept. The meaning of -p
is specific to the script itself. The help function lists -p as "Specify Package Name".imx519_kernel_driver_low_speed
: This appears to be another argument to the script, likely specifying a particular package or module to be installed or configured. In this context, it seems to refer to a lower-speed version of the kernel driver for the IMX519 camera sensor.When you run this command, the script install_pivariety_pkgs.sh
is executed, and it receives -p imx519_kernel_driver_low_speed
as its argument. This triggers the installation of the low-speed kernel driver for the IMX519 sensor.
./install_pivariety_pkgs.sh -p libcamera
./install_pivariety_pkgs.sh -p libcamera_apps
libcamera
and libcamera-apps
are particularly relevant when using Arducam cameras on the Raspberry Pi because libcamera
provides the underlying support for camera hardware. In contrast, libcamera-apps
offers practical examples of how to use the library in real-world applications.
libcamera
is a complex, cross-platform camera support library designed for embedded and mobile devices. It aims to provide a modern and easy-to-use API for camera hardware control.libcamera
is designed to work across different operating systems and hardware platforms, making it a versatile choice for camera applications in diverse environments.libcamera-apps
are applications or tools built on top of the libcamera
library. They serve as examples or utilities for using libcamera
.libcamera
.libcamera-apps
might include a basic camera app, a video recording tool, or a streaming application.libcamera-apps
can be helpful for developers as reference implementations or starting points for building their own camera-based applications.Open up Real VNC (or similar) and connect to the remote desktop.
Alternatively, plug in a monitor to the mini HDMI port.
This shows if you have correctly installed the camera to the PI.
libcamera-hello
You can also try to take a still image and a video which will be saved in ~.
libcamera-still -t 0 --autofocus-mode auto -o test.jpg
libcamera-vid -t 10000 -o test.h264
Installing VLC can take a while.
sudo apt install -y vlc
Allow VLC to be run as root. This is needed for the streaming to start.
sudo sed -i 's/geteuid/getppid/' /usr/bin/vlc
You can list your camera with libcamera-hello, which will allow you to detect the optimal resolution.
libcamera-hello --list-cameras
The output code should look similar to the following snippet.
You can use nano to create the stream.sh file.
sudo nano ~/stream.sh
The following are the contents for the stream.sh file.
You might want to experiment with the resolution, frame rate and other parameters.
--vflip and/or --hflip are used for correcting the view.
You can also set up authentication. Replace the username and password with your preferred credentials.
#!/bin/bash
libcamera-vid -t 0 --inline --width 1920 --height 1080 --framerate 15 -o - | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/stream}' :demux=h264 --sout-rtsp-user=username --sout-rtsp-pwd=password
This command is a Bash script that combines two commands to capture video from a camera and stream it over RTSP (Real-Time Streaming Protocol) with VLC media player. Let's break down each part:
#!/bin/bash
: This is the shebang line that tells the system to execute this script with the Bash shell.libcamera-vid -t 0 --inline --width 1920 --height 1080 --framerate 15 -o -
:
libcamera-vid
: A command-line tool to capture video from a camera, typically used with Raspberry Pi cameras.-t 0
: Sets the timeout to 0 seconds, meaning the capture will continue indefinitely.--inline
: Enables inline headers for H.264 encoding.--width 1920 --height 1080
: Sets the video resolution to 1920x1080 pixels.--framerate 15
: Sets the video frame rate to 15 frames per second.-o -
: Specifies the output to standard output (stdout).|
: This pipe symbol takes the output of the libcamera-vid
command and uses it as the input for the next command.cvlc -vvv stream:///dev/stdin
:
cvlc
: The command-line interface for VLC media player.-vvv
: Increases the verbosity level to provide more detailed logging.stream:///dev/stdin
: Tells VLC to read the video stream from standard input (stdin), which is the output from libcamera-vid
.--sout '#rtp{sdp=rtsp://:8554/stream}'
:
--sout
: Specifies the streaming output chain for VLC.#rtp{sdp=rtsp://:8554/stream}
: Configures VLC to use RTP (Real-Time Protocol) and sets up an SDP (Session Description Protocol) announcement for RTSP at port 8554 with the path /stream
.:demux=h264
:
:demux=h264
: Specifies that the input stream is in H.264 format.--sout-rtsp-user=username --sout-rtsp-pwd=password
:
--sout-rtsp-user
and --sout-rtsp-pwd
: These options set the username and password for the RTSP stream, providing basic authentication.In summary, this script captures video from a camera using libcamera-vid
, encodes it in H.264 format, and streams it over an RTSP server using VLC, with access controlled by a username and password.
Note that while this method adds a basic level of security, it's essential to consider additional security measures, especially if the stream is sensitive or needs to be highly secure.
Securing RTSP (Real-Time Streaming Protocol) streams is important to prevent unauthorised access and ensure the privacy and integrity of the video data. Here are some recommendations for enhancing security over RTSP:
Now you have to enable the script file to be executed:
Note: Replace pizero with your pi username.
sudo chmod +x /home/pizero/stream.sh
sudo nano /lib/systemd/system/stream.service
Paste the following in it (keeping in mind that pizero is the user in /home/pizero/stream.sh and you will need to replace with your own user):
[Unit]
Description=Custom Webcam Streaming Service
After=multi-user.target
[Service]
Type=simple
ExecStart=/home/pizero/stream.sh
Restart=on-abort
[Install]
WantedBy=multi-user.target
Set the correct permissions on the service file:
sudo chmod 644 /lib/systemd/system/stream.service
This permission setting (644) is commonly used for configuration files and scripts that should not be executed but must be readable and editable by the owner.
644
: This is the numeric mode, which represents the new permissions for the file. In Unix-like systems, file permissions are represented by three sets of digits. The first digit represents the user's permissions, the second digit represents the group's permissions, and the third digit represents others' permissions.
6
(which stands for rw-
in symbolic notation) grants the user (owner) read and write permissions but not execute permission.4
(which stands for r--
) grants read-only permission. This is applied to both the group and others.sudo systemctl enable stream.service
By running sudo systemctl enable stream.service
, you are instructing systemd to create the necessary links and configurations to start the stream.service
automatically whenever the system boots up. This is particularly useful for background services or daemons running whenever the system is operational, such as making sure your rtsp server spins up as soon as the Raspberry Pi Zero 2 W boots up.
Start the service.
sudo service stream start
Running sudo service stream start
, you are instructing the system to start the stream
service. This is typically used to activate daemons or background services that need to run continuously or are required for the functioning of other parts of the system.
It's important to note that in systems that use systemd (which has become the default in many modern Linux distributions), the systemctl
command is generally used instead of service
. However, the service
command is still present for compatibility reasons and is often a wrapper around systemctl
in such systems.
Check the status of the service
sudo service stream status
You are good to go as long as Active displays as active (running).
Start VLC media player and navigate to Media -> Open Network Stream (or press CTRL + N).
Enter URL as follows:
rtsp://pizero.local:8554/stream
Replace pizero with your specific hostname.
At this point, VLC will ask you for the username and password you set up.
Congratulations! You have now set up your Raspberry Cam Zero 2 W with the Arducam IMX519 to broadcast over RTSP.
You can check out more detail on the setup at the following links:
Arducam IMX519 Quick Start Guide
This guide is aimed at helping you set up the Raspberry Pi Zero 2 W in "headless mode". Headless mode connects to the Raspberry Pi command line or desktop environment without attaching a screen, keyboard, or mouse. Connect the Raspberry Pi to your main machine via WiFi or USB OTG, and you can use SSH to dial in. The guide has specific sections for Windows, Mac, and Linux.
If you would like to purchase a Raspberry Pi Zero 2 W or the Arducam IMX519, here are some affiliate links:
Raspberry Pi Zero 2 W on amazon.co.uk
Have an SD card ready and plug it into your machine's SD card reader.
Note: Flashing works best with the internal SD Card reader because the card will have multiple partitions.
Download and install the Raspberry Pi Imager. You can get the utility from the downloads page:
https://www.raspberrypi.com/software/
If you are on linux:
sudo apt install rpi-imager
Go ahead and select Raspberry Pi Zero 2 W from the Device Section.
Select the Raspberry Pi Os (Legacy, 32-bit), a Debian Bullseye port. Select the SD card you inserted and click next.
EDIT SETTINGS to apply OS customisation settings.
It would be best if you considered giving each hostname a unique name.
Set up your username and password.
Include your Wifi SSID (name) and password.
You can go ahead and write the configuration to the card.
You can access the. Raspberry Pi Zero 2 W headless (without a screen, keyboard and mouse) by setting up USB OTG (On The Go).
You need to edit some files on the SD card, using a code editor like Visual Studio Code or Notepad++.
You can download Visual Studio Code from here:
If you would like a privacy-focused version of Visual Studio Code, you can try VSCodium from here:
Or if you want to try Notepad++:
To enable headless USB OTG we need to load the dwc2 driver. According to the good people on the Raspberry Pi Forums, dwc2 is an upstream driver which can do the OTG host/gadget flip dictated by OTG_SENSE.
You can just browse the bootfs partition with your favourite editor.
Search for rootwait in cmdline.txt. Add one space after rootwait and paste in the following:
modules-load=dwc2,g_ether
Open config.txt and type the following under the [all] section:
dtoverlay=dwc2
Create a blank file and name it ssh, with no extension. This will enable ssh on the pi zero.
Make sure you save all files. If in doubt, check out the screenshot below.
You can now unplug the card from your machine and plug it into the Raspberry Pi.
This section will show you how to establish a secure shell (SSH) between your machine and your Raspberry Pi Zero 2 W. You can use your terminal to install, update, and configure the libraries.
There are a few prerequisites which are required to connect to your Raspberry Pi through a Windows machine:
Note: Connecting the Raspberry Pi 2 Zero W to Windows 10 through USB OTG can make the USB not recognised by the driver as USB Ethernet/Gadget. If this is the case, I wrote a post with the fix, which you can find here:
https://albert-fit.com/how-to-fix-windows-10-raspberrypi-usb-otg-ethernet-gadget/
Strangely, you need Bonjour to get Raspberry Pi to talk to Windows over a local network.
According to Free Code Camp, Bonjour is Apple's implementation of zero-configuration networking (zeroconf). It allows devices running Windows and Apple operating systems (like macOS and iOS) to connect and share resources without configuration settings.
Bonjour allows you to locate other devices, such as scanners and printers, on a local network and connect with them. It also will enable you to share files irrespective of your operating system, whether it's Windows, macOS, or Linux.
Bonjour will be installed on a Windows machine when setting up iTunes. You can also download and install the Bonjour SDK from:
https://developer.apple.com/bonjour/
According to SSH.com, PuTTY is a versatile terminal program for Windows. It is the world's most popular free SSH client. Putth supports SSH, telnet, and raw socket connections with good terminal emulation. Putty also supports public key authentication and Kerberos single sign-on. It also includes command-line SFTP and SCP implementations.
You can download Putty from:
The official page:
https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
or the Microsoft Store:
Once you have installed putty, you can ssh into your Raspberry Pi by typing your hostname in the following format:
[user]@[hostname].local
You will then be prompted for your password, which you set up in the Pi Imager under username and password OS configuration.
Upon a successful connection, the putty window text displayed gives you the [username]@[hostname]:~$ line
No additional software is required to connect the Raspberry Pi Zero 2 W to a Mac machine. You can verify if your USB OTG setup is working by navigating to Network in Settings, where you will be able to see the RNDIS/Ethernet Gadget.
Once connected, you can open a terminal window (cmd + space to open Spotlight and type terminal). In the terminal, type ssh [username]@[hostname]. Just so you know, the authenticity of the host won't be established (because we have just created it). You can type yes to continue, and you will be prompted to insert the password, which you set up in the Pi Imager under username and password OS configuration.
When successfully connected, you will be greeted by [username]@hostname:~ $
I currently run a Pop Os! machine, and I know the following settings to work for USB OTG on any Debian based distribution:
You can now ssh the same way as you do with mac:
First, let's update the system. Connect to the Raspberry Pi with ssh and type the following in the terminal window:
sudo apt update
sudo apt full-upgrade
sudo reboot
The Sudo is a command-line utility for Unix-based systems. The utility provides an efficient way to temporarily grant users or user groups privileged access to system resources to run commands they cannot run under their regular accounts.
The apt
command is a powerful package management utility used primarily in Debian-based distributions, including Ubuntu. It performs package management tasks, such as installing, removing, and upgrading software packages.
In Linux-based operating systems, mainly Ubuntu, the command essentially instructs to update the local package index. It downloads package details from all set sources to refresh the package cache. The package manager (in this case, ‘apt’) must know which packages are available and where to find them to install software or packages on your system. You can read more about it on linuxsimplify.com.
According to Kris Koishigawa, the "full
upgrade
" command downloads and installs the updates for each outdated package and dependency on your system.
The "autoremove" command will search for any packages and dependencies that are no longer required and remove them.
sudo apt autoremove
sudo apt clean
The "clean" command will clean up the cache.
RealVNC is one of the many ways you can gain remote access to your Raspberry Pi Zero 2 W. Install RealVNC on your Raspberry Pi by typing the following in the ssh terminal:
sudo apt install realvnc-vnc-server realvnc-vnc-viewer
You now need to enable the VNC server on the Raspberry Pi:
sudo raspi-config
Hitting enter will Open the Raspberry Pi Software Configuration Tool.
Navigate to the Interface Options.
Select VNC and hit Enter.
Hit enter again to enable the VNC.
Now, you can exit this menu by hitting Esc, Esc.
Download the client from the following link:
https://www.realvnc.com/en/connect/download/combined/
You do not have to sign up to download and use the client on your local network.
Click on "Use only for direct or offline connectivity".
You can make an account if you'd like to. If you do not wish to create an account, click "Use RealVNC Viewer without signing in".
Type the following command to get the IP of the Raspberry Pi from the window where you have the SSH connection open:
hostname -I
Copy the IP shown first with Ctrl+Shift+C and paste it in the "Enter a VNC Server address or search" bar.
You will get the server identity check error, and you can continue. (You have just set up this server, so there is no identity to check).
Alternatively, you can connect with your hostname. local (in my example pizero.local).
Once you connect, the connection will be saved under your connections.
Now you should see the Raspbian Desktop in a window on your machine!
Congratulations on your Raspberry Pi Zero 2 W setup!
This tutorial details how to:
In this comprehensive guide, we'll tackle the common USB OTG connection issues when using the Raspberry Pi Zero 2 with Windows 10.
The problem is that when you connect the Raspberry Pi Zero 2 W, it first gets identified as a USB Serial Device on a COM port.
Thankfully, we can overcome this by installing the RNDIS driver for the USB device.
I have uploaded the specific driver on my GitHub page, and you can find it here:
https://github.com/albert-fit/windows_10_raspi_usb_otg_fix
As in the image above, you can connect the Raspberry Pi by plugging the USB into the connector closest to the HDMI port.
Once connected, the Raspberry Pi green light will be on, and you should see the device in your device manager's Ports (COM & LPT) section.
To open the device manager, press the windows symbol on your keyboard and type Device Manager. Click on the first result.
Note: If you do not see the Raspberry Pi in the Ports section, try checking the following:
To update the driver, right-click on the USB Serial Device and click on Update Driver.
You can just select the folder you have unzipped when downloading the driver.
Once the update is complete, the device will show as a USB Ethernet/RNDIS Gadget.
You can check it works as it should by navigating to Network & Internet Settings and clicking on the new Ethernet connection.
You should see the USB Ethernet/RNDIS Gadget in the description.
Congratulations! You can now go ahead and try to SSH or VNC with your favourite clients.
If you would like to purchase a Raspberry Pi Zero 2 W, here are some affiliate links:
Raspberry Pi Zero 2 W on amazon.co.uk
This post was largely inspired by MatsK.