Getting Connected
ThingsBoard acts as a standard MQTT broker. Any MQTT v3.1, v3.1.1, or v5.0 client can connect — firmware libraries (Paho, MQTT.js, arduino-mqtt), integration tools, or the mosquitto command-line client used in examples throughout this section.
Connection Parameters
Section titled “Connection Parameters”| Parameter | Value |
|---|---|
| Host | Your ThingsBoard hostname |
| Port | 1883 (plain) · 8883 (TLS) |
| Protocol | MQTT v3.1, v3.1.1, v5.0 |
| QoS | 0 (at most once) · 1 (at least once) |
Command-Line Tools
Section titled “Command-Line Tools”Examples in this section use mosquitto_pub and mosquitto_sub. Install the mosquitto client package for your OS from mosquitto.org/download or your system package manager, then verify with mosquitto_pub --version.
Credential Types
Section titled “Credential Types”Choose the credential type that fits your device’s security requirements. Configure it per device under Manage credentials — see Devices.
| Type | MQTT Username | MQTT Password | MQTT Client ID |
|---|---|---|---|
| Access Token | Device access token | (empty) | (any) |
| MQTT Basic | Custom username (optional) | Custom password (optional) | Custom client ID (optional) |
| X.509 Certificate | (empty) | (empty) | (any) — mutual TLS only |
Access Token
Section titled “Access Token”The simplest credential type. The token acts as the MQTT username; no password is required.
Plain MQTT (port 1883):
mosquitto_pub -d -q 1 -h "mqtt.thingsboard.cloud" -p 1883 -t "v2/t" -u "$ACCESS_TOKEN" -m '{"temperature": 25}'MQTT over TLS (port 8883) — one-way SSL:
The client verifies the server’s identity using a CA root certificate. Download ca-root.pem from the Check connectivity dialog on the device page.
mosquitto_pub -d -q 1 --cafile ca-root.pem -h "mqtt.thingsboard.cloud" -p 8883 -t "v2/t" -u "$ACCESS_TOKEN" -m '{"temperature": 25}'MQTT Basic
Section titled “MQTT Basic”Replaces the access token with a custom clientId, username, and password. All three fields are optional — use whichever combination your firmware supports.
| Authentication scheme | Required fields |
|---|---|
| Client ID only | clientId |
| Username + Password | username, password |
| All three combined | clientId, username, password |
Plain MQTT (port 1883):
mosquitto_pub -d -q 1 -h "mqtt.thingsboard.cloud" -p 1883 -t "v2/t" -i "$CLIENT_ID" -u "$USERNAME" -P "$PASSWORD" -m '{"temperature": 25}'MQTT over TLS (port 8883) — one-way SSL:
mosquitto_pub -d -q 1 --cafile ca-root.pem -h "mqtt.thingsboard.cloud" -p 8883 -t "v2/t" -i "$CLIENT_ID" -u "$USERNAME" -P "$PASSWORD" -m '{"temperature": 25}'X.509 Certificate
Section titled “X.509 Certificate”Mutual TLS: both the server and the device authenticate each other with certificates. Always uses port 8883. Two strategies are available.
Individual Certificate
Section titled “Individual Certificate”Each device has its own certificate registered directly on the device record.
- Generate a certificate and private key:
Terminal window # RSAopenssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 365 -nodes# or EC (smaller, faster)openssl ecparam -out key.pem -name prime256v1 -genkeyopenssl req -new -key key.pem -x509 -nodes -days 365 -out cert.pem - Register the certificate in ThingsBoard: Entities → Devices → [device] → Manage credentials → X.509 Certificate, paste
cert.pem, save. - Connect using the certificate and key:
Terminal window mosquitto_pub --cafile ca-root.pem -d -q 1 -h "mqtt.thingsboard.cloud" -p 8883 -t "v2/t" --key key.pem --cert cert.pem -m '{"temperature": 25}'
Certificate Chain (Recommended for fleets)
Section titled “Certificate Chain (Recommended for fleets)”Register one intermediate CA certificate in the device profile instead of one certificate per device. ThingsBoard extracts the device name from the certificate’s Common Name using a configurable regex and can auto-provision new devices when their certificate is first seen.
- Generate the certificate hierarchy (root → intermediate → device):
Terminal window # Root CAopenssl req -x509 -newkey rsa:4096 -keyout rootKey.pem -out rootCert.pem -sha256 -days 365 -nodes# Intermediate CAopenssl req -new -newkey rsa:4096 -keyout intermediateKey.pem -out intermediate.csr -sha256 -nodesecho basicConstraints=CA:TRUE > ca-ext.txtopenssl x509 -req -in intermediate.csr -out intermediateCert.pem -CA rootCert.pem -CAkey rootKey.pem -days 365 -sha256 -CAcreateserial -extfile ca-ext.txt# Device certificateopenssl req -new -newkey rsa:4096 -keyout deviceKey.pem -out device.csr -sha256 -nodesopenssl x509 -req -in device.csr -out deviceCert.pem -CA intermediateCert.pem -CAkey intermediateKey.pem -days 365 -sha256 -CAcreateserial# Build the chain sent by the devicecat deviceCert.pem intermediateCert.pem rootCert.pem > chain.pem - Register the intermediate certificate in ThingsBoard: Profiles → Device profiles → [profile] → Device Provisioning → X.509 Certificate Chain. Paste
intermediateCert.pem, configure the CN regex for device name extraction, and optionally enable auto-provisioning. - Connect using the chain and device key:
Terminal window mosquitto_pub --cafile ca-root.pem -d -q 1 -h "mqtt.thingsboard.cloud" -p 8883 -t "v2/t" --key deviceKey.pem --cert chain.pem -m '{"temperature": 25}'
MQTT over TLS
Section titled “MQTT over TLS”ThingsBoard Cloud already has TLS configured with a valid certificate. Connect on port 8883 and pass the system CA bundle (or your OS trust store) — no additional server-side setup is needed.
Topic Formats
Section titled “Topic Formats”ThingsBoard supports two MQTT topic formats. Both are equivalent — they accept the same payloads and produce the same result.
| Format | Prefix | Example (telemetry) |
|---|---|---|
| Short | v2/ | v2/t |
| Standard | v1/devices/me/ | v1/devices/me/telemetry |
Short topics are available since ThingsBoard 3.5. They reduce bandwidth for constrained devices and support an optional serialization suffix: /j for JSON or /p for Protobuf. Without a suffix, the format configured in the device profile is used.
Complete topic mapping:
| Operation | Short | Standard |
|---|---|---|
| Publish telemetry | v2/t | v1/devices/me/telemetry |
| Publish client attributes | v2/a | v1/devices/me/attributes |
| Subscribe to shared attribute updates | v2/a | v1/devices/me/attributes |
| Request attribute values | v2/a/req/$id | v1/devices/me/attributes/request/$id |
| Receive attribute response | v2/a/res/+ | v1/devices/me/attributes/response/+ |
| Subscribe to server-side RPC | v2/r/req/+ | v1/devices/me/rpc/request/+ |
| Respond to server-side RPC | v2/r/res/$id | v1/devices/me/rpc/response/$id |
| Send client-side RPC request | v2/r/req/$id | v1/devices/me/rpc/request/$id |
| Receive client-side RPC response | v2/r/res/+ | v1/devices/me/rpc/response/+ |
| Publish claim request | v2/c | v1/devices/me/claim |
MQTT Transport Configuration
Section titled “MQTT Transport Configuration”When you set the transport type to MQTT in a device profile, additional settings become available under Transport configuration. These settings apply to all devices using the profile.
Custom topic filters
Section titled “Custom topic filters”You can override the default topics that ThingsBoard listens on for telemetry and attributes:
| Filter | Default | Description |
|---|---|---|
| Telemetry topic filter | v1/devices/me/telemetry | Topic the platform listens on for incoming telemetry |
| Attributes publish topic filter | v1/devices/me/attributes | Topic for client-side attribute publishes |
| Attributes subscribe topic filter | v1/devices/me/attributes | Topic for shared attribute update subscriptions |
MQTT wildcards are supported:
+matches a single topic level — e.g.v1/devices/+/telemetryor+/devices/+/attributes#matches the rest of the topic and must be the last character — e.g.#orv1/devices/me/#
Payload format
Section titled “Payload format”Choose the serialization format for MQTT messages:
| Format | Description |
|---|---|
| JSON | Default. Devices send plain JSON payloads as shown in the telemetry, attributes, and RPC examples. |
| Protobuf | Devices send binary Protocol Buffers messages. You define the .proto schemas per profile. |
JSON options
Section titled “JSON options”Send PUBACK on PUBLISH message validation failure — by default, ThingsBoard closes the MQTT session when it receives a message that fails validation (e.g. malformed JSON). Enable this option to send a PUBACK acknowledgment instead, so the device stays connected. Useful for devices that cannot handle unexpected disconnects gracefully.
Protobuf configuration
Section titled “Protobuf configuration”When Protobuf is selected, you define four .proto schemas in the device profile — one for each message type:
Telemetry proto schema:
syntax = "proto3";package telemetry;
message SensorDataReading { optional double temperature = 1; optional double humidity = 2; InnerObject innerObject = 3;
message InnerObject { optional string key1 = 1; optional bool key2 = 2; optional double key3 = 3; optional int32 key4 = 4; optional string key5 = 5; }}Attributes proto schema:
syntax = "proto3";package attributes;
message SensorConfiguration { optional string firmwareVersion = 1; optional string serialNumber = 2;}RPC request proto schema (server-side RPC commands sent to the device):
syntax = "proto3";package rpc;
message RpcRequestMsg { optional string method = 1; optional int32 requestId = 2; optional string params = 3;}RPC response proto schema (device replies to two-way RPC):
syntax = "proto3";package rpc;
message RpcResponseMsg { optional string payload = 1;}Customize these schemas to match your device’s data model. Field names in the telemetry and attributes schemas become the ThingsBoard telemetry keys and attribute keys respectively.
Enable compatibility with other payload formats — when enabled, the platform tries to parse messages as Protobuf first, then falls back to JSON if parsing fails. This is useful during firmware upgrades when older devices still send JSON while newer ones send Protobuf. The compatibility mode introduces slight performance overhead, so disable it once all devices are updated.
Send PUBACK on PUBLISH message validation failure — same behavior as for JSON: sends a PUBACK instead of closing the session on validation failure.
MQTT Sparkplug B
Section titled “MQTT Sparkplug B”Check MQTT Sparkplug B Edge of Network (EoN) node to treat connected devices as Sparkplug B nodes. See the Sparkplug API reference for details.
Connection Response Codes
Section titled “Connection Response Codes”| Code | Meaning |
|---|---|
0x00 | Connected successfully |
0x04 | Bad username or password — access token missing or malformed |
0x05 | Not authorized — access token invalid or device disabled |
MQTT v5.0 error codes
Section titled “MQTT v5.0 error codes”MQTT v5.0 clients receive numeric error codes in CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, and DISCONNECT packets. The table below lists every code ThingsBoard may return.
| Code | Name | Packets | Description |
|---|---|---|---|
| 128 | Unspecified error | CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECT | An unspecified error occurred. |
| 129 | Malformed packet | CONNACK, DISCONNECT | The received packet cannot be parsed according to the MQTT specification. |
| 130 | Protocol error | CONNACK, DISCONNECT | The packet is structurally valid but contains data or behavior that violates the protocol. |
| 131 | Implementation-specific error | CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECT | The packet is valid but not accepted by the current receiver’s implementation. |
| 132 | Unsupported protocol version | CONNACK | The requested MQTT protocol version is not supported. |
| 133 | Client identifier not valid | CONNACK | The client ID is well-formed but not accepted by the server (like exceeding the maximum length). |
| 134 | Bad username or password | CONNACK | The client provided an incorrect username or password. |
| 135 | Not authorized | CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECT | The operation is not authorized (like publishing to a topic the client has no permission for). |
| 136 | Server unavailable | CONNACK | The server is currently unavailable. |
| 143 | Topic filter invalid | PUBACK, PUBREC, DISCONNECT | The topic filter format is correct but not accepted by the server. |
| 144 | Topic name invalid | PUBACK, PUBREC, DISCONNECT | The topic name format is correct but not accepted by the client or server. |
| 149 | Packet too large | CONNACK, DISCONNECT | The packet exceeds the maximum size agreed upon during connection. |
| 151 | Quota exceeded | CONNACK, PUBACK, PUBREC, SUBACK, DISCONNECT | An administratively imposed limit has been exceeded. |
| 153 | Payload format invalid | PUBACK, PUBREC, DISCONNECT | The payload does not match the format indicated by the Payload Format Indicator property. |