Prusa Connect Camera Server Using Viam
by michaellee1019 in Workshop > 3D Printing
1118 Views, 0 Favorites, 0 Comments
Prusa Connect Camera Server Using Viam
I have transitioned from using OctoPrint to Prusa Connect for all of my printers. Overall I have found the integration better for me as I only have Prusa printers and the Prusa's integration is seamless. One main disadvantage is being able to observe the printers with a camera.
Prusa's out of the box camera support didn't suit my needs. Currently Prusa Connect only supports using a phone as a camera across all their printer models. If you happen to have an older MK model (MK2.5, MK2.5S, MK3, MK3S, MK3S+.) you could use the RaspberryPi that is hosting PrusaLink + Prusa Connect, but you cannot use this method for the XL, Mini, or MK4 models.
So I set out to solve this problem by making a service that runs on Raspberry PI using Viam. Viam has built-in support for various camera types, automatically detects and configures cameras, and supports multiple cameras connected at the same time. If you have ever tried to setup your own camera streaming server on a Raspberry Pi, you know how frustrating this can be, and Viam makes it much easier.
The service I wrote takes snapshots from the camera and uploads the images to the Prusa Connect Camera API. At this time, Prusa Connect does not support video streaming. Their UI auto-refreshes images every 10 seconds which is reasonable enough to monitor and detect print failures.
Supplies
- A Raspberry Pi 4 B or higher model
- 5v USB-C charger that supports Raspberry PIs. I prefer the CanaKit 3.5A Raspberry Pi 4 Power Supply (USB-C).
- Any number of webcams, CSI cameras, Mac Laptops, iPhones that you plan to use for this project. In Step 1, I will outline how to use each camera.
- Signup for Prusa Connect*
- Signup for Viam*
At the time of writing, Prusa Connect is a free service and is in beta. Viam does not require a credit card to signup and provides a $5 free tier per month. You will incur some charges occasionally downloading modules to machines, but only costs cents per download.
Determine Camera Setup
In this project you can use any camera that is supported by Viam, either a built-in component that is supported by the company or any of the community developed modules as part of the Viam Registry.
I recommend the following options:
- Any USB Web camera
- A Camera Serial Interface (CSI) Module using Viam's CSI Modular Component.
- Run Viam on any Mac and use the embedded camera on it to monitor your printer. Use Continuity Camera to access the cameras on your iPhone, iPad for the snapshots.
Connect Your Camers
This is pretty straight forward for USB Cameras. One note is that I confirmed that you can have more than 4 cameras if you want, and I've found that the Anker 4-Port USB 3.0 Hub. This is great for printer farms where you can technically connect 16 cameras across the 4 USB ports. Note that I've only tried up to 6 cameras myself.
For CSI Cameras, you can follow this video on proper handling and installation onto the raspberry pi.
Imaging Your Raspberry Pi
There are many ways to image Raspberry Pis. For this project I recommend following Viam's Setup Guide for Raspberry Pi.
After following this tutorial you should have a raspberry pi setup and connected to a Viam Machine.
Configure Your Viam Machine
With a few clicks you can configure as many cameras as you have connected into Viam:
- Navigate to the Config tab of your machine.
- On the Components tab, click + Add Component.
- In the list chose Camera.
- Click on the model of camera you have connected to your pi:
- webcam for a USB Camera, Mac camera, or a camera accessible by Continuity Camera.
- csi-pi for a CSI Module
- Or choose some other model based on any custom camera setup you have.
- If you chose a camera model from a module, you will be shown a display about the module and need to click Add Module to proceed.
- Give the camera component a name that means something to you. For example "mk4_overhead". You will use this when configuring the camera service later.
- Click Create.
- Configure the Camera.
- For webcamera, Viam will automatically detect the connected cameras and provide a dropdown list for you under the video path attribute. The rest of the attributes are optional and I recommend not changing them until you have confirmed that your camera stream appears by following the rest of the instructions. If your camera does not appear in the list, its likely the camera is not functional or its drivers are not supported on Raspberry Pi OS.
- For CSI Camera, you will need to populate JSON to configure the camera. Example configuration is located on the module's readme page.
- Click Save config to update your machine.
- Switch to the Control tab and preview your cameras. Confirm the camera shows up and the stream is viewable.
- If you are having problems with the camera, check the Logs tab and see if there are any useful messages about the camera failing to initialize.
Repeat for as many cameras as you have attached to your Pi.
Configure the Prusa Connect Camera Server Module
The last step is to configure the camera server module that I have created. First you need to login to connect.prusa.com and create camera instances for each of your printers:
- Go to the printer's detail page.
- Click on the Camera tab.
- Click on "+ Add new other camera".
- Click the Pencil to give your camera a name.
- Take note of the fingerprint that is listed as you will use it later. Note that you should consider the fingerprint as a password and not post it or share it publicly.
On Viam, follow these steps:
- Go to the Config tab of your machine.
- On the Components tab, click + Add Component.
- Search for prusa_connect:camera_server.
- Click Add Module.
- Give the component a name like Camera Server and click Create.
You will be provided with a textbox to add JSON to configure the server. I have provided examples on the module readme for how to populate the config. You should end up with an attribute JSON like the following:
{
"attributes": {
"cameras_config": {
"prusaxl1": {
"fingerprint": "usb-046d_C922_Pro_Stream_Webcam_F684FE8F-video-index0",
"token": "<secret from connect.prusa.com>"
},
"prusaxl2": {
"fingerprint": "usb-046d_HD_Pro_Webcam_C920_79AA2B6F-video-index0",
"token": "<secret from connect.prusa.com>"
}
}
},
Note: Once you set your fingerprint it cannot be changed. If you forget the value, you can look through your old config on the History tab of your machine. Or you delete and create a new camera on connect.prusa.com.
Note: You need to add every camera listed in attributes as part of the Depends On field, otherwise the module will not be able to pull camera images from it.
See Images in Prusa Connect
You should now see images from your cameras in Prusa Connect. If images are not showing up, check your robot logs for any error messages happening within Viam or as a response to the Prusa Connect API.