Skip to main content

Manually send MQTT messages

If you're not using Connhex Edge, you'll need to manually exchange data with your Connhex instance - both sending messages and receiving commands. This section describes how to do so with MQTT: all the following steps must be implemented in the physical device's firmware.

Provisioning

You can only connect devices which have already been registered (i.e. provisioned) in Connhex. To read more on how you can provision devices, see here.

Initialization: configuration download

This paragraph assumes the device already has its init_id and init_key. In case of more complex setups, a key exchange will have to happen before the device can communicate: read more here and here.

The first step is to retrieve the device config: make sure you're including the init_key as authorization header. The response includes all the parameters needed to connect via MQTT:

ParameterDescription
urlCloud IoT endpoint URL
connhex_idUsername
connhex_keyPassword
connhex_channelsMQTT topics. More on this below.
client_certClient certificate
client_keyPrivate client key
ca_certServer certificate
ca_chainCertificates chain, also containing authorities for domain verification

MQTT topics

MQTT topics associated with the device follow this format:

channels/<channel_id>/messages/events/<subtopic_id>

where <channel_id> corresponds to one of the IDs contained in the connhex_channels object from the response and <subtopic_id> can be optionally used to segment data according to any policy. For example, when using the Connhex Message Policy with the following channels:

"connhex_channels": [
{
"id": "4ab1d46b-f6f0-4c24-8598-515945071ea7",
"name": "control channel",
"metadata": {
"type": "control"
}
},
{
"id": "54f12aea-e632-4bcd-a164-c2e73dce964a",
"name": "event channel",
"metadata": {
"type": "event"
}
}
]

data can be sent on any of the following: channels/54f12aea-e632-4bcd-a164-c2e73dce964a/messages/events/<infos|params|data>

Protocol Version Requirements

Ensure you are using MQTT version 3.1.1 or 3.1 for proper compatibility.

Sending data and receiving commands

Your device should now be able to publish data and subscribe to receive commands: make sure you're using one of the supported message formats.