Skip to content

Instantly share code, notes, and snippets.

@cannikin
Forked from p123ad/README.md
Last active June 11, 2024 20:40
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 start it automatically.

  1. Create /etc/systemd/system/prusaconnect_upload_cam.service and paste the content from below.
  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: 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
libcamera-still -v 0 --immediate --width 2274 --height 1280 -q 80 -o output.jpg
# 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: $CAMERA_TOKEN" \
--data-binary "@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
@Joshmandi
Copy link

For anyone curious, this works with Raspbian Lite on a Raspberry Pi Model 3 B+.

Additionally, I ended up installing fswebcam with:

sudo apt-get install fswebcam

and replacing the libcamera-still line within prusaconnect_upload_cam.sh:

libcamera-still -v 0 --immediate --width 2274 --height 1280 -q 80 -o output.jpg

with the following line:

fswebcam -r 2274x1280 --no-banner output.jpg

I haven't combed through the fswebcam documentation enough to see if I could mimic the 80% quality adjustment, but I'm sure that if someone gets curious they can find it.

@TheDracoArt
Copy link

thank you so much i was trying to get some sort of ip cam running an my pi zero 2 and was on the verge of quitting.
than i found this guide and that exexly what i want to do for my printer.

it might be more beginner frindly to include the sudo nano part in the guide to create the script in the shell :)

@cannikin
Copy link
Author

it might be more beginner frindly to include the sudo nano part in the guide to create the script in the shell :)

Good call, updated!

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