Skip to main content

MQTT module "mqtt"

Description

The MQTT module is used for communication with an MQTT broker in both directions.

Several smartCORE channels can be assigned to one MQTT topic, whereby the module enables the selection of several topics.

In addition, several modules can be instantiated in order to be able to communicate with several different brokers at the same time.

With regard to the interpretation and preparation of MQTT payloads, so-called payload hints are supported, which serve to describe the format.

In the publication direction, these are used to perform a syntax check of the publication templates into which consumed channel values are then inserted (e.g. for common formats such as JSON, XML, ...).

Conversely, these payload hints in the subscribe direction serve to provide suitable data extraction mechanisms for the above-mentioned formats, so that the extracted values can be produced in the corresponding smartCORE channels.

Interfaces & protocols used

  • MQTT

JSON configuration

The following section describes the entire JSON configuration of the module and explains the individual parameters.

Example configuration (minimum and typical)

The following is a minimal example configuration:

    {
"module": "MQTT",
"factory": "mqtt",
"config":{
"brokerAddress": "ssl://awe.some.io:8883",
"brokerUserId": "USER_ID",
"brokerUserPassword": "USER_PASSWORD",
"brokerClientId": "CLIENT_ID",
"topics":[

<MQTT TOPIC CONFIGURATIONS>

]
}
}

Example configuration (maximum)

    {
"module": "MQTT",
"factory": "mqtt",
"config":{
"useLocalBroker":false,
"enabled":true,
"pollingIntervalMs":1000,

"brokerAddress": "ssl://awe.some.io:8883",
"brokerAuthentication": true,
"trustStore": "/etc/ssl/certs/ca-certificates.crt",
"brokerUserId": "USER_ID",
"brokerUserPassword": "USER_PASSWORD",
"brokerClientId": "CLIENT_ID",

"maxBufferedMessages": 10,
"connectTimeoutS": 10,
"disconnectTimeoutS": 10,
"keepaliveIntervalS": 10,
"completionIntervalS": 10,
"sendingIntervalS": 10,
"topics":[

<MQTT TOPIC CONFIGURATIONS>

]
}
}

Examples of JSON objects for topic configuration...

The following JSON objects can be inserted into the above array of topics.

...in the direction of publication

The strings listed under mqttName serve as placeholders, which are replaced 1:1 by the values of the smartCORE channels listed under channelName.

{
"name": "sensor/state",
"direction": "publish",
"payload":"{\"voltage\":%BAT_VOLTAGE%,\"current\":%BAT_CURRENT%}",
"payloadHint": "json",
"channels":[
{
"mqttName":"%BAT_VOLTAGE%",
"channelName": "PowerSupplyVoltage"
},
{
"mqttName":"%BAT_CURRENT%",
"channelName": "PowerSupplyCurrent"
}
]
}

...in subscription direction

The strings listed under mqttName describe how channel data is to be extracted from the MQTT payload, which is possible in different ways (see below).

  {
"name": "sensor/status/switch:0",
"direction": "subscribe",
"payloadHint": "json",
"channels":[
{
"mqttName": "power",
"channelName": "power",
"dataType": "float"
},
{
"mqttName": "current",
"channelName": "Current",
"dataType": "float"
},
{
"mqttName": "temperature/tC",
"channelName": "TemperatureTC",
"dataType": "float"
}
]
}

Module parameters

Parameter nameRequiredData typeMeaningful value rangeDefaultDescription
useLocalBrokerNoBOOLtrue, falsefalseStart MQTT Broker on the device and communicate with it
enabledNoBOOLtrue, falsetrueActivity of the communication to the broker
pollingIntervalMsNoINT1 -1000Module-related processing interval [ms]
brokerAddressNoSTRINGvalid broker URL"ssl://awe.some.io:8883" , "tcp://awe.some.io:1883" (not recommended)
brokerAuthenticationNoBOOLtrue, falsetrueBroker authentication
trustStoreNoSTRING/etc/ssl/certs/ca-certificates.crtTrust Store for broker authentication
brokerUserIdNoSTRINGBroker user ID
brokerUserPasswordNoSTRINGBroker user password
brokerClientIdNoSTRINGBroker client/device identifier
maxBufferedMessagesNoINT1 -10Maximum number of buffered MQTT messages within the MQTT client
connectTimeoutSNoINT1 -10Timeout regarding connection establishment
disconnectTimeoutSNoINT1 -10Timeout regarding disconnection after request
keepaliveIntervalSNoINT1 -10Timeout regarding detection of a disconnected connection
completionIntervalSNoINT1 -10Timeout regarding acknowledgement receipt after sending an MQTT message
sendingIntervalSNoINT1 -10Timeout for sending an MQTT message
topicsYESJSON Arraysee MQTT Topic Configuration

MQTT topic configuration "topics"

A distinction is made between published and subscribed topics.

Topics to be published (published topics)

A topic to be published, which generally contains a selection of smartCORE channels, is configured as a JSON object containing the following parameters.

Parameter NameRequiredData TypeMeaningful Value RangeDefaultDescription
nameYESSTRINGName/path of the topic on the broker
directionYESSTRING"publish"set to "publish" for a topic to be published
payloadYESSTRINGpayload template
payloadHintYESSTRING"plain", "json""json"Selection of the payload format to be transmitted with regard to which a syntax check of the payload template is performed, if this is possible
channelsYESJSON arraysee below

The channel configuration of channels to be published takes the form of a JSON array containing JSON objects with the following parameters.

Parameter NameRequiredData TypeMeaningful Value RangeDefaultDescription
channelNameYESSTRINGName of the smartCORE channel to be consumed
mqttNameYESSTRINGsubstring within the payload template, which is replaced with a string representation of the current channel value.
formatNOSTRINGFormatting instruction regarding conversion of the channel value to string
translateFalseNOSTRING"false"String representation of the Boolean value for false
translateTrueNOSTRING"true"String representation of the Boolean value for true

Topics to be subscribed to (subscribed topics)

A topic to be subscribed to, which generally provides a selection of smartCORE channels, is configured as a JSON object containing the following parameters.

Parameter NameRequiredData TypeMeaningful Value RangeDefaultDescription
nameYESSTRINGName/path of the topic on the broker
directionYESSTRING"subscribe"to be set to "subscribe" for a topic to be subscribed to
payloadHintYESSTRING"plain", "json""json"Selection of the payload format to be received for data extraction into configured channels.
channelsYESJSON arraysee below

The channel configuration of channels to be published takes the form of a JSON array containing JSON objects with the following parameters.

Parameter nameRequiredData typeMeaningful value rangeDefaultDescription
channelNameYESSTRINGName of the smartCORE channel to be produced
dataTypeYESSTRINGvalid smartCORE data typedata type of the channel
bufferSizeNOINT1 -1024Buffer size of the channel
physicalUnitNOSTRINGPhysical unit of the channel
mqttNameYESSTRINGString that describes an instruction for extracting individual channel values from a payload (e.g. a path specification for JSON payloads)
translateFalseNOSTRING"false"String representation of the Boolean value for false
translateTrueNOSTRING"true"String representation of the Boolean value for true

Direct 1:1 data extraction (payloadHint "plain")

The entire content of the payload is used as the channel value in the subscription direction (subscribe). An automatic error-tolerant conversion to the target data type is carried out as far as this is possible. Conversely, there is no syntax check in the publication direction.

JSON (payloadHint "json")

To extract channel data from a subscribed topic with a JSON-formatted payload, a path can be used as the "mqttName" parameter.

Example: If the received payload contains the following JSON object.

{
"someObject":{
"someSubObject/with/slash":{
"someKey": 42
}
}
"someArray":[
1,
2,
3
]
}

then "mqttName": "someObject/someSubObject\/with\/slash/someKey" extracts the value 42, which is then produced in the channel defined as "channelName" (i.e. it is necessary to escape the separator "/").

Accordingly, "mqttName": "someArray/1" extracts the value 2 from the above-mentioned JSON object (i.e. in the case of a JSON array, the path component is used as the index).

If several separators "/" are specified in succession, they are regarded as a single separator.

If an invalid path is specified, no channel data is produced in any case.

Module information

InformationValue
AuthorsoptiMEAS Measurement and Automation Systems GmbH
since smartCORE2.6
module typeeither consumer, producer or both
Dependenciesexisting MQTT broker