MQTT Connector
This is a community built MQTT connector from Stackhub
Overview
The PahoMqtt Extension implements MQTT client subscription and publication to topics on an MQTT broker where messages on those topics contain current point values. It uses the Eclipse Paho MQTT java client for all MQTT Broker communication. This extension depends on pahoMqtt which is just a wrapper for the runtime Java jar files it requires.
Discovery and learn are not supported functions. As a result, brokers are not discoverable from within the Connectors app and from within the Builder app, the Connectors panel will not show the PahoMqtt Connector.
All PahoMqtt proxy points must be manually bound.
History synchronization is not supported by PahoMqtt. You will need to use History Collected to store history.
Prerequisites
Download and install the latest of these pods for the current framework version being used:
pahoMqtt v1.2.1 - do not use v1.2.2!
pahoMqttExt v1.0.14 - do not use v1.0.15!
Once installed, go to the Settings app → Ext → and enable Paho MQTT.
Creating a MQTT connector
Source documentation available here.
The MQTT connector can be added via the DB Builder → Connectors tree under the PahoMqtt.
The connector URI has the following format:
tcp://[IP]:[port]
where:
IP – the IP address of the MQTT device
port - default for MQTT is 1883
If username is entered, then a password is also expected. These will be supplied to the broker on connection. If you have a non-authenticating broker, just leave username and password blank.
The connector also supports Client Cert based authentication; see source documentation.
Additionally, the tags below need to be added to the connector:
Adding data points
This portion has to be done manually. Each point has to be created and have a reference to the MQTT connector. More documentation from Stackhub about “Parser” and “PointName” can be found here and look for those sections.
First, the user needs to create a local point in DB Builder.
Then add the following tags to read the current value:
Tag name | Required | Tag type | Details | Notes |
---|---|---|---|---|
| yes | string | /point/current/topic |
|
| yes | reference | @id_of_mqtt_connector |
|
| optional | string | pointName |
|
| optional | string | functionName | parses the json into readable content |
To make a point writable, it needs to have these additional tags.
Tag name | Required | Tag type | Details | Notes |
---|---|---|---|---|
| yes | string | /point/write/topic |
|
| optional | string | pointName |
|
| optional | string | functionName | function to construct the writing message |
Examples
Reading a raw value
How to read a value if the topic contains just a raw value.
Reading a raw value with multiple variables
How to read a value based on the variable name (if the topic contains more variable names).
Reading a json value
How to read the value in json format.
Under Advanced Apps, click on Funcs, then create a new function, name it as you prefer and paste the below code:
(msg, params : null) => do
json: ioReadJson(msg)
return json
end
Proceed tagging the point:
Here is the json content used in the above example:
{
"jsonValue0": 15,
"jsonValue1": 25
}
Writing
Writing a raw value
How to write to a point when sending single values.
Go to the Funcs app
Create a new func called pointWriteSimpleVal
Copy the code to the right and paste into the func and save it.
Create the
pahoMqttConstructor
tag, either in the point to be written or in the connector, and give it the “pointWriteSimpleVal” value.Disable/enable the MQTT connector.
(pt, val) => do
if (val != null) do
if (val.isDict) return val->writeVal.toStr
else return val.toStr
end
return "NULL"
end
Writing a formatted value
How to write to a point when sending formatted values (i.e. in json format).
Go to the Funcs app
Create a new func called pointWriteFormattedVal
Copy the code to the right and paste into the func and save it.
Create the
pahoMqttConstructor
tag, either in the point to be written or in the connector, and give it the “pointWriteFormattedVal” value.Disable/enable the MQTT connector.
The json output will be similar to this, where “jsonValue0” is the pahoMqttPointName
and “15” is the writeVal of the point:
On line 10, you can customize your output format.
Customizing the function will also allow you to bring in more tags from the point, as depicted with the pahoMqttPointName
tag in the example above.