Skip to content

Instantly share code, notes, and snippets.

@cannikin
Forked from p123ad/README.md
Last active June 6, 2025 21:53
Show Gist options
  • Save cannikin/4954d050b72ff61ef0719c42922464e5 to your computer and use it in GitHub Desktop.
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
@Melkaz
Copy link

Melkaz commented Jun 6, 2025

Thanks for the snippets, it's very nice !

If you have an error like this when you check the logs of your service:

Jun 06 22:38:10 raspberrypi prusaconnect_upload_cam.sh[3694]: ERROR: *** failed to open file /tmp/output.jpg ***
Jun 06 22:38:10 raspberrypi prusaconnect_upload_cam.sh[3633]: libcamera-still returned an error, retrying after 30s...

You may have some permissions issues in the way your service is set.

Do this instead:

  1. Delete and edit your service file
    sudo rm /etc/systemd/system/prusaconnect_upload_cam.service && sudo nano /etc/systemd/system/prusaconnect_upload_cam.service

  2. Paste this, change your user name (often pi but can be something else) and save the file

[Unit]
Description=Raspi Cam to Prusa Connect
After=network.target

[Service]
ExecStart=/usr/local/bin/prusaconnect_upload_cam.sh
##################################
### CHANGE TO YOUR USERNAME BELOW  ###
##################################
User=your-username
Environment=XDG_RUNTIME_DIR=/run/user/1000
Restart=always

[Install]
WantedBy=multi-user.target
  1. Restart your service
sudo systemctl daemon-reload
sudo systemctl restart prusaconnect_upload_cam.service
  1. Check that it works correctly (you should see a 204 server response after a few seconds)
journalctl -u prusaconnect_upload_cam.service -f
  1. If it works, set the service to start at boot
sudo systemctl enable prusaconnect_upload_cam.service

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