MQTT (Pro)

Connecting IoT and IIoT systems with realvirtual

Overview

MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe messaging protocol designed for IoT and IIoT communication. It serves as one of the most important communication standards in Industry 4.0, enabling real-time data exchange between shop floor equipment, cloud platforms, and enterprise systems.

The MQTT interface in realvirtual provides seamless integration with MQTT brokers for cloud connectivity and distributed automation systems. It supports both MQTT 3.1.1 and MQTT 5.0 protocols, with options for WebSocket transport, TLS encryption, and automatic reconnection handling.

Key Features

  • Support for MQTT 3.1.1 and MQTT 5.0 protocols

  • TCP and WebSocket transport options

  • TLS/SSL encryption for secure communication

  • Username/password authentication

  • Automatic signal discovery and import

  • Hierarchical topic organization

  • Batched message publishing for performance optimization

  • Background thread communication for optimal Unity performance

  • Topic filtering with MQTT wildcards

  • Real-time bidirectional communication

Compatible MQTT Brokers

The interface works with all standard MQTT brokers including:

  • Mosquitto - Popular open-source broker (mosquitto.org)

  • HiveMQ - Enterprise-grade cloud broker (hivemq.com)

  • AWS IoT Core - Amazon's managed IoT service

  • Azure IoT Hub - Microsoft's cloud IoT platform

  • Local brokers - For on-premise installations

Adding MQTT Interface

The MQTT interface can be added to a realvirtual scene by selecting realvirtual > Add Object > Interface > MQTT.

MQTT Interface Inspector

The MQTT interface provides comprehensive configuration options through the Unity Inspector, including connection settings, security options, signal management, and real-time monitoring.

Properties

Connection Settings

Broker (string) - Address of the MQTT broker. This can be a hostname (e.g., test.mosquitto.org), IP address (e.g., 192.168.1.100), or localhost for local brokers.

Port (int) - MQTT communication port. Default is 1883 for unencrypted TCP connections. Common port configurations:

  • 1883: Standard MQTT over TCP

  • 8883: MQTT over TLS/SSL

  • 8080/8083: MQTT over WebSocket (varies by broker)

Websocket (boolean) - When enabled, uses WebSocket transport instead of TCP. This is required for web builds and useful for bypassing firewall restrictions that block standard MQTT ports.

MQTT5 (boolean) - Enables MQTT version 5.0 protocol features. When disabled, uses MQTT 3.1.1 which has broader compatibility with older brokers.

Send Cycle Ms (int) - Batch duration in milliseconds for signal updates. When set to 0, changes are sent immediately. Values above 0 batch multiple signal updates together for improved performance, reducing network overhead when many signals change rapidly.

Security Settings

Security (boolean) - Master switch for security features. When enabled, allows configuration of authentication credentials and TLS encryption.

UserName (string) - Username for MQTT broker authentication. Leave empty for anonymous connections. Only visible when Security is enabled.

Password (string) - Password for authenticated connections. Only visible when Security is enabled.

TLS (boolean) - Enables TLS/SSL encryption for secure communication. The interface accepts self-signed certificates for development environments. Only visible when Security is enabled.

Delete Last Session Packets (boolean) - Controls clean session behavior. When enabled, starts with a fresh session discarding any queued messages from previous connections. When disabled, retains and delivers unacknowledged messages from the last session.

Signal Import Settings

Use Topic Filters (boolean) - Enables selective topic import using MQTT wildcard patterns. When disabled, imports all available topics from the broker.

Import Topic Filters (list) - MQTT wildcard patterns for selective signal import. Supports standard MQTT wildcards:

  • + matches a single level (e.g., sensor/+/temperature)

  • # matches multiple levels (e.g., factory/line1/#) Only visible when Use Topic Filters is enabled.

Import As Hierarchy (boolean) - When enabled, creates a GameObject hierarchy matching the MQTT topic structure. Topics like factory/line1/sensor/temp create nested GameObjects. When disabled, creates flat structure with underscores replacing slashes.

Max Import Topics (int) - Limits the number of topics imported to prevent overwhelming the interface with large broker deployments. Set to 0 for unlimited import.

Runtime State

State (readonly) - Current connection status displayed as "Connected" (green) or "Disconnected" (red).

Input Signals (readonly) - Number of PLCInput signals (publishing to MQTT).

Output Signals (readonly) - Number of PLCOutput signals (subscribing from MQTT).

Comm Cycle Ms (readonly) - Current communication cycle time in milliseconds.

Cycle Count (readonly) - Number of completed communication cycles since connection.

Configuration Actions

Update Cycle Ms (int) - Target communication cycle time for the background thread.

Only Transmit Changed Inputs (boolean) - When enabled, only publishes signals that have changed since the last cycle, reducing network traffic.

Auto Reconnect (boolean) - Automatically attempts to reconnect when the connection is lost.

Reconnect Interval Seconds (int) - Time to wait between reconnection attempts.

Max Reconnect Attempts (int) - Maximum number of reconnection attempts before giving up (0 = unlimited).

Debug Mode (boolean) - Enables detailed logging for troubleshooting connection and communication issues.

Signal Management

Automatic Signal Import

The MQTT interface provides powerful signal discovery and import capabilities:

  1. Click the Import Signals button in the Inspector

  2. The interface connects to the broker and subscribes to topics based on your filter configuration

  3. Signals are automatically created for discovered topics with appropriate data types (Bool, Int, Float, or Text)

  4. Topics are mapped to GameObject names with special characters sanitized

Topic Filtering

Use MQTT wildcard patterns in the Import Topic Filters to selectively import signals:

sensor/+/temperature    # All temperature sensors
machine/press/#         # Everything under machine/press
+/+/status             # All status topics at third level
production/line*/+     # Not valid - wildcards must be + or #

Manual Signal Configuration

Signals can also be created manually:

  1. Add child GameObjects under the MQTT interface

  2. Add PLCInput (publish) or PLCOutput (subscribe) components

  3. Set the signal's Name property to the MQTT topic

  4. If Name is empty, the GameObject name is used as the topic

Hierarchical Organization

When Import As Hierarchy is enabled, the topic structure factory/line1/plc/status creates:

MQTT Interface
└── factory
    └── line1
        └── plc
            └── status (with Signal component)

This organization helps manage large numbers of signals in complex automation systems.

Usage Examples

Basic Connection

// Configure MQTT interface programmatically
var mqtt = GetComponent<MQTTInterface>();
mqtt.Broker = "test.mosquitto.org";
mqtt.Port = 1883;
mqtt.OpenInterface();

Secure Cloud Connection

For cloud brokers requiring authentication:

  1. Enable Security

  2. Enter broker credentials in UserName and Password

  3. Enable TLS for encrypted communication

  4. Set appropriate port (typically 8883 for TLS)

WebSocket for Web Builds

For Unity WebGL builds or firewall-restricted environments:

  1. Enable Websocket

  2. Use WebSocket port (typically 8080 or 8083)

  3. Ensure broker supports WebSocket connections

Performance Optimization

For high-frequency signal updates:

  1. Set Send Cycle Ms to batch updates (e.g., 100ms)

  2. Enable Only Transmit Changed Inputs

  3. Use topic filters to limit subscriptions

See Also

Last updated