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
@dmyers7
Copy link

dmyers7 commented Feb 15, 2024

hello I'm getting this error and I don't know how to deal with it. [video4linux2,v4l2 @ 0x5576955610] Cannot find a proper format for codec 'mjpeg' (id 7), pixel format 'none' (id -1) /dev/video0: Invalid argument

See my note above, did you install ffmeg & motion? The camera will not have the right codec to compress the images and will not appear as device video0 without them.

@hillsandales
Copy link

I am running a similar script that takes the webcam image from octoprint.

I'm finding that the script will work for a period of time, but when I look at Prusa Connect after a day or so, the image shows an error that it hasn't updated for over an hour, etc.

When I run systemctl status prusaconnect_upload_cam.service, I'm seeing the service is running, but there's output from the script showing a HTTP error, 503 Service Temporarily Unavailable from the curl command.

My thought is that occasionally the curl command cannot reach the Prusa Connect service for whatever reason, and this 503 HTML page is being sent back from nginx webserver.

I tried putting a --retry 10 in the curl command to get curl to retry up to 10 times. The error output from curl says it will retry in N seconds with N times remaining, but doesn't seem to actually do it.

Restarting the service fixes it, but I'm trying to find a method of making this reliable where it will attempt a new connection later.

The script waits a longer period of time if an error is returned from curl that is non-zero. However, the 503 HTTP error doesn't seem to make curl return non-zero, as it is returning a webpage HTML, just not the expected response.

@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