Skip to content

Instantly share code, notes, and snippets.

@dimo414
Last active June 5, 2023 06:53
Show Gist options
  • Save dimo414/901aa09935e54f464bf86c384c983c2d to your computer and use it in GitHub Desktop.
Save dimo414/901aa09935e54f464bf86c384c983c2d to your computer and use it in GitHub Desktop.
Raspberry Pi Camera + mjpg-streamer systemd setup

This article includes setup instructions for the Raspberry Pi camera, but doesn't capture setting it up as a service (even though a few lines up it sets up OctoPrint as a service), and instead provides a pair of relatively-complex scripts to be run at startup via /etc/rc.local.

Setup

  • copy webcam.sh to the mjpg-streamer directory (or wherever you'd like, just update the MJPG_DIR variable)
  • copy pi-webcam.service to /etc/systemd/system/ (requires sudo), ensure ExecStart points to the correct path
  • Register the service with sudo systemctl enable pi-webcam.service
[Unit]
Description=Run Pi Camera as Webcam
After=network-online.target
Wants=network-online.target
[Service]
Environment="LC_ALL=C.UTF-8"
Environment="LANG=C.UTF-8"
Type=simple
User=pi
ExecStart=/home/pi/mjpg-streamer/webcam.sh
[Install]
WantedBy=multi-user.target
#!/bin/bash
MJPG_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )/mjpg-streamer-experimental"
camera_options="-fps 5"
http_options="-p 5001"
start_cam() {
LD_LIBRARY_PATH="$MJPG_DIR" "${MJPG_DIR}/mjpg_streamer" \
-i "input_raspicam.so ${camera_options}" \
-o "output_http.so -w ${MJPG_DIR}/www ${http_options}"
}
# According to
# https://community.octoprint.org/t/setting-up-octoprint-on-a-raspberry-pi-running-raspbian-or-raspberry-pi-os/2337
# this is necessary; it doesn't seem to be but it's harmless
vcgencmd version &> /dev/null
if [[ "$(vcgencmd get_camera)" = 'supported=1 detected=1'* ]]; then
logger "webcam.sh: Starting Raspberry Pi camera"
start_cam
else
logger "webcam.sh: Raspberry Pi camera not found: $(vcgencmd get_camera)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment