Table of Contents |
---|
Info |
---|
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
Info |
---|
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
Info |
---|
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.
Additionally, the two tags tag below need needs to be added to the connector if there are points that are writable.
pahoMqttQos (number tag) - value being either 0, 1, or 2. Recommend setting it to 1. There is no difference between those values.pahoMqttConstructor (string tag) - value being the name of the contructor function being used
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 |
pahoMqttPointName (optional - string type): pointName
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 |
pahoMqttPointName (optional - string type): pointNameExamples
functionName | constructor function to output json |
Info |
---|
The |
Examples
Reading a raw value
How to read a value if a 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 from a in json .
format.
Under Advanced Apps, click on Funcs, then create a new function, name it as you prefer and paste the below code:
Code Block |
---|
(msg, params : null) => do json: ioReadJson(msg) |
return json
end |
Proceed tagging the point:
Here is the json content used in the above example:
Code Block |
---|
{
"jsonValue0": 15,
"jsonValue1": 25
} |
Writing a 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.
That’s it, now we recommend to disable/enable the MQTT connector.
Code Block |
---|
(pt, val) => do if (val != null) do if (val.isDict) return val->writeVal.toStr else return val.toStr end return "NULL" end |