Skip to content

Instantly share code, notes, and snippets.

@kevdougful
Created January 19, 2021 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevdougful/7bedad03a6560e4fa86c20de76d2ea59 to your computer and use it in GitHub Desktop.
Save kevdougful/7bedad03a6560e4fa86c20de76d2ea59 to your computer and use it in GitHub Desktop.
OctoPrint USB WebCam Setup
# /etc/haproxy/haproxy.cfg
global
maxconn 4096
user haproxy
group haproxy
daemon
log 127.0.0.1 local0 debug
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
option http-server-close
option forwardfor
maxconn 2000
timeout connect 5s
timeout client 15min
timeout server 15min
frontend public
bind :::80 v4v6
use_backend webcam if { path_beg /webcam/ }
default_backend octoprint
backend octoprint
reqrep ^([^\ :]*)\ /(.*) \1\ /\2
option forwardfor
server octoprint1 127.0.0.1:5000
backend webcam
reqrep ^([^\ :]*)\ /webcam/(.*) \1\ /\2
server webcam1 127.0.0.1:8080
# install deps
apt install -y haproxy subversion libjpeg8-dev imagemagick ffmpeg libv4l-dev cmake
# install octoprint proxy config
cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg_old
cp haproxy.cfg /etc/haproxy/haproxy.cfg # use the haproxy.cfg from this gist
# enable the haproxy service
echo ENABLE=1 >> /etc/default/haproxy
service haproxy restart
# build mjpg-streamer
git clone https://github.com/jacksonliam/mjpg-streamer.git ~/mjpg-streamer
cd ~/mjpg-streamer/mjpg-streamer-experimental
export LD_LIBRARY_PATH=.
make
# create the webcam scripts
cp webcam.sh webcamDaemon.sh ~/.octoprint/scripts
chmod +x ~/.octoprint/scripts/*
# startup script
cp rc.local /etc/rc.local
chmod +x /etc/rc.local
# reboot
shutdown -r now
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/home/ubuntu/.octoprint/scripts/webcam.sh start
exit 0
#!/bin/bash
# Start / stop streamer daemon
case "$1" in
start)
/home/ubuntu/.octoprint/webcamDaemon.sh >/dev/null 2>&1 &
echo "$0: started"
;;
stop)
pkill -x webcamDaemon.sh
pkill -x mjpg_streamer
echo "$0: stopped"
;;
*)
echo "Usage: $0 (start|stop)" >&2
;;
esac
#!/bin/bash
MJPGSTREAMER_HOME=/home/ubuntu/mjpg-streamer/mjpg-streamer-experimental
MJPGSTREAMER_INPUT_USB="input_uvc.so"
MJPGSTREAMER_INPUT_RASPICAM="input_raspicam.so"
# init configuration
camera="auto"
camera_usb_options="-r 640x480 -f 10"
camera_raspi_options="-fps 10"
if [ -e "/boot/octopi.txt" ]; then
source "/boot/octopi.txt"
fi
# runs MJPG Streamer, using the provided input plugin + configuration
function runMjpgStreamer {
input=$1
pushd $MJPGSTREAMER_HOME
echo Running ./mjpg_streamer -o "output_http.so -w ./www" -i "$input"
LD_LIBRARY_PATH=. ./mjpg_streamer -o "output_http.so -w ./www" -i "$input"
popd
}
# starts up the RasPiCam
function startRaspi {
logger "Starting Raspberry Pi camera"
runMjpgStreamer "$MJPGSTREAMER_INPUT_RASPICAM $camera_raspi_options"
}
# starts up the USB webcam
function startUsb {
logger "Starting USB webcam"
runMjpgStreamer "$MJPGSTREAMER_INPUT_USB $camera_usb_options"
}
# we need this to prevent the later calls to vcgencmd from blocking
# I have no idea why, but that's how it is...
vcgencmd version
# echo configuration
echo camera: $camera
echo usb options: $camera_usb_options
echo raspi options: $camera_raspi_options
# keep mjpg streamer running if some camera is attached
while true; do
if [ -e "/dev/video0" ] && { [ "$camera" = "auto" ] || [ "$camera" = "usb" ] ; }; then
startUsb
elif [ "`vcgencmd get_camera`" = "supported=1 detected=1" ] && { [ "$camera" = "auto" ] || [ "$camera" = "raspi" ] ; }; then
startRaspi
fi
sleep 120
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment