AllThingsTalk Message Broker enables sending and receiving of messages to and from AllThingsTalk Cloud. When communicating with the Cloud, a client sends a message addressed to a topic like asset/<ASSET_ID>/state.
The Broker, in turn, publishes the message to all clients that have subscribed to receive messages for that topic.
This article explains how to use Messaging API based on MQTT protocol.
MQTT is a publish/subscribe based messaging protocol for use on top of the TCP/IP protocol. It is designed for connections with remote locations where a small code footprint is required or network bandwidth is limited.
To quickly get started with MQTT, we recommend that you use the Mosquitto command-line tools. Make sure to pick a suitable Binary installation.
AllThingsTalk allows clients to connect to the Broker and exchange information using MQTT and MQTTS endpoints:
MQTT (Non SSL connections)-h '<instance>.allthingstalk.io'
MQTTS (SSL connections)-h '<instance>.allthingstalk.io' -p 8883 --cafile '<the_cert_file.pem>'
| Instance | MQTT endpoint |
|---|---|
| AllThingsTalk Maker | api.allthingstalk.io |
| ALSO IoT Platform | also.allthingstalk.io |
| Private space (e.g, ACME) | acme.allthingstalk.io |
For ease of use, we’ll use api.allthingstalk.io in the following examples of this article.
If you want to use MQTTS secure endpoint, you might - depending on your client - need to provide the public SSL certificate. Download it here and include it in your call.
The Broker requires that every client provides valid authentication credentials, either when publishing or subscribing for a message.
To authenticate your Device application with the Broker, you will need a Device token.
Similarly, for a Ground level application we suggest to use Ground token.
For example:
-u 'maker:4ExampleTokenqXpXzDng5Ivtx1'
Although the Broker doesn’t check the password that clients send, some MQTT clients won’t send message if password is empty. To stay on the safe side, please provide any string
-P 'bfnbayv9'
We support MQTT version 3.1.1.
When connecting to a broker, we recommend you use the following parameters:
You can use Clean session = False if you want to have a permanent connection. In this case, the broker will store all the undelivered messages in case of a broken connection, and will deliver those messages upon successful reconnection.
When subscribing and publishing to a topic, you can choose between QoS0 and QoS1, depending on your application and business rules.
Message Broker understands these message formats:
When communicating with the Broker, your client needs to address the message to a certain topic.
Read more about MQTT Topics here: http://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices
Use Asset States topics to hit the Broker with sensory data. There are two kinds:
Asset State topic is suitable for clients that hold an Asset IDasset/<ASSET_ID>/state
Device Asset State topic is suitable for clients that hold a Device ID and an Asset Name.device/<DEVICE_ID>/asset/<ASSET_NAME>/state
To publish to an asset abc123 a state of 20.1 (e. g. celsius degrees):
mosquitto_pub -h 'api.allthingstalk.io' -t 'asset/abc123/state' \
-u 'maker:40lqFaaHjc8kWeqXpXzDng5Ivtx1' -P 'bfnbayv9' \
-m '{"value": 20.1}'
You can also publish data to the same asset by identifying device and asset name, e. g. a temperature:
mosquitto_pub -h 'api.allthingstalk.io' -t 'device/def456/asset/temperature/state' \
-u 'maker:40lqFaaHjc8kWeqXpXzDng5Ivtx1' -P 'bfnbayv9' \
-m '{"value": 20.1}'
Use Asset Feeds topics to receive asset state changes in real time.
Asset Feed topic is suitable for clients that hold an Asset ID.asset/<ASSET_ID>/feed
Device Asset Feed topic is suitable for clients that hold a Device ID and an Asset Namedevice/<DEVICE_ID>/asset/<ASSET_NAME>/feed
To receive a feed of temperature sensor states coming from device def456:
mosquitto_sub -h 'api.allthingstalk.io' -t 'device/def456/asset/temperature/feed' \
-u 'maker:4ExampleTokenqXpXzDng5Ivtx1' -P 'bfnbayv9'
Messages (feeds) will arrive in JSON format:
{"at":"2017-06-01T15:43:00Z","value":20.1}
{"at":"2017-06-01T15:43:16Z","value":20.2}
{"at":"2017-06-01T15:43:42Z","value":21.4}
Use Asset Commands topics to receive actuation commands sent by the Broker.
Asset Command topic is suitable for clients that hold an Asset IDasset/<ASSET_ID>/command
Device Asset Command topic is suitable for clients that hold a Device ID and an Asset Namedevice/<DEVICE_ID>/asset/<ASSET_NAME>/command
Listening for a boolean command to control an actuator abc123.
mosquitto_sub -h 'api.allthingstalk.io' -t 'asset/abc123/command' \
-u 'maker:4ExampleTokenqXpXzDng5Ivtx1' -P 'bfnbayv9'
Messages (commands) will arrive in JSON format:
{"at":"2017-06-01T17:31:51Z","value":true,"meta":null}
Use + wildcard in a topic to receive a command for any asset within a given device:
mosquitto_sub -h 'api.allthingstalk.io' -t 'device/def456/asset/+/command' \
-u 'maker:4ExampleTokenqXpXzDng5Ivtx1' -P 'bfnbayv9'
Some devices also publish MQTT messages on topics of their own, next to the AllThingsTalk topics above. A common case is a status message the device sends right after it connects. Shelly devices of the second generation and later, for example, publish true on <shelly-id>/online when they connect and set a Last Will of false on the same topic.
When your client is connected with a Device token or a Ground token, the Broker accepts a publish on such a topic and ignores it: the connection stays open and a QoS1 publish is acknowledged as usual, but the message is not stored, does not become an asset state, does not appear in the device’s debug log and does not trigger any rule. No client can subscribe to such a topic either, so the message never reaches anyone. A Last Will on such a topic is handled in the same way.
A topic is outside AllThingsTalk when its first level is not asset, device, gateway, ground or user (in upper or lower case), and all its levels are non-empty and use only letters, digits, -, _ and :.
For example, acme-plug-7f3a/online and acme-plug-7f3a/status/switch:0 are topics outside AllThingsTalk.
Only messages on state topics are stored. For a device, these are asset/<ASSET_ID>/state, device/<DEVICE_ID>/asset/<ASSET_NAME>/state and device/<DEVICE_ID>/state (see Data formats). So publish every reading you want to keep to one of these topics, for an asset that exists on your device. Write these topics exactly as shown here, with asset, device and state in lower case and without a leading or trailing /.
The Broker closes the whole connection when your client publishes or subscribes to a topic it has no right to, and your client then has to connect again. For example:
feed or command topicacme-plug-7f3a/online/, /acme-plug-7f3a/online or acme-plug-7f3a//onlineacme-plug-7f3a/online, or to #A device with the ID def456 publishes its own status message true on acme-plug-7f3a/online with QoS1. mosquitto_pub finishes without an error, and the connection stays open:
mosquitto_pub -h 'api.allthingstalk.io' -t 'acme-plug-7f3a/online' -q 1 \
-u 'maker:4ExampleTokenqXpXzDng5Ivtx1' -P 'bfnbayv9' \
-m 'true'
To store a reading of the same device, for example 12.3 for its asset power, publish it to a state topic instead:
mosquitto_pub -h 'api.allthingstalk.io' -t 'device/def456/asset/power/state' \
-u 'maker:4ExampleTokenqXpXzDng5Ivtx1' -P 'bfnbayv9' \
-m '{"value": 12.3}'