Skip to main content

PubSub (MQTT)

Use the pubsub: section to send the values of variables to an MQTT broker. The gateway sends OPC UA PubSub messages (OPC UA Part 14), in JSON, at a fixed interval. The MQTT clients that subscribe to the broker get the values without an OPC UA connection.

Words on this page​

WordMeaning
brokerThe MQTT server that receives the messages and sends them to the subscribers.
topicThe name of the MQTT channel of a message: plant/boiler.
published data setA named list of variables that the gateway sends together.
fieldOne variable of a published data set. Its name is the key of the value in the message.
writer groupA group of data set writers that send their messages at the same interval.
data set writerThe part of a writer group that sends one published data set.
PublisherIdThe name of the gateway in each message.

A complete example​

port: 4840

instances:
- browseName: Boiler
typeDefinition: ua:BaseObjectType
organizedBy: /ua:Objects
- browseName: Temperature
dataType: ua:Double
componentOf: /ua:Objects/own:Boiler
- browseName: Running
dataType: ua:Boolean
componentOf: /ua:Objects/own:Boiler

pubsub:
publisherId: edge-01
publishedDataSets:
- name: Boiler
fields:
- name: Temperature
node: /ua:Objects/own:Boiler/own:Temperature
- name: Running
node: /ua:Objects/own:Boiler/own:Running
connections:
- name: PlantBroker
transport: mqtt-json
url: mqtt://broker.plant.local:1883
writerGroups:
- name: Fast
writerGroupId: 1
publishingInterval: 1000
dataSetWriters:
- name: BoilerWriter
dataSetWriterId: 1
dataSet: Boiler
topic: plant/boiler

Each 1000 ms, the broker receives this message on the topic plant/boiler:

{
"MessageId": "B5087A1F-65B4-6D67-B6D4-000000000004",
"MessageType": "ua-data",
"PublisherId": "edge-01",
"Messages": {
"DataSetWriterId": "1",
"MessageType": "ua-keyframe",
"Payload": { "Temperature": 42.5, "Running": true }
}
}

The key reference is in pubsub.

How to publish variables​

  1. Make sure that each variable exists. Declare it in instances: or get it from a device.
  2. Add a pubsub: section.
  3. Add a published data set to publishedDataSets:.
  4. Add a field for each variable. Set node: to the browse path of the variable, as in mapping:.
  5. Add a connection to connections:. Set url: to the URL of the broker.
  6. Add a writer group to the connection. Give it a writerGroupId: and a publishingInterval:.
  7. Add a data set writer to the writer group. Set dataSet: to the name of the published data set.
  8. Restart the gateway.

The gateway examines each node: when it starts. If the gateway cannot find a node, it does not start and shows the browse path.

Topics​

The gateway sends the messages of a data set writer on one topic. The gateway uses the first topic that it finds in this order:

  1. topic: of the data set writer.
  2. topic: of the writer group.
  3. The default topic: opcua/json/data/<publisherId>/<writerGroupId>/<dataSetWriterId>.

Broker URL​

The URL starts with mqtt://, mqtts://, ws:// or wss://. To give a user name and a password, put them in the URL: mqtt://user:password@broker.plant.local:1883.

warning

A password in the URL is in clear text in the configuration file. Give read access to the configuration file only to the persons that must have the password.

Keys​

KeyRequiredDefaultMeaning
publisherIdoptionalthe host nameThe PublisherId in each message. A string or a positive integer.
publishedDataSets[].namerequiredThe name of the published data set.
publishedDataSets[].fields[].namerequiredThe key of the value in the message.
publishedDataSets[].fields[].noderequiredThe browse path or the NodeId of the variable.
publishedDataSets[].fields[].samplingIntervaloptionalThe interval in ms between two reads of the variable.
connections[].namerequiredThe name of the connection.
connections[].transportoptionalmqtt-jsonThe transport. Only mqtt-json is available.
connections[].urlrequiredThe URL of the broker.
writerGroups[].writerGroupIdrequiredThe WriterGroupId, from 1 to 65535.
writerGroups[].publishingIntervaloptional1000The interval in ms between two messages.
writerGroups[].qosoptionalatMostOnceThe MQTT quality of service: atMostOnce, atLeastOnce or exactlyOnce.
writerGroups[].topicoptionalThe topic of the messages of the writer group.
dataSetWriters[].dataSetWriterIdrequiredThe DataSetWriterId, from 1 to 65535. It must be unique in the pubsub: section.
dataSetWriters[].dataSetrequiredThe name of the published data set to send.
dataSetWriters[].topicoptionalThe topic of the messages of the data set writer.

serverOptions.enablePubsub​

enablePubsub: true adds the OPC UA PubSub information model (Server/PublishSubscribe) with no connection. An OPC UA client can then add connections with the PubSub methods. The gateway does not send a value when only enablePubsub: is set. A pubsub: section adds the PubSub information model. You do not have to set enablePubsub:.

Errors​

The gateway does not start when the pubsub: section has one of these errors. The message gives the path of the entry, for example pubsub.publishedDataSets(Boiler).fields(Temperature).

MessageMeaningFix
cannot find node '<path>'The browse path of a field does not find a node.Correct node:. Run opcua-omni-edge dump-config to see the browse paths.
a published field must be a VariableThe node of a field is an Object or a Method.Set node: to a Variable.
no published data set is named '<name>'dataSet: of a data set writer has no published data set.Correct dataSet:, or add the published data set.
the dataSetWriterId <id> is used more than onceTwo data set writers have the same id.Give each data set writer a different dataSetWriterId:.
the writerGroupId <id> is used more than onceTwo writer groups of one connection have the same id.Give each writer group a different writerGroupId:.
the name '<name>' is used more than onceTwo published data sets, or two connections, have the same name.Give each entry a different name:.