Mainsail is a beautiful, responsive and modern interface for Klipper Firmware and in this guide I will show you how to add webcam support (usb/pi webcam) to allow you to keep an eye on your 3D printer remotely!
This guide will assume that you already have a working Raspberry Pi with Klipper and Mainsail installed.
If you don’t, please check the 3DPrintBeginner tutorial on how to install Klipper and MainsailOS.
To add webcam support to Mainsail, we'll use mjpg-streamer—a command line application that copies JPEG frames from your camera input and combines them into a video stream that can be picked up by Mainsail's interface.
Installing dependencies
Before we install mjpg-streamer we need to be sure that our host system has all the dependencies installed.
To do this, we need to open a SSH instance with our Raspberry Pi:
If you didn’t change the password at install of after the install, the pass should be: “raspberry”
After that we need to install the dependencies:
Installing mjpeg-streamer
Now, we need to create a clone of mjpg-streamer (to checkout the sources so we can install it):
For that, just run the below commands in the terminal:
Testing the webcam stream
Now we need to test the stream of the webcam.
You can use either a USB Webcam or a Raspberry Pi camera module attached via ribbon cable.
The commands are a bit different for a USB webcam or a Pi camera
USB Camera:
Pi camera:
If you are using a Pi camera and the command above is not working, please make sure you have enabled pi cam support:
In both cases we're telling mjpg-streamer to start a stream using your camera (USB, or Raspi), to output the stream at a framerate of 15fps and a resolution of 1280 horizontal by 720 vertical pixels (720P), and to enable http access to the stream.
Test the stream
You can access(test) the webcam stream by accessing via your webbrowser the url
If all has gone well your camera should be streaming. Congratulations! You can now stop the stream by entering CTRL+C in your terminal window.
Run MJPEG-streamer on startup
First thing we need is to create a shell script:
This will open nano editor. Paste the following into the editor (credits for the script to Jacob) :
This is for the Pi Camera:
And this one is for the USB Camera:
#!/bin/sh
# /etc/init.d/mainsail-stream.sh
### BEGIN INIT INFO
# Provides: mainsail-stream.sh
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: mjpg_streamer for webcam
# Description: Streams /dev/video0 to http://IP/?action=stream
### END INIT INFO
f_message(){
echo "[+] $1"
}
# Carry out specific functions when asked to by the system
case "$1" in
start)
f_message "Starting mjpg_streamer"
/usr/local/bin/mjpg_streamer -b -i "input_uvc.so -fps 15 -x 1640 -y 922" -o "output_http.so -w /usr/local/share/mjpg-streamer/www"-b
sleep 2
f_message "mjpg_streamer started"
;;
stop)
f_message "Stopping mjpg_streamer…"
killall mjpg_streamer
f_message "mjpg_streamer stopped"
;;
restart)
f_message "Restarting daemon: mjpg_streamer"
killall mjpg_streamer
/usr/local/bin/mjpg_streamer -b -i "input_uvc.so -fps 15 -x 1640 -y 922" -o "output_http.so -w /usr/local/share/mjpg-streamer/www"
sleep 2
f_message "Restarted daemon: mjpg_streamer"
;;
status)
pid=`ps -A | grep mjpg_streamer | grep -v "grep" | grep -v mjpg_streamer. | awk ‘{print $1}’ | head -n 1`
if [ -n "$pid" ];
then
f_message "mjpg_streamer is running with pid ${pid}"
f_message "mjpg_streamer was started with the following command line"
cat /proc/${pid}/cmdline ; echo ""
else
f_message "Could not find mjpg_streamer running"
fi
;;
*)
f_message "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac
exit 0
Once you have your script pasted and any changes configured, press CTRL+X to exit the editor. The editor will ask Save modified buffer? Press Y and hit Enter/Return.
Enable MJPEG to run at startup
So now we need to enable the script to run at startup, if somehow Pi shutdowns/restarts, we need to start the MJPEG script also:
Now we need to start the service:
And also, test to see if the service is running and working as expected:
Press CTRL+C to exit the above command.
Adding your webcam to Mainsail
Head over to your web browser, open Mainsail, and navigate to Settings > Interface. Here we can configure several different options related to your webcam:
Access Mainsail's interface options
Webcam URL: enter webcam/?action=stream in this field.
Toggle this option to enable the webcam menu item on the left.
Toggle this option to display your webcam in the dashboard menu item.
That’s all folks :) !
Hope you enjoyed it!
Many thanks for this tutorial. I followed it and was able to get the cam working (once). When i tried to start the service with "sudo service mainsail-stream start" i get the following message:
ReplyDeletepi@mainsailos:~ $ sudo service mainsail-stream.sh start
Job for mainsail-stream.service failed because the control process exited with e rror code.
See "systemctl status mainsail-stream.service" and "journalctl -xe" for details.
pi@mainsailos:~ $ sudo service mainsail-stream status
● mainsail-stream.service
Loaded: loaded (/etc/init.d/mainsail-stream.sh; generated)
Active: failed (Result: exit-code) since Sun 2022-02-20 15:22:42 GMT; 20s ago
Docs: man:systemd-sysv-generator(8)
Process: 4302 ExecStart=/etc/init.d/mainsail-stream.sh start (code=exited, status=2)
What am I doing wrong here?
I have to ssh this (/usr/local/bin/mjpg_streamer -i "input_uvc.so -y -q 75 -f 15 -r 640x480" \
ReplyDelete-o "output_http.so -w /usr/local/share/mjpg-streamer/www") in everytime i boot the pi. it doesnt seem to be automagicaly starting. any help on where to look or what to do. thanks this got me camera after week and i mean weeks of trying different options with my ps3 cam.
Thank you for the great article. Any way of having both of these running at the same time?
ReplyDeleteGreat article. What about multiple cameras?
ReplyDelete