I'm not a fan of leaving a webcam running unattended in my home, even if its pointed at my printer and a wall.
So I made a little self-contained python script that reads the printers status from Octoprint. When the printer is printing, the webcam is on, allowing for timelapses and snapshots to be sent. If the printer is off or has just completed printing the webcam is also off.
Running every minute via Cron is a timely way to trigger the script and poll the printers status.
* * * * * python /home/pi/autoWebcam.py
import json
from urllib2 import Request, urlopen
import psutil
import os
def octoprint(path):
q = Request("http://10.0.0.60/api/{}".format(path))
q.add_header("X-Api-Key", "[YOUR API KEY GOES HERE]")
a = urlopen(q).read()
return json.loads(a)
def checkWebcamD():
for proc in psutil.process_iter():
if proc.name() == "mjpg_streamer":
return True
return False
if __name__ == "__main__":
printer = octoprint("job")
if "Offline" in printer['state'] or "Operational" in printer['state']:
if checkWebcamD():
os.system("/etc/init.d/webcamd stop")
if "Printing" in printer['state']:
# If we are printing, check the webcam
# if the webcam service is not running, turn it on
if not checkWebcamD():
os.system("/etc/init.d/webcamd start")