MQTT Client

<< Click to Display Table of Contents >>

Navigation:  Multi-Protocol MasterOPC Server > Multi-Protocol MasterOPC Server > MQTT Client >

MQTT Client

MQTT is a simplified network protocol for operation in TCP/IP networks. The main protocol destination is the devices IoT (Internet of Things).

The protocol is used for message exchange by a principle "publisher-subscriber"; at that, an intermediate server named "broker" is used. As a MQTT server, can act a cloud service (in this case, a domain name of the server should be specified in the "IP address" field), or a local MQTT server, for example, Mosqitto (in this case, its IP address should be specified). The delivery kit contains a demo configuration Demo_MQTT.mbp with connection to the cloud service.

Settings of MQTT client connection

To connect the built-in MQTT client to set Connection=TRUE in the MQTT Client section of the Dialog of Server Parameters. This opens extra settings of connection to a MQTT server.

MQTT-klient

IP адрес - IP address or domain name of a MQTT server (broker)

IP порт - exchange port; 1883 is used by default

Keepalive interval (s) - interval of sending a test request in order to check connection state

User ID - compound string with a field-identifier of a device. That string is defined by broker parameters.

Clean Session - if True, a broker clears all information when terminating a session and disconnecting a client. If False, data are saved.

User Name - user name

Password - password

Publish QoS - type of QoS transferred. The following variants are available: 0 - without confirmation of reception, 1 - with confirmation of reception, 2 - with confirmation of confirmation of reception.

Publish retain - transfer of the Retain flag. Retain is a flag of necessity to store the message. If TRUE, a broker stores the last message received by subscription. For example, the client A transferred data to the broker, and, after that, the client B subscribed to it. If the flag is set on, the client B receives the data.

Subscribe QoS - type of QoS received. The following variants are available: 0 - without confirmation of reception, 1 - with confirmation of reception, 2 - with confirmation of confirmation of reception.

Will - this string is transferred to a broker. And, when disconnecting a MQTT client, the broker sends a broadcast request to all subscribers. The string consists of the following test fields: Will topic, Will payload, Will retain, Will QoS.

Reconnect timeout (s) - if connection fails, an attempt to connect occurs after this time

Topic Publish rule - this setting opens the script editor  

MQTT-klient1

The script contains the default function getMQTTTopicPayload. This function is invoked each time when a value of a tag with transfer to a MQTT server is changed. The following parameters are passed to the function: a name of a tag, its Publish topic, QoS and the Retain flag as well as a value, a quality tag and a timestamp.

The function must return an error flag (if true, data are not transferred), the name topic, data to be transferred (payload), QoS and Retain.

Using built-in functions of the script editor, you can change data transferred; for example, you can transfer not only a value (transferred by default) but a timestamp or a quality tag or any other service data.

MQTT settings in a tag

If the MQTT client is connected, the group MQTT Client is available in parameters of each tag. That group contains the following settings: Publish (transfer of data to broker) and Subscribe (reception of data from broker). If both settings are ON, the following settings are available:

MQTT-klient2

Publish topic - name of a section (topic), according which a value should be transferred

Publish on change - data transfer mode. True - transfer by change. False - periodical transfer (period=poll cycle).

Subscribe topic - name of a section (topic), according which a value should be received to a tag. In this case, the tag must have the access type ReadWrite.