Seen a post about webcam at jcode, i made my own version because:
1. need no additional python libraries
2. need camera stay on while printer is on, ready for next print, not depending on octoprint status
Running cron script every minute:
* * * * * /home/pi/autocam.py
and script autocam.py:
#!/usr/bin/python
import os
import subprocess
SERIAL_DEV = "/dev/serial/by-id/your-serial-port-id"
def checkserial():
return os.path.exists(SERIAL_DEV)
def checkwebcam():
r = subprocess.check_output("ps ax | grep mjpg_streamer -c",shell=True)
c = int(r)-2
return c > 0
if __name__ == "__main__":
if checkserial() and not checkwebcam():
os.system("echo $(date '+%Y-%m-%d %H:%M:%S') start >> autocam.log")
os.system("service webcamd start")
elif not checkserial() and checkwebcam():
os.system("echo $(date '+%Y-%m-%d %H:%M:%S') stop >> autocam.log")
os.system("service webcamd stop")
Source: https://jcode.me/octoprint-turning-off-webcam-automatically/