MQTT PubSub API
The MQTT PubSub client is used to connect to an MQTT broker, subscribe to topics, and publish data. It can be used in combination with an OPC UA server to listen to changes in nodes and publish data to an MQTT broker. It is also possible to use it to publish data to an MQTT broker without an OPC UA server. In this case, you’ll need to set data manually.
MQTT Client constructor
- ua.newMqttClient()
- ua.newMqttClient(config)
- ua.newMqttClient(config, uaServer)
- ua.newMqttClient(config, model)
Create an instance of MQTT client. MQTT client is used to connect to MQTT broker, subscribe to topics, publish data.
- Parameters:
config – MQTT client configuration. If not provided, then default configuration will be used.
uaServer – OPC UA server instance. If provided, MQTT client can connect to nodes and listen value changes.
model – OPC UA model. If provided will use it for encoding/decoding datastructures.
- Returns:
MQTT client instance.
If uaServer and model not provided then MQTT client then will be loaded only base OPCUA model. Thus will be possible to encode/decode only base OPCUA data types.
MQTT Connect
- uaMqtt:connect(endpointUrl, transportProfileUri, <connectCallback>)
Connect to MQTT broker.
- Parameters:
endpointUrl – OPC UA Endpoint URL
transportProfileUri –
OPC UA Transport Profile URI. Defines encoding of the messages:
ua.TransportProfileUri.MqttBinary
ua.TransportProfileUri.MqttJson
connectCallback – Callback function
local function connectCallback(status)
print("Connected to MQTT broker")
end
uaMqtt:connect("mqtt://localhost:1883", ua.TransportProfileUri.MqttBinary, connectCallback)
MQTT subscribe
- uaMqtt:subscribe(topic, messageCallback)
Subscribe to MQTT topic.
- Parameters:
topic – MQTT topic
messageCallback – Callback function
- messageCallback(topic, message)
Callback function for MQTT message.
- Parameters:
topic – MQTT topic
message – Decoded OPC UA message
MQTT publishing
- uaMqtt:createDataset(fields, datasetId)
Create dataset which will describe message content to publish.
- Parameters:
fields –
Array of Fields. Each field is a table with next fields:
name: Field name. Defines field name in the dataset which will be used in JSON format and in metadata for binary messages.
nodeId: nodeId in OPUCA server instance which changes will be listened and published.
The order of fields will be preserved in published MQTT payload messages.
classId – Class ID. Is used to distinguish datasets at subscriber side.
- uaMqtt:setValue(datasetId, fieldName, dataValue)
Set field value in the dataset.
- Parameters:
datasetId – Dataset ID
fieldName – Field name
dataValue – Field value
- uaMqtt:publish(dataTopic, publisherId)
Publish dataset. Will forcibly publish dataset with all the changed fields.
- Parameters:
dataTopic – Data topic
publisherId – Publisher ID
- uaMqtt:startPublishing(dataTopic, publisherId, periodMs)
Start publishing dataset periodically with changed fields.
- Parameters:
dataTopic – Data topic
publisherId – Publisher ID. string or numeric value.
periodMs – Period in milliseconds
- uaMqtt:stopPublishing()
Stop publishing dataset.