Skip to content

Instantly share code, notes, and snippets.

@mandreko
Forked from joltcan/README.md
Last active June 1, 2024 10:33
Show Gist options
  • Save mandreko/1ac7e189967f7a170485c4a04c459c57 to your computer and use it in GitHub Desktop.
Save mandreko/1ac7e189967f7a170485c4a04c459c57 to your computer and use it in GitHub Desktop.
Prusa Connect Webcam upload

From Nunos great instructions

Instructions

  1. Go to the Cameras section at https://connect.prusa3d.com
  2. Add a new camera.
  3. Click the QR code link
  4. Click "Start Camera"
  5. Open your browser's inspector window and look for the "/snapshot" request.
  6. Copy the "Fingerprint" and "Token" headers into the file below.
  7. Save prusaconnect_upload_cam.sh from below to /usr/local/bin/prusaconnect_upload_cam.sh and make it executable chmod +x /usr/local/bin/prusaconnect_upload_cam.sh.

Service

To run in the background, create /etc/systemd/system/prusaconnect_upload_cam.service and then start and enable it: systemctl enable --now prusaconnect_upload_cam.service.

[Unit]
Description=Raspi Camera to Prusa Connect
[Service]
ExecStart=/usr/local/bin/prusaconnect_upload_cam.sh
[Install]
WantedBy=multi-user.target
#!/bin/bash
# Set default values for environment variables
: "${HTTP_URL:=https://webcam.connect.prusa3d.com/c/snapshot}"
: "${DELAY_SECONDS:=10}"
: "${LONG_DELAY_SECONDS:=60}"
: "${FINGERPRINT:=<fingerprint>}"
: "${TOKEN:=<token>}"
while true; do
# Delete the previous image since libcamera-jpeg won't overwrite
rm -f /tmp/prusa_output.jpg 2> /dev/null
# Grab a frame from the Raspi Camera
libcamera-jpeg -o /tmp/prusa_output.jpg -q 80 --width 800 --height 600 -n
# 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 "@/tmp/prusa_output.jpg" \
--no-progress-meter \
--compressed
# Reset delay to the normal value
DELAY=$DELAY_SECONDS
else
echo "libcamera-jpeg returned an error. Retrying after ${LONG_DELAY_SECONDS}s..."
# Set delay to the longer value
DELAY=$LONG_DELAY_SECONDS
fi
sleep "$DELAY"
done
@jeepgod
Copy link

jeepgod commented Apr 11, 2024

I am having trouble getting this to work. I am not sure what I did wrong I have everything created and in the correct spot.

When I run systemctl enable --now prusaconnect_upload_cam.service it ask me for the password 3 times is that normal or did I miss something

@mandreko
Copy link
Author

@jeepgod Can you provide any additional information? I don't know what is prompting you for a password. Is it sudo, the actual service script somehow, or something else?

I'm happy to help, but I have no clue as of yet.

@jeepgod
Copy link

jeepgod commented Apr 22, 2024

I am new to doing this kind of thing. I used Nano to create prusaconnect_upload_cam.sh & prusaconnect_upload_cam.service in their correct directories. I copied each line one at a time and kept the spacing as best as I could. The Token and Fingerprint where swapped with correct information but I was unsure if I should keep < >. after doing all that I ran the commands listed above and got the follow requesting my password 3 times.

Screenshot 2024-04-21 at 6 55 44 PM

@mandreko
Copy link
Author

@jeepgod To run systemctl commands, you need to do them with sudo. Those are system-level scripts that need escalated privileges to run.

@stepman0
Copy link

stepman0 commented Jun 1, 2024

I tried to get this working with my Logitech USB webcam (already installed on printer), but libcamera-jpeg could not detect it. As a workaround, I use fswebcam which can be installed via sudo apt install -y fswebcam.

Just replace line 15 in the script above:

    # Grab a frame from the Raspi Camera
    #libcamera-jpeg -o /tmp/prusa_output.jpg -q 80 --width 800 --height 600 -n
    # Grab a frame from USB webcam
    fswebcam -r 800x600 --jpeg 80 -D 0 /tmp/prusa_output.jpg

This also on a x86 linux (e.g. LXC on Proxmox)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment