viam-micro-server Development Setup
viam-micro-server
is the lightweight version of viam-server
which can run on resource-limited embedded systems (ESP32) that cannot run the fully-featured viam-server
.
This page provides instructions for configuring a development environment for working with viam-micro-server
, outlines the steps for performing various development tasks, and provides troubleshooting and development tips to help organize and streamline work.
Looking to install viam-micro-server?
If you only want to install and use viam-micro-server
, see Install viam-micro-server
instead.
The instructions below are for configuring a development environment in order to:
- Develop custom firmware which combines
viam-micro-server
with one or more modules. - Develop modules for
viam-micro-server
. - Develop
viam-micro-server
itself.
Required software
viam-micro-server
is written in Rust. To be able to develop for viam-micro-server
on macOS and Linux systems, you must install the following software on your development machine:
Install dependencies:
sudo apt-get install bison ccache cmake curl dfu-util flex git gperf libffi-dev libssl-dev libudev-dev libusb-1.0-0 ninja-build python3 python3-pip python3-venv wget
If you haven’t already, install Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then, install dependencies:
brew install cmake dfu-util ninja
If you do not yet have a Rust environment installed, install it with
rustup
.curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Once completed, start a new shell instance, or run
. "$HOME/.cargo/env"
in the current one.See the Rust Installation guide for more information and other installation methods.
Install
espup
and ESP development utilities withcargo
:cargo install cargo-espflash cargo-generate espflash espup ldproxy
Use
espup
to download and install the ESP Rust toolchain:espup install -s -f /dev/null -v 1.76.0
Note
Prior versions of the above espup
instructions created a file called export-rs.sh
which needed to be sourced before proceeding.
That requirement no longer applies for viam-micro-server
development.
The above command redirects espup
’s production of the setup script to /dev/null
to avoid cluttering the home directory.
If you would like to instead retain the setup script, replace /dev/null
in the above command with the location where you would like the script to be written, or remove the -f /dev/null
entirely and the file will be written to $HOME/export-esp.sh
by default.
Creating a project or module
With the dev setup for viam-micro-server
, you can:
- Create a project that combines
viam-micro-server
with one or more modules to produce a new firmware instance with expanded functionality. - Create modules that can integrate with
viam-micro-server
to deliver new opt-in functionality or device support. - Work on
viam-micro-server
itself, to add features and fix bugs, or to produce a build for a specific ESP-IDF version or platform for which Viam does not offer a pre-built solution.
Create a new machine and obtain its credentials:
Navigate to the Viam app and add a new machine. Click on the name of the machine to go to the machine’s page, then select the CONFIGURE tab.
Then select the part status dropdown to the right of your machine’s name on the top of the page and copy the Machine cloud credentials:
viam-micro-server
needs these credentials, which contains your machine part secret key and cloud app address, to connect to the Viam app.
Caution: Keep your API key and machine address safe
We strongly recommend that you add your API key and machine address as an environment variable. Anyone with these secrets can access your machine, and the computer running your machine.
Generate a new project skeleton from this template:
cargo generate --git https://github.com/viamrobotics/micro-rdk.git
Select
templates/project
when prompted. Give the project a name of your choice. Selectesp32
for MCU. If you wish to configure anesp32-camera
or afake
camera as a component of your machine, select true for include camera module and traits.You will be prompted to paste your machine’s
viam-server
robot JSON configuration into the terminal, which is the same thing as its machine cloud credentials. Paste in the credentials you obtained in step 1.Change directories into the generated project:
cd <your-path-to/your-project-directory>
If you wish to use version control for this project, this is the best time to initialize a git repository and commit all the generated files, but be sure to exclude the generated
viam.json
file, which includes secrets:git add . git restore viam.json git commit -m "initial commit"
Compile the project:
make build-esp32-bin
Please note that the first build may be fairly time consuming, as ESP-IDF must be cloned and built, and all dependent Rust crates must be fetched and built as well. Subsequent builds will be faster.
Upload the generated firmware to your ESP32:
Connect the ESP32 board you wish to flash to a USB port on your development machine, and run:
make flash-esp32-bin
When prompted, select the serial port that your ESP32 is connected to through a data cable.
If successful, you will retain a serial connection to the board until you press
Ctrl-C
. To manage this connection, consider running it within a dedicated terminal session, or undertmux
orscreen
. While the serial connection is live, you can also restart the currently flashed image withCtrl-R
.
Note
The above build and flash steps may be combined by using the upload
target:
make upload
Navigate to your new machine’s page on the Viam app. If successful, Live should be displayed underneath Last online.
You may now add any desired modules to the project by including them in the
dependencies
section of theCargo.toml
for the generated project. After adding (or removing) a module or changing the version of a module, you must rerun steps 5-6 above in order to rebuild the firmware and reflash the device.
If you have not previously developed a module for
viam-micro-server
, please review the module template README and the example module implementation walkthrough before continuing.Generate a new module skeleton from this template:
cargo generate --git https://github.com/viamrobotics/micro-rdk.git
Select
templates/module
when prompted, give the module a name of your choice, and answer any additional prompts.Change directories into the generated tree:
cd <your-path-to/your-module-directory>
If you wish to use version control for the module, this is the best time to initialize a git repository and commit all the generated files. There are no secrets in a newly generated module repository:
git add . git commit -m "initial commit"
Develop the module by defining
structs
which implement the necessarytraits
and adding tests and registration hooks for them, per the walkthrough.To consume the module, follow the “Create a Project” workflow in a different directory, and register your module in the
dependencies
section of the project’sCargo.toml
file, then build and flash the project. The module will now be available for use by adding it to your machine configuration on the Viam app.
Clone the
viamrobotics/micro-rdk
repository (optionally specifying a path for the new clone) andcd
into the clone:git clone https://github.com/viamrobotics/micro-rdk [path-to-new-clone] cd <your-path-to/your-viam-micro-server-clone>
Make any necessary additions or fixes.
Compile and run the project. The
viam-micro-server
can either be built for ESP32, or as a native version for testing purposes:
Compile the project for ESP32:
make build-esp32-bin
Please note that the first build may be fairly time consuming, as ESP-IDF must be cloned and built, and all dependent Rust crates must be fetched and built as well. Subsequent builds will be faster.
Next, flash the generated firmware to your ESP32.
Connect the ESP32 board you wish to flash to a USB port on your development machine, and run:
make flash-esp32-bin
When prompted, select the serial port that your ESP32 is connected to through a data cable.
The native server can be built and run with one command:
make native
For further details on viam-micro-server
development, including credentials management and developer productivity suggestions, please see the development technical notes page on GitHub.
Troubleshooting
Error: xtensa-esp32-elf-gcc: error: unrecognized command line option '--target=xtensa-esp32-espidf'
when building on macOS
This is caused by an upstream bug.
To work around this issue, ensure that CRATE_CC_NO_DEFAULTS=1
is set in the environment when building.
Error: Failed to open serial port
If you run into the error Failed to open serial port
when flashing your ESP32 with Linux, make sure the user is added to the group dialout
with sudo gpasswd -a $USER dialout
.
Error: espflash::timeout
If you get the following error while connecting to your ESP32:
`Error: espflash::timeout
× Error while connecting to device
╰─▶ Timeout while running command
Run the following command:
espflash flash --erase-parts nvs --partition-table partitions.csv target/xtensa-esp32-espidf/release/esp32-camera --baud 115200 && sleep 2 && espflash monitor
Try the connection command again. The baud rate on your device may not have been fast enough to connect. If successful, the Viam app will show that your machine part’s status is Live.
Error: viam.json
not found
If you get the error viam.json not found
try the following to manually add your machine cloud credentials as a file in your project:
- Navigate to your machine’s page on the Viam app and select the CONFIGURE tab.
- Select the part status dropdown to the right of your machine’s name on the top of the page:
- Click the copy icon underneath Machine cloud credentials.
viam-micro-server
needs this JSON, which contains your machine part secret key and cloud app address, to connect to the Viam app. - Navigate to the directory of the project you just created.
- Create a new
viam.json file and paste theviam-server
machine cloud credentials in. - Save the file.
Error: failed to run custom build command for esp32-explorer (/host)
This may occur for various reasons such as your machine cloud credentials, WiFi SSID, or password not being populated.
Check that your machine cloud credentials are provided in your project directory as
Error: invalid value 460800
for --before <BEFORE>
Change "-b"
to "-B
in the "-B"
is the Baudrate config.
Run the following commands to flash
espflash write-bin 0x0 target/esp32-server.bin -B 460800 && sleep 2 && espflash monitor
You can find additional assistance in the Troubleshooting section.
You can also ask questions in the Community Discord and we will be happy to help.
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!