Powersaving using USB-webcam

I'm using a simple Chinese USB webcam (works with uvccamera Linux kernel module) on a home server, having a Braswell-based CPU (ultra low TDP) with mjpg-streamer and Octoprint. OS is the current LTS Ubuntu Server (18.04).

Everything works fine, but I found, that the webcam seems to be running all the time instead of just when someone is visiting the web interface or actually "using" the stream. The cam gets relatively hot (even in low resolution mode) and has basically doubled the energy consumption of the whole system running also other services. The CPU load is very low (around 1-3% for the mjpg-streamer running service), though.

As soon as I stop the mjpg-streamer service, the cam seems to be disabled (no more heat). It seems to be wasteful to let the cam run 24/7 for only looking at the stream maybe once or twice a day for a few seconds.
Maybe this is a really crappy webcam, but I was under the impression, that the cam software (in this case mjpg-streamer) only accesses the cam hardware and "doing its thing", when there is actually one or more clients watching the stream.

Since I've designed the whole system to be very energy-saving, I won't use the cam like this.

I don't need or want time lapse recordings, I only want the cam for checking in on the printer from time to time.

Any ideas how to do this? I was thinking of some kind of script that starts the service when a connection on the web server port is made and disabling it automatically after a certain time - maybe this is possible using systemd. As far as I could find out, mjpg-streamer doesn't support any "power saving" options.

Or maybe there is another software option?

Thank you for any input on this topic! :slight_smile:

I can't help you with an automated script but I got an other idea:

There is a part in this post which shows how you can control your webcam start script via a button in the webinterface.
(it's in the Optional: Webcam section)

Thank you very much! :slight_smile: This is the next best thing to having it automatically start/stop.

I will describe what I did, maybe someone else wants to do it the same way:

Since I'm using snapd for running mjpg-streamer, I added the following commands to the config.yaml of Octoprint:

system:
  actions:
  - action: streamon
    command: /usr/bin/sudo /usr/bin/snap start mjpg-streamer
    confirm: false
    name: Enable webcam
  - action: streamoff
    command: /usr/bin/sudo /usr/bin/snap stop mjpg-streamer
    confirm: false
    name: Disable webcam

Since I'm running OctoPrint with the system user "octoprint", I added following to my sudoer.d/file in /etc/sudoers.d/octoprint

octoprint ALL = NOPASSWD: /usr/bin/snap start mjpg-streamer
octoprint ALL = NOPASSWD: /usr/bin/snap stop mjpg-streamer

This allows the user octoprint to execute the command with root privileges.

Now I'm able to start and stop the service within the OctoPrint GUI:

1 Like