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

Is this also possible if Octoprint is installed on the Raspberry ? If so, how exactly would I have to proceed? Following this guide here I can't get it to run. I always get errors with the fad labeling, I always have to omit urs/local/bin so that I can execute it. I can't get the autostart to run at all. I would appreciate your help.

@p123ad
Copy link
Author

p123ad commented Jan 15, 2024

Hey,
I made this because I wanted a solution without Octoprint. If you are already using Octoprint, you can integrate your raspi cam for example with this guide (https://all3dp.com/2/octoprint-webcam-setup-camera/).

Please check in which folder you currently are. If you are in /usr/local/bin/ you can directly start the script from here ./prusaconnect_upload_cam.sh

@Mario9274
Copy link

Yes, the cam via Octoprint is not a problem. but would integrate the cam gene into Prusa Connect at the same time. Can you explain to me how I check which folder I'm in? and how exactly do I create the autostart service?

@p123ad
Copy link
Author

p123ad commented Jan 21, 2024

I don't know if there will be problems if you try to access the camera from Octoprint and with this custom script. Maybe there could be some conflicts.

Can you explain to me how I check which folder I'm in?

You can check the current directory with pwd

and how exactly do I create the autostart service?

There are different ways to achive to start a script automatically after booting.
The method here is called "systemd service". You can google it to get better explanations.

  1. Go to the systemd folder with cd /etc/systemd/system/
  2. Create the service description file with sudo touch prusaconnect_upload_cam.service
  3. Paste the content from below and save the file.
[Unit]
Description=Raspi Cam to Prusa Connect

[Service]
ExecStart=/usr/local/bin/prusaconnect_upload_cam.sh

[Install]
WantedBy=multi-user.target
  1. Start the service with sudo systemctl start prusaconnect_upload_cam.service. Now the script should start. And you an check if it is working in PrusaConnect.
  2. You can additionally check if the service is running with sudo systemctl status prusaconnect_upload_cam.service.
  3. If everything works fine you can "enable" the service to start automatically after system boot sudo systemctl enable prusaconnect_upload_cam.service. You can check if it works when you restart your raspi with sudo reboot.

I hope this helps a little bit.

@dmyers7
Copy link

dmyers7 commented Jan 25, 2024

Thank you for the great instructions. If you install the light version of Pi OS you will need to apt get ffmeg, enable legacy camera support in rasp config, apt install motion (see: https://raspberrytips.com/raspberry-pi-camera-as-webcam/) and then your pi camera will support /dev/video0 as described in the script. Depending on the camera you end up using, you may also need to change the resolution in the script for ffmpeg encoding.

You may also need / want to add the ability to rotate the camera image by adding the following to the front end of the (.sh) python script around line 15 ahead of the first "while" statement:

v4l2-ctl -c rotate=180
or
v412-ctl -c horizontal_flip=1

@David-Tru
Copy link

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

@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