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
| Word | Meaning |
|---|---|
| broker | The MQTT server that receives the messages and sends them to the subscribers. |
| topic | The name of the MQTT channel of a message: plant/boiler. |
| published data set | A named list of variables that the gateway sends together. |
| field | One variable of a published data set. Its name is the key of the value in the message. |
| writer group | A group of data set writers that send their messages at the same interval. |
| data set writer | The part of a writer group that sends one published data set. |
| PublisherId | The 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
- Make sure that each variable exists. Declare it in
instances:or get it from a device. - Add a
pubsub:section. - Add a published data set to
publishedDataSets:. - Add a field for each variable. Set
node:to the browse path of the variable, as inmapping:. - Add a connection to
connections:. Seturl:to the URL of the broker. - Add a writer group to the connection. Give it a
writerGroupId:and apublishingInterval:. - Add a data set writer to the writer group. Set
dataSet:to the name of the published data set. - 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:
topic:of the data set writer.topic:of the writer group.- 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.
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
| Key | Required | Default | Meaning |
|---|---|---|---|
publisherId | optional | the host name | The PublisherId in each message. A string or a positive integer. |
publishedDataSets[].name | required | The name of the published data set. | |
publishedDataSets[].fields[].name | required | The key of the value in the message. | |
publishedDataSets[].fields[].node | required | The browse path or the NodeId of the variable. | |
publishedDataSets[].fields[].samplingInterval | optional | The interval in ms between two reads of the variable. | |
connections[].name | required | The name of the connection. | |
connections[].transport | optional | mqtt-json | The transport. Only mqtt-json is available. |
connections[].url | required | The URL of the broker. | |
writerGroups[].writerGroupId | required | The WriterGroupId, from 1 to 65535. | |
writerGroups[].publishingInterval | optional | 1000 | The interval in ms between two messages. |
writerGroups[].qos | optional | atMostOnce | The MQTT quality of service: atMostOnce, atLeastOnce or exactlyOnce. |
writerGroups[].topic | optional | The topic of the messages of the writer group. | |
dataSetWriters[].dataSetWriterId | required | The DataSetWriterId, from 1 to 65535. It must be unique in the pubsub: section. | |
dataSetWriters[].dataSet | required | The name of the published data set to send. | |
dataSetWriters[].topic | optional | The 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).
| Message | Meaning | Fix |
|---|---|---|
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 Variable | The 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 once | Two data set writers have the same id. | Give each data set writer a different dataSetWriterId:. |
the writerGroupId <id> is used more than once | Two writer groups of one connection have the same id. | Give each writer group a different writerGroupId:. |
the name '<name>' is used more than once | Two published data sets, or two connections, have the same name. | Give each entry a different name:. |