Create ThingsBoard Devices and Return their Access Tokens
Source:R/push_to_thingsboard.R
tb_setup_devices.RdConvenience wrapper for the initial setup against a fresh ThingsBoard tenant. Authenticates with either a username/password login (JWT – works on every ThingsBoard edition, required for self-hosted Community Edition) or an account-level API key (ThingsBoard Cloud), then:
Create one device per name (or fetch the device if it already exists),
Read each device's access token via the credentials endpoint.
The returned named character vector can be fed directly into
tb_push_station_telemetry as device_token.
Usage
tb_setup_devices(
station_ids,
api_key = Sys.getenv("TB_API_KEY"),
host = tb_default_host(),
name_prefix = "wasserportal-",
device_type = "wasserportal",
username = Sys.getenv("TB_USERNAME"),
password = Sys.getenv("TB_PASSWORD")
)Arguments
- station_ids
character vector of Wasserportal
Messstellennummervalues. Each becomes a ThingsBoard device namedpaste0(name_prefix, station_id).- api_key
account-level API key (ThingsBoard Cloud only), generated under Account > Security > API keys > Generate. Sent in the
X-Authorization: ApiKey <key>request header. Defaults to env varTB_API_KEY. Ignored whenusernameandpasswordare supplied.- host
base URL of the ThingsBoard instance, without trailing slash. Defaults to env var
TB_HOSTif set, otherwise"https://thingsboard.cloud". Use"https://eu.thingsboard.cloud"for the EU cloud or e.g."https://dashboards.inowas.org"for a self-hosted instance.- name_prefix
prefix added in front of every station id when forming the ThingsBoard device name. Default
"wasserportal-".- device_type
ThingsBoard device profile / type. Default
"wasserportal". The profile is auto-created on first use.- username
ThingsBoard user for the username/password (JWT) login. Defaults to env var
TB_USERNAME. When set together withpasswordit takes precedence overapi_key– this is the route to use for self-hosted Community Edition.- password
ThingsBoard password. Defaults to env var
TB_PASSWORD.
Details
Set TB_USERNAME + TB_PASSWORD for the login route, or generate an API
key in the ThingsBoard Cloud UI under
Account > Security > API keys > Generate and set TB_API_KEY.
Examples
if (FALSE) { # \dontrun{
# Self-hosted ThingsBoard Community Edition (username/password login):
Sys.setenv(
TB_HOST = "https://dashboards.inowas.org",
TB_USERNAME = "me@example.org",
TB_PASSWORD = "secret"
)
tokens <- tb_setup_devices(c("149", "5867000", "5803900"))
# ThingsBoard Cloud (account API key):
Sys.setenv(
TB_HOST = "https://eu.thingsboard.cloud",
TB_API_KEY = "<paste-your-API-key-here>"
)
tokens <- tb_setup_devices(c("149", "5867000", "5803900"))
} # }