Table of Contents

MQTT

In Mervis IDE, the MQTT protocol is technically implemented as a client driver. However, because Mervis RT can both publish (provide) and subscribe to (receive) data, it effectively operates in both ways. It is included in the server protocols section because it acts as a data provider to other systems via the central Broker.

Data is organized using “Topics,” which are hierarchical structures that act as paths where data is published. For example, a base folder could be building/hvac, with sub-folders like building/hvac/temp.

(Note on Organization: Mervis supports Prototypes to help you systematically organize your naming conventions across large projects. While we won't cover them deeply here, they are highly recommended for keeping your variables clean.)

Step 1: Setting Up the Broker Connection

First, we need to establish the communication link between your Mervis device and the MQTT broker.

Setting the TCP and MQTT device parameters

1. Configure TCP Parameters

2. Configure Device Parameters

Useful Macros When setting up your Client ID or Topics, you can use case-insensitive macros to automatically pull in system data. Using these ensures you have the full context without typing repetitive names.

Macro Description
ClientId Injects the defined MQTT Client ID.
PlcName Injects the assigned name of the PLC.
system.rtc Injects the system's Real-Time Clock (local time).
system.rtcutc Injects the system's Real-Time Clock in Coordinated Universal Time (UTC).

3. Set a Sensible Read/Write Interval (CRITICAL)

Step 2: Organizing Read and Write Groups

Mervis RT organizes communication by separating incoming data from outgoing data.

The Variable Browser showing datapoints cleanly separated into Read_Group and Write_Group

1. Read Group (Subscribing)

Mervis RT uses Read Groups to subscribe to a topic, parse the document, and update values in your program.

Read Group Properties

2. Write Group (Publishing) & Templates

Mervis RT uses this group to create a document and send it out to the broker.

Write Group Properties

Step 3: Creating and Mapping Data Points

(Note for new users: The process of mapping data points and handling Enums here is a general feature of Mervis client channels. If you have used Modbus or other protocols in Mervis before, this process will be very familiar.)

1. Map Incoming Variables

Inside your Read_Group, create a data point for your input.

Mapping incoming MQTT data to the internal variable

2. Map Outgoing Variables

Inside your Write_Group, create a data point for your result (e.g., Total).

Mapping the calculated internal variable out to the MQTT broker

Step 4: Function Block Logic

With your communication variables mapped, you can use them in standard IEC 61131-3 logic.

A simple FBD program adding logic block inputs together

In a practical application, you might map two similar MQTT inputs (for example, adding Power_Zone_1 and Power_Zone_2 together to calculate a facility's total power consumption). The math block computes the sum, and the result is written directly to your global MQTT output variable (Calculated_Total).

Step 5: Testing the Setup with MQTTX

To ensure everything is working smoothly, use a desktop client like MQTTX to simulate real-world data traffic. This lets you see the direct results of the custom Publish Templates you created in Step 2.

Simulating sensor data in MQTTX