From d25af2774cc9f88f7b577e4df3f19218c43e3673 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Tue, 27 Feb 2024 19:00:24 +0100 Subject: [PATCH] initial commit --- README.md | 1 + prusaconnect | 45 ++++++++++++++++++++++++++++++++++++++++++++ prusaconnect.service | 14 ++++++++++++++ stream | 3 +++ stream.service | 14 ++++++++++++++ 5 files changed, 77 insertions(+) create mode 100644 README.md create mode 100755 prusaconnect create mode 100644 prusaconnect.service create mode 100755 stream create mode 100644 stream.service diff --git a/README.md b/README.md new file mode 100644 index 0000000..60ebd4d --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +ustreamer is provided by https://github.com/pikvm/ustreamer diff --git a/prusaconnect b/prusaconnect new file mode 100755 index 0000000..35b570e --- /dev/null +++ b/prusaconnect @@ -0,0 +1,45 @@ +#!/bin/bash + +# Set default values for environment variables +: "${HTTP_URL:=https://webcam.connect.prusa3d.com/c/snapshot}" +#: "${CAMERA_DEVICE:=/dev/video0}" +#: "${RTSP_URL:=rtsp://127.0.0.1/stream1}" +: "${WEBCAM_URL:=http://127.0.0.1:8080/snapshot}" +: "${DELAY_SECONDS:=10}" +: "${LONG_DELAY_SECONDS:=60}" +: "${FINGERPRINT:=6hQoYaD67pVje5UuZCNw}" +: "${TOKEN:=RqOepwpzApYOjoGjQst7}" +: "${IMG_PATH:=$HOME/output.jpg}" +: "${STATUS_URL:=http://192.168.0.130/api/v1/status}" +: "${PRUSALINK_KEY:=iv3yNhDfDRaHdeD}" + +while true; do + state=$(curl -m 5 -sSL $STATUS_URL -H "X-Api-Key: $PRUSALINK_KEY" | jq -r ".printer.state") + if [ $state == "PRINTING" ]; then + curl -k -sSL $WEBCAM_URL -o $IMG_PATH + # If no error, upload it. + if [ $? -eq 0 ]; then + # POST the image to the HTTP URL using curl + curl -X PUT "$HTTP_URL" \ + -H "accept: */*" \ + -H "content-type: image/jpg" \ + -H "fingerprint: $FINGERPRINT" \ + -H "token: $TOKEN" \ + --data-binary "@$IMG_PATH" \ + --no-progress-meter \ + --compressed + + # Reset delay to the normal value + DELAY=$DELAY_SECONDS + else + echo "FFmpeg returned an error. Retrying after ${LONG_DELAY_SECONDS}s..." + + # Set delay to the longer value + DELAY=$LONG_DELAY_SECONDS + fi + else + echo "printer not printing. retrying after ${LONG_DELAY_SECONDS}s..." + DELAY=$LONG_DELAY_SECONDS + fi + sleep "$DELAY" + done diff --git a/prusaconnect.service b/prusaconnect.service new file mode 100644 index 0000000..0a6a370 --- /dev/null +++ b/prusaconnect.service @@ -0,0 +1,14 @@ +[Unit] +Description=Send snapshots to prusaconnect +After=network.target + +[Service] +ExecStart=/home/gaugendre/printer-raspberry-pi/prusaconnect +Type=exec +Restart=always +User=gaugendre +Group=gaugendre + +[Install] +WantedBy=default.target +RequiredBy=network.target diff --git a/stream b/stream new file mode 100755 index 0000000..81ba209 --- /dev/null +++ b/stream @@ -0,0 +1,3 @@ +#!/bin/bash + +/usr/bin/libcamerify /home/gaugendre/ustreamer/ustreamer --device /dev/video0 --host 0.0.0.0 -r 1024x768 --encoder=m2m-image diff --git a/stream.service b/stream.service new file mode 100644 index 0000000..599fd75 --- /dev/null +++ b/stream.service @@ -0,0 +1,14 @@ +[Unit] +Description=Stream camera to mjpeg +After=network.target + +[Service] +ExecStart=/home/gaugendre/printer-raspberry-pi/stream +Type=exec +Restart=always +User=gaugendre +Group=gaugendre + +[Install] +WantedBy=default.target +RequiredBy=network.target