Skip to main content

Commands

Commands let you invoke an operation on a connected device in real time. The cloud sends a request, the device executes it, and returns a response. The platform waits 10 seconds for the response.

How It Works​

Commands use a request-response pattern over MQTT with correlation IDs to match responses to requests:

Invoking a Command (Cloud Side)​

Commands are invoked through the Beacon Tower asset API. The request body is the command payload: it is forwarded to the device unchanged as the payload field of the invocation.

curl -X POST https://api.beacontower.ai/assets/temp-sensor-001/commands/reboot \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"force": true
}'

Send a JSON object, or no body. A scalar or array body reaches the device as "payload": null.

There is no timeout parameter. The platform waits 10 seconds for the device to respond. A field such as "timeout" in the body is not interpreted; it is passed to the device as part of the payload.

Asset API result​

The call returns 200 with the command's status once the device has answered or the wait has ended. The status is derived from the device's statusCode (see Publishing the Response):

Outcomestatus
Device answered 200Completed
Device answered 202Pending
Device answered any other code (including 201 and 204)Failed
Device not connected, did not answer within 10 seconds, or the provider failedFailed

responsePayload carries the payload field of the device's response, or null when the response has none.

While a command is Pending or Running, a new call for the same command on the same asset is not sent to the device: it returns 200 with the status of the command already in progress. A command left Pending by a 202 answer stays Pending, so later calls for that command keep returning it.

Other responses:

StatusWhenBody
400Invalid asset id or payloadPlain text
404Asset not found, or its command is not initialisedEmpty
503The asset processor is unavailablePlain text Asset processor unavailable
500Any other failure, including an internal timeoutEmpty

A Failed status does not tell you whether the device received the command. Before you retry, check whether running the command twice is safe.

Provider route​

Below the asset API, the provider-facing endpoint is spelled POST /providers/{providerId}/clients/{providerClientId}/directMethod, taking a RunDirectMethodRequest and returning a RunDirectMethodResult. Those names are unchanged — use them verbatim if you call that surface directly.

{ "method": "reboot", "componentId": null, "payload": { "force": true } }

method is required; componentId and payload are optional.

For providers built on the Provider SDK (including the MQTT provider):

StatusWhenBody
200The device answered, whatever its statusCode{ "status": <device statusCode>, "payload": <device response>, "errorMessage": null }
400method missingValidation problem details
403No permission on the provider clientPlain text
404Provider or provider client not foundPlain text
409Provider client not provisionedPlain text
500Device not connected, no answer within 10 seconds, or any provider failurePlain text error message

A 200 means the device answered; read status for the device's own result. Device offline, timeout and transport failures are not distinguished: all three return 500.

Handling Commands (Device Side)​

Subscribe to Command Invocations​

The device subscribes to its command invocation topic using a wildcard:

device/{deviceId}/command/+/invoke    (QoS 1)

The + wildcard matches any command name, so the device receives all command invocations on a single subscription.

Invocation Payload​

When a command is invoked, the device receives a message on device/{deviceId}/command/{commandName}/invoke (QoS 1, not retained):

{
"correlationId": "a1b2c3d4e5f67890abcdef1234567890",
"payload": {
"force": true
}
}
FieldDescription
correlationIdIdentifies this invocation. 32 lowercase hexadecimal characters (a GUID without dashes), new for every invocation. Use it in the response topic.
payloadThe command payload from the caller: a JSON object, or null

There is no responseTopic field. The device builds the response topic itself, as below.

Publishing the Response​

After executing the command, the device publishes the result to:

device/{deviceId}/command/{commandName}/response/{correlationId}    (QoS 1)

{commandName} is the name from the invoke topic and {correlationId} is the value from the invocation. The platform matches the response by the last topic segment; a response whose correlationId is unknown or no longer awaited (for example, after the wait has ended) is discarded.

The response is a JSON object with two fields, both optional:

FieldDescription
statusCodeThe command result, an integer from 200 to 599. 200 when absent.
payloadThe data returned to the caller: any JSON value (object, array, string, number, boolean or null).
{
"statusCode": 200,
"payload": { "uptime_before_reboot": 86400 }
}

Only payload reaches the caller. Any other field in the response is dropped, so put everything the caller needs inside payload.

ResponseResult
JSON object with statusCode 200–299, or without statusCodeSuccess with that status; the caller receives payload
JSON object with statusCode 300–599Device error: the status and payload are returned to the caller
Empty messageStatus 200, no payload
JSON object with a statusCode that is not an integer from 200 to 599The command fails (PayloadTypeMismatch)
A JSON scalar or array, or text that is not JSONThe command fails (PayloadTypeMismatch)

A failed command reaches the caller as described in Timeouts and Errors.

Complete Device-Side Example​

import paho.mqtt.client as mqtt
import json

DEVICE_ID = "temp-sensor-001"

def on_message(client, userdata, msg):
# Parse command name from topic
# Topic format: device/{id}/command/{name}/invoke
parts = msg.topic.split("/")
method_name = parts[3]

invocation = json.loads(msg.payload)
correlation_id = invocation["correlationId"]
payload = invocation.get("payload")
response_topic = f"device/{DEVICE_ID}/command/{method_name}/response/{correlation_id}"

# Execute the command
if method_name == "reboot":
result = {"statusCode": 200, "payload": {"message": "Rebooting in 5 seconds"}}
elif method_name == "get_diagnostics":
result = {"statusCode": 200, "payload": {"cpu_usage": 45.2, "memory_free": 1024}}
else:
result = {"statusCode": 404, "payload": {"message": f"Unknown method: {method_name}"}}

# Publish response
client.publish(response_topic, json.dumps(result), qos=1)

client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id=DEVICE_ID)
client.username_pw_set(DEVICE_ID, "device-primary-key")
client.on_message = on_message
client.tls_set()
client.connect("mqtt.example.com", 8883, keepalive=120)

# Subscribe to all command invocations
client.subscribe(f"device/{DEVICE_ID}/command/+/invoke", qos=1)
client.loop_forever()

Timeouts and Errors​

Timeout​

The platform waits 10 seconds for the response. If the device does not answer in time, the asset API returns 200 with status Failed, and the provider route returns 500 with a plain-text message. A response that arrives later is discarded.

Device Offline​

If the device is not connected to the broker when the command is invoked, the platform does not publish the invocation at all, so nothing is queued for the device. The caller sees the same result as a timeout, after the same 10 seconds.

Error Handling​

Provider-side failures reach the caller as Failed (asset API) or 500 with a plain-text message (provider route). There is no JSON error body and no machine-readable error code on either route.

Supported Commands​

Commands are not predefined — any command name is valid. The command name is simply the string in the topic path. It is up to the device firmware to recognize and handle the commands it supports.

Common patterns:

CommandTypical Purpose
rebootRestart the device
get_diagnosticsReturn diagnostic information
set_configApply configuration changes
update_firmwareTrigger firmware update
pingConnectivity check

Next Steps​