Skip to content

Instantly share code, notes, and snippets.

@PetrFsX
Forked from p123ad/README.md
Created July 25, 2024 18:26
Show Gist options
  • Select an option

  • Save PetrFsX/04ecfc327e2575e896f2f6294fadfa26 to your computer and use it in GitHub Desktop.

Select an option

Save PetrFsX/04ecfc327e2575e896f2f6294fadfa26 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment