Jump to content

MQTT

From Commander4j
Revision as of 21:47, 9 November 2026 by Dgarratt (talk | contribs) (Updated by push_wiki.py)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The MQTT connector publishes a message to an MQTT broker. It is an output connector only. A typical use is passing a value from a file, such as a production count, to a dashboard or another system that subscribes to the topic.

Map configuration

config.xml

<output>
   <id>out1</id>
   <enabled>true</enabled>
   <description>Send to MQTT Broker</description>
   <type>MQTT</type>
   <url>
      <path>./interface/output/XML MQTT</path>
      <prefix/>
      <fileExtension/>
      <use83GUID>false</use83GUID>
      <retainOriginalFilename>false</retainOriginalFilename>
   </url>
   <mqtt>
      <mqttBroker>tcp://broker.example.com:1883</mqttBroker>
      <mqttTopic>factory/line1/production</mqttTopic>
      <mqttClient>middleware4j-line1</mqttClient>
      <mqttContentXPath>/message/messageData/productionDeclaration/productionQuantity</mqttContentXPath>
      <mqttQos>2</mqttQos>
   </mqtt>
</output>
Setting Description
type MQTT
url / path A folder. A small report of each publish is written here. See below.
mqtt / mqttBroker Address of the broker, for example tcp://broker.example.com:1883.
mqtt / mqttTopic The topic to publish to.
mqtt / mqttClient The client identifier this connection presents to the broker. Each client connected to a broker needs a different identifier.
mqtt / mqttContentXPath An XPath expression selecting the element whose text is published as the message. Required.
mqtt / mqttQos Quality of service: 0, 1 or 2.

The settings shared by all connectors are described on the Maps page.

What is published

The message is the text of the one element selected by mqttContentXPath. With the settings above and a Commander4j production declaration as input, the message published would be the production quantity, for example 5616.000.

To publish something built from several values, add a stylesheet to the map that assembles the text into one element and point mqttContentXPath at that element.

<xsl:template match="/message/messageData/productionDeclaration">
   <mqtt>
      <payload><xsl:value-of select="SSCC"/>;<xsl:value-of select="material"/>;<xsl:value-of select="productionQuantity"/></payload>
   </mqtt>
</xsl:template>

With <mqttContentXPath>/mqtt/payload</mqttContentXPath> this publishes 000000000000000123;43170672;5616.000.

The report file

For each message a text file ending in .mqtt is written to the output folder. It lists the broker, client, topic and quality of service, the message that was published, and each step of the connection, ending with the word SUCCESS. It is a simple record that the message was sent.

The sample map map429 in samples/advanced/config/config.xml shows an MQTT output.

See also: Connectors, Maps, SOCKET