MQTT Device API Reference
Last updated
Last updated
MQTT basics
is a lightweight publish-subscribe messaging protocol which probably makes it the most suitable for various IoT devices. You can find more information about MQTT .
JIoT server nodes act as an MQTT Broker that supports QoS levels 0 (at most once) and 1 (at least once) and a set of predefined topics.
Client libraries setup
You can find a large number of MQTT client libraries on the web. Examples in this article will be based on Mosquitto and MQTT.js. In order to setup one of those tools, you can use instructions in our Hello World guide.
MQTT Connect
We will use access token device credentials in this article and they will be referred to later as $ACCESS_TOKEN. The application needs to send MQTT CONNECT message with username that contains $ACCESS_TOKEN. Possible return codes and their reasons during connect sequence:
0x00 Connected - Successfully connected to JIoT MQTT server.
0x04 Connection Refused, bad user name or password - Username is empty.
0x05 Connection Refused, not authorized - Username contains invalid $ACCESS_TOKEN.
By default, JIoT supports key-value content in JSON. Key is always a string, while value can be either string, boolean, double or long. Using custom binary format or some serialization framework is also possible. See protocol customization for more details. For example:
In order to publish telemetry data to JIoT server node, send PUBLISH message to the following topic:
The simplest supported data formats are:
or
Please note that in this case, the server-side timestamp will be assigned to uploaded data!
In case your device is able to get the client-side timestamp, you can use following format:
Mosquitto
MQTT.js
telemetry-data-as-object.json
telemetry-data-as-array.json
telemetry-data-with-ts.json
JIoT attributes API allows devices to
Upload client-side device attributes to the server.
Request client-side and shared device attributes from the server.
Subscribe to shared device attributes from the server.
Publish attribute update to the server
In order to publish client-side device attributes to JIoT server node, send PUBLISH message to the following topic:
Mosquitto
MQTT.js
new-attributes-values.json
Request attribute values from the server
In order to request client-side or shared device attributes to JIoT server node, send PUBLISH message to the following topic:
where $request_id is your integer request identifier. Before sending PUBLISH message with the request, client need to subscribe to
The following example is written in javascript and is based on mqtt.js. Pure command-line examples are not available because subscribe and publish need to happen in the same mqtt session.
MQTT.js
mqtt-js-attributes-request.js
Result
Please note, the intersection of client-side and shared device attribute keys is a bad practice! However, it is still possible to have same keys for client, shared or even server-side attributes.
Subscribe to attribute updates from the server
In order to subscribe to shared device attribute changes, send SUBSCRIBE message to the following topic:
When a shared attribute is changed by one of the server-side components (such as the REST API or the Rule Chain), the client will receive the following update:
Mosquitto
MQTT.js
In order to subscribe to RPC commands from the server, send SUBSCRIBE message to the following topic:
Once subscribed, the client will receive individual commands as a PUBLISH message to the corresponding topic:
where $request_id is an integer request identifier.
The client should publish the response to the following topic:
The following example is written in javascript and is based on mqtt.js. Pure command-line examples are not available because subscribe and publish need to happen in the same mqtt session.
MQTT.js
mqtt-js-rpc-from-server.js
In order to send RPC commands to server, send PUBLISH message to the following topic:
where $request_id is an integer request identifier. The response from server will be published to the following topic:
The following example is written in javascript and is based on mqtt.js. Pure command-line examples are not available because subscribe and publish need to happen in the same mqtt session.
MQTT.js
mqtt-js-rpc-from-client.js
MQTT transport can be fully customized for specific use-case by changing the corresponding module.
In the example above, we assume that “1451649600512” is a with milliseconds precision. For example, the value ‘1451649600512’ corresponds to ‘Fri, 01 Jan 2016 12:00:00.512 GMT’