Previous
Create custom modules
Part 1 of 5 | ⏱️ 15-20 minutes
In this tutorial series, you’ll learn how to create a custom module to add support for hardware that isn’t already supported by existing registry modules.
Throughout this tutorial series, you’ll create a hello-world module that demonstrates two common module capabilities:
This example shows how to implement multiple resources within a single module.
Before you begin, make sure you have the following:
The Viam CLI is required to generate module code and upload modules to the registry.
Install the Viam CLI on your development machine:
To download the Viam CLI on a macOS computer, install brew and run the following commands:
brew tap viamrobotics/brews
brew install viam
To download the Viam CLI on a Linux computer with the aarch64 architecture, run the following commands:
sudo curl -o /usr/local/bin/viam https://storage.googleapis.com/packages.viam.com/apps/viam-cli/viam-cli-stable-linux-arm64
sudo chmod a+rx /usr/local/bin/viam
To download the Viam CLI on a Linux computer with the amd64 (Intel x86_64) architecture, run the following commands:
sudo curl -o /usr/local/bin/viam https://storage.googleapis.com/packages.viam.com/apps/viam-cli/viam-cli-stable-linux-amd64
sudo chmod a+rx /usr/local/bin/viam
You can also install the Viam CLI using brew on Linux amd64 (Intel x86_64):
brew tap viamrobotics/brews
brew install viam
Download the binary and run it directly to use the Viam CLI on a Windows computer.
If you have Go installed, you can build the Viam CLI directly from source using the go install command:
go install go.viam.com/rdk/cli/viam@latest
To confirm viam is installed and ready to use, issue the viam command from your terminal.
If you see help instructions, everything is correctly installed.
If you do not see help instructions, add your local PATH variable.
If you use bash as your shell, you can use the following command:
echo 'export PATH="$HOME/go/bin:$PATH"' >> ~/.bashrc
For more information see install the Viam CLI.
Verify installation:
viam version
You should see output like: viam version 0.x.x
Authenticate to Viam:
viam login
This will open a new browser window with a prompt to start the authentication process. If a browser window does not open, the CLI will present a URL for you to manually open in your browser. Follow the instructions to complete the authentication process.
Use your organization, location, or machine part API key and corresponding API key ID in the following command:
viam login api-key --key-id <api-key-id> --key <organization-api-key-secret>
While you can develop a module without a machine, you’ll need one to test your module.
Add a new machine on Viam.
On the machine’s page, follow the setup instructions to install viam-server on the computer you’re using for your project.
Wait until your machine has successfully connected to Viam.
Already have a machine? Great! You’ll configure it in Part 4.
Your module can be written in Python or Go. Choose your preferred language and verify your environment:
Required: Python 3.11 or newer
Check your version:
python3 --version
You should see: Python 3.11.x or higher
Need to install Python 3.11+?
brew install python@3.11sudo apt install python3.11Required: Go 1.20 or newer
Check your version:
go version
You should see: go version go1.20 or higher
Need to install Go? Download from go.dev
Before writing a module, it’s helpful to write a simple test script to verify you can control your hardware.
For the example module, this script opens an image file and prints a random number.
Create a file named test.py:
import random
from PIL import Image
# Open an image
img = Image.open("example.png")
img.show()
# Return a random number
random_number = random.random()
print(random_number)
Test it:
python3 test.py
Create a file named test.go:
package main
import (
"fmt"
"math/rand"
"os"
)
func main() {
// Open an image
imgFile, err := os.Open("example.png")
if err != nil {
fmt.Printf("Error opening image file: %v\n", err)
return
}
defer imgFile.Close()
imgByte, err := os.ReadFile("example.png")
fmt.Printf("Image file type: %T\n", imgByte)
if err != nil {
fmt.Printf("Error reading image file: %v\n", err)
return
}
// Return a random number
number := rand.Float64()
fmt.Printf("Random number: %f\n", number)
}
Test it:
go run test.go
For your own hardware, replace this test script with code that uses the manufacturer’s API or SDK to control your device.
A module is essentially a packaged wrapper around your hardware control code. The module:
viam-server when it startsGetImages for cameras)In Part 2, you’ll choose which Viam API best matches your hardware’s capabilities.
Environment ready:
Understanding:
Now that your environment is ready, continue to Part 2: Choose an API and generate code to select the right API for your hardware and generate your module structure.
Tutorial navigation:
Was this page helpful?
Glad to hear it! If you have any other feedback please let us know:
We're sorry about that. To help us improve, please tell us what we can do better:
Thank you!