Skip to content

Instantly share code, notes, and snippets.

@p123ad
Last active February 28, 2024 02:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save p123ad/5b1482200715d834e9db736fa9e187d0 to your computer and use it in GitHub Desktop.
Save p123ad/5b1482200715d834e9db736fa9e187d0 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

Made this for my PRUSA MINI with a Raspberry Pi Zero 2W and a Raspi Cam v1.3

Based on the great instructions from Nunos and Joltcans

3

Instructions

  1. Go to the Cameras section at https://connect.prusa3d.com
  2. Add a new camera "Add new other camera".
  3. Copy the generated Token
  4. Set up your Raspberry Pi with simpe Raspian OS and enable the camera with raspi-config.
  5. Create a shell script prusaconnect_upload_cam.sh and paste the Token into the file below.

Test

  1. 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.
  2. Start the script with ./usr/local/bin/prusaconnect_upload_cam.sh

If It works you should see the images appearing in Prusa Connect every 10 seconds.

1

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
# Script is from here
# https://gist.github.com/moritzmhmk/48e5ed9c4baa5557422f16983900ca95
# https://gist.github.com/nunofgs/84861ee453254823be6b069ebbce9ad2
# 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:=put the token here}"
while true; do
# Grab a frame from the Raspi Cam using FFmpeg, -video_size 1280x720
ffmpeg \
-y \
-f video4linux2 \
-input_format mjpeg \
-video_size 1280x720 \
-i /dev/video0 \
-vframes 1 \
-filter:v "vflip" \
-f mjpeg \
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 "FFmpeg returned an error. Retrying after ${LONG_DELAY_SECONDS}s..."
# Set delay to the longer value
DELAY=$LONG_DELAY_SECONDS
fi
sleep "$DELAY"
done
@cannikin
Copy link

cannikin commented Feb 28, 2024

I couldn't get ffmpeg to work with /dev/video0 on my Pi Zero W 2 and Camera Module 3, but I found an even simpler way using the built-in libcamera-still utility, no ffmpeg or legacy camera support needed! https://gist.github.com/cannikin/4954d050b72ff61ef0719c42922464e5

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