Skip to content

Instantly share code, notes, and snippets.

@cannikin
Forked from p123ad/README.md
Last active December 4, 2025 03:58
Show Gist options
  • Select an option

  • Save cannikin/4954d050b72ff61ef0719c42922464e5 to your computer and use it in GitHub Desktop.

Select an option

Save cannikin/4954d050b72ff61ef0719c42922464e5 to your computer and use it in GitHub Desktop.
Use Raspberry Pi Camera with Prusa Connect

Use Raspberry Pi and Pi Cam for Prusa Connect

I couldn't get the script from p123ad to work on my Pi Zero W 2 with Camera Module 3 (all kinds of ffmpeg errors). There are several built-in tools for working with the camera now, so I tried to figure out if I could use one of those instead.

Behold this version, which uses the built-in libcamera-still tool to actually interact with the camera and save a JPEG. That image is then uploaded to Prusa Connect, same as the original script.

Instructions

  1. Go to the Cameras section at https://connect.prusa3d.com
  2. Add a new camera by clicking "Add new other camera"
  3. Copy the generated Token
  4. Set up your Pi Zero W 2 with Raspian OS Lite (32-bit) (this may work with other combinations of Pi and OS but I haven't tested)
  5. Log into your Pi and create a shell script with sudo nano /usr/local/bin/prusaconnect_upload_cam.sh and swap out connect-token-here in the script example with the Token you copied in step 3
  6. Change ownership of the script to the user you log into your pi with, for example if your user is pi then run: sudo chown pi:pi /usr/local/bin/prusaconnect_upload_cam.sh
  7. Make the script executable: chmod +x /usr/local/bin/prusaconnect_upload_cam.sh
  8. Start the script with /usr/local/bin/prusaconnect_upload_cam.sh

If it works you should see no error messages, and a new image appearing in Prusa Connect every 10 seconds.

Create Autostart Service

To run the script in the background and have it start automatically when your Pi starts:

  1. Create the service file with sudo nano /etc/systemd/system/prusaconnect_upload_cam.service, paste the content from below and save.
  2. Start the service: sudo systemctl start prusaconnect_upload_cam.service.
  3. Check if the service is running with sudo systemctl status prusaconnect_upload_cam.service.
  4. Enable the service to run at startup: sudo systemctl enable prusaconnect_upload_cam.service.
[Unit]
Description=Raspi Cam 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://connect.prusa3d.com/c/snapshot}"
: "${DELAY_SECONDS:=10}"
: "${LONG_DELAY_SECONDS:=60}"
# FINGERPRINT can be a random string with at least 16 characters
: "${FINGERPRINT:=123456789012345678}"
# CAMERA_TOKEN generated by the Connect server
: "${CAMERA_TOKEN:=connect-token-here}"
while true; do
# Grab a frame from libcamera-still with the highest resolution
# that is displayed on Prusa Connect: 1704 x 1278 for a 4:3 image
# Setting the quality to 80 saves almost 50% in file size for
# very little decrease in quality. Set to taste!
# If you need to rotate the image 180° add `--rotate 180`
# (One user reported needing to make this `--camera.rotate 180` instead!)
libcamera-still -v 0 --immediate --width 2274 --height 1280 -q 80 -o /tmp/output.jpg
# If no error, upload it.
if [ $? -eq 0 ]; then
# POST the image to the HTTP URL using curl
curl -k -X PUT "$HTTP_URL" \
-H "accept: */*" \
-H "content-type: image/jpg" \
-H "fingerprint: $FINGERPRINT" \
-H "token: $CAMERA_TOKEN" \
--data-binary "@/tmp/output.jpg" \
--no-progress-meter \
--compressed
# Reset delay to the normal value
DELAY=$DELAY_SECONDS
else
echo "libcamera-still returned an error, retrying after ${LONG_DELAY_SECONDS}s..."
# Set delay to the longer value
DELAY=$LONG_DELAY_SECONDS
fi
sleep "$DELAY"
done
@alxwolf
Copy link

alxwolf commented Nov 23, 2025

Thank you @cannikin!

With Trixie, libcamera is no longer on board but replaced by rpicam.

Corresponding command is:

rpicam-still \
        --timeout 1 \
        --width 2274 --height 1280 \
        --quality 80 \
        --nopreview \
        -o /tmp/output.jpg

@alxwolf
Copy link

alxwolf commented Nov 23, 2025

For convenience, the full script (and I added to configure the name of cam in Prusa Connect):

#!/bin/bash

# Set default values for environment variables
: "${HTTP_URL:=https://connect.prusa3d.com/c/snapshot}"
: "${DELAY_SECONDS:=10}"
: "${LONG_DELAY_SECONDS:=60}"
# FINGERPRINT can be a random string with at least 16 characters
: "${FINGERPRINT:=1234567812345678}"
# CAMERA_TOKEN generated by the Connect server
: "${CAMERA_TOKEN:=connect-token-here}"
# CAMERA_NAME to show in Prusa Connect
: "${HTTP_INFO_URL:=https://connect.prusa3d.com/c/info}"
: "${CAMERA_NAME:=RPi Cam Prusa Mini+}"

# Set camera name once
curl -k -X PUT "$HTTP_INFO_URL" \
  -H "accept: application/json" \
  -H "content-type: application/json" \
  -H "fingerprint: $FINGERPRINT" \
  -H "token: $CAMERA_TOKEN" \
  --data "{\"config\": { \"name\": \"$CAMERA_NAME\" }}"

while true; do
    # Grab a frame from rpicam-still with the highest resolution
    # that is displayed on Prusa Connect: 1704 x 1278 for a 4:3 image
    # Setting the quality to 80 saves almost 50% in file size for
    # very little decrease in quality. Set to taste!
    # If you need to rotate the image 180° add `--rotate 180`
    # (One user reported needing to make this `--camera.rotate 180` instead!)

	rpicam-still \
        	--timeout 1 \
        	--width 1704 --height 1278 \
        	--quality 80 \
		--rotation 180 \
        	--nopreview \
        	-o /tmp/output.jpg

    # If no error, upload it.
    if [ $? -eq 0 ]; then
        # POST the image to the HTTP URL using curl
        curl -k -X PUT "$HTTP_URL" \
            -H "accept: */*" \
            -H "content-type: image/jpg" \
            -H "fingerprint: $FINGERPRINT" \
            -H "token: $CAMERA_TOKEN" \
            --data-binary "@/tmp/output.jpg" \
            --no-progress-meter \
            --compressed

        # Reset delay to the normal value
        DELAY=$DELAY_SECONDS
    else
        echo "rpicam-still returned an error, retrying after ${LONG_DELAY_SECONDS}s..."

        # Set delay to the longer value
        DELAY=$LONG_DELAY_SECONDS
    fi

    sleep "$DELAY"
done

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