Button API

The button API allows you to get button presses from your button components.

The button component supports the following methods:

Method NameDescriptionviam-micro-server Support
PushPush the button.
DoCommandExecute model-specific commands that are not otherwise defined by the component API.
CloseSafely shut down the resource and prevent further use.

Establish a connection

To get started using Viam’s SDKs to connect to and control your button and the rest of your machine, go to your machine’s page on the Viam app, Navigate to the CONNECT tab’s Code sample page, select your preferred programming language, and copy the sample code.

To show your machine’s API key in the sample code, toggle Include API key.

When executed, this sample code creates a connection to your machine as a client.

The following examples assume you have an button called "my_button" configured as a component of your machine. If your button has a different name, change the name in the code.

Import the button package for the SDK you are using:

import (
  "go.viam.com/rdk/components/button"
)

API

Push

Push the button.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

Parameters:

  • extra (None) (optional)
  • callOptions (CallOptions) (optional)

Returns:

  • (Promise)

For more information, see the TypeScript SDK Docs.

DoCommand

Execute model-specific commands that are not otherwise defined by the component API. For built-in models, model-specific commands are covered with each model’s documentation. If you are implementing your own button and add features that have no built-in API method, you can access them with DoCommand.

Parameters:

Returns:

Example:

myButton, err := button.FromRobot(machine, "my_button")

command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := myButton.DoCommand(context.Background(), command)

For more information, see the Go SDK Docs.

Parameters:

  • command (Struct) (required): The command to execute.
  • callOptions (CallOptions) (optional)

Returns:

For more information, see the TypeScript SDK Docs.

Close

Safely shut down the resource and prevent further use.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.

Returns:

  • (error): An error, if one occurred.

Example:

myButton, err := button.FromRobot(machine, "my_button")

err = myButton.Close(context.Background())

For more information, see the Go SDK Docs.