Prusa Connect RPi Camera via API
Intro Fluff
I was using RPi 4 to drive my MK3S+ printers, using the PrusaLink image, one of the great features of PrusaLink is the native support for USB cameras.
I upgraded my Prusa printers from MK3 to MK4, the surprise downside of the upgrade was that suddenly I had nowhere to plug my USB cameras into the Prusa MK4!
Fortunately, Prusa has enabled camera image upload via API.
Here are the steps I took
My code is available and hosted on GitHub.
prusa-connect-rpi-camera
Prusa Connect
Login to Prusa Connect
Navigate to your printer that you want to pair the camera with
Navigate to Camera
+ Add new other camera
Note the Token
, We will need that later
Raspberry Pi
Starting with fresh SD image of Bookworm
Update and Upgrade
sudo apt update
sudo apt upgrade -y
Install Python 3 and Pip
Python 3 should be pre-installed, but you may need to install pip for package management
sudo apt install python3-pip -y
Install git
sudo apt install git
Clone this repo
git clone https://github.com/nickjvturner/prusa-connect-rpi-camera
Navigate into the cloned directory
cd prusa-connect-rpi-camera
Create a Virtual Environment
python3 -m venv venv
Activate the Virtual Environment
source venv/bin/activate
Install OpenCV and uuidgen (apt)
sudo apt install python3-opencv -y
sudo apt-get install uuid-runtime
Install requests, OpenCV, Flask (pip3)
pip3 install requests
pip3 install opencv-python
pip3 install flask
Generate a UUID for the camera
The camera requires a UUID, we can generate one using uuidgen
uuidgen
Update secrets.py
Add the freshly created UUID into the secrets.py file
Add the token from earlier to secrets.py
Review code before execution
Quickly skim through:
pc-cam-local.py
pc-cam-uploader.py
Test pc-cam-local
You should manually execute/test these scripts, make sure you have activated the virtual environment venv
to avoid an error about Flask not being found.
python3 pc-cam-local.py
Check this is working, open up a browser, navigate to the IP address of the Raspberry Pi on port 5000
http://{IP_ADDRESS_OF_YOUR_RPI}:5000
You should see a simple web page displaying frames being captured by your camera.
IP Camera feed
http://{IP_ADDRESS_OF_YOUR_RPI}:5000/video_feed
This URL should serve you only the frames, not embedded within a web page.
IP Camera Viewer
This is the point where you could point an IP camera viewer app at the IP Camera feed URL. I am currently enjoying 'IP Cams'.
Test pc-cam-uploader
Back to the script testing, pc-cam-local.py should also be writing a .jpg frame to disk every 10 seconds. Check this file is present in your 'prusa-connect-rpi-camera' directory.
latest_frame.jpg
Manually execute pc-cam-uploader.py
python3 pc-cam-uploader.py
If everything is working Prusa Connect should receive the 'latest_frame.jpg'.
Configure scripts to run on boot
Create a service file for pc-cam-local.py
sudo nano /etc/systemd/system/pc-cam-local.service
[Unit]
Description=RPi Camera Local Web Server
After=network.target
[Service]
ExecStart=/home/pi/prusa-connect-rpi-camera/venv/bin/python /home/pi/prusa-connect-rpi-camera/pc-cam-local.py
WorkingDirectory=/home/pi/prusa-connect-rpi-camera
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
Create a service file for pc-cam-uploader.py
sudo nano /etc/systemd/system/pc-cam-uploader.service
[Unit]
Description=Prusa Connect RPi Camera image uploader
After=network.target
[Service]
ExecStart=/home/pi/prusa-connect-rpi-camera/venv/bin/python /home/pi/prusa-connect-rpi-camera/pc-cam-uploader.py
WorkingDirectory=/home/pi/prusa-connect-rpi-camera
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
Enable and start the services
sudo systemctl daemon-reload
sudo systemctl enable pc-cam-local
sudo systemctl start pc-cam-local
sudo systemctl enable pc-cam-uploader
sudo systemctl start pc-cam-uploader
Made changes?
If you git pull
and there are changes or you make local changes yourself, you need to stop the related service, reload systemctl daemon and restart the related service
sudo systemctl daemon-reload
sudo systemctl enable pc-cam-local
sudo systemctl restart pc-cam-local
sudo systemctl enable pc-cam-uploader
sudo systemctl restart pc-cam-uploader