MQTT

Connecting IOT systems with realvirtual.io

This interface is only included in realvirtual.io Professional. The interface depends on BestHTTPV3 and BestMQTTV3, which must be purchased separately from the Unity Asset Store (https://assetstore.unity.com/packages/tools/network/best-http-267636 and

https://assetstore.unity.com/packages/tools/network/best-mqtt-268762)

For backwards compatibility also the old versions BestHTTP/2 and Best MQTT v1 can be used but they are not longer available on the Unity Asset Store.

MQTT is a lightweight, publish-subscibe, machine to machine networking protocol for messaging (see https://en.wikipedia.org/wiki/MQTT). MQTT is one of the most important communication standards in IOT. It is lightweight and provides a simple publishing and subscription mechanism. All clients connected to an MQTT network communicate with an MQTT broker. MQTT messages can be transported over TCP-IP or websocket. You can install a local MQTT broker (e.g. Mosquitto is a very common https://mosquitto.org) or use a hosted MQTT server in the cloud (e.g. like HIVEMQ https://www.hivemq.com/mqtt-cloud-broker/).

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

Prerequisites

To utilize the MQTT interfaces, the following steps are required:

  1. Asset Store Purchases: Acquire both BestHTTP V3 and BestMQTT V3 from the Unity Asset Store. These assets provide the necessary foundation for implementing MQTT communication within your projects.

  2. Compiler Defines: After installing the aforementioned packages, it's essential to define compiler symbols in your project settings. Add REALVIRTUAL_BESTHTTP3 and REALVIRTUAL_BESTMQTT3 as compiler defines. This step ensures that your project recognizes and integrates the functionality of these assets correctly.

Signal subscription and import

You can import all MQTT topics from the MQTT broker by running * Import Signals * without having any child objects under the MQTT interface. If the MQTT broker has many topics, this can generate a lot of signals. All signals will be standard PLCOutputs (inputs to realvirtual.io). You can change the signal direction after importing if necessary.

It is also possible to define signals manually. The name of the gameobject can be changed to a name. In this case the property Name of the PLCOutput or PLCInput must be the subject. If Name is emtpy, realvirtual.io expects the name of the gameobject to be the name of the topic.

Another way is to use an empty Gameobject to define one or more Topics to be subscribed. A topic can also contain wildcards - e.g. outputs/# will apply to all topics subscribed under outputs. If you press *Import Signals *, only the topics under the defined topics will be imported.

Properties

IsConnected (Readonly)

True if the client is connected to an MQTT broker.

Broker

The address of the broker.

Port

The communication port with the broker.

DebugMode

True for additional console protocols.

Websocket

True if you want to communicate with websockets, if False the communication is done via TCP-IP.

MQTT5

True if the MQTT5 standard is used. If False, MQTT3.1.1 is used.

Send cycle Ms

if Send cycle is 0, all changed PLCInput signals are automatically published to the MQTT broker. This can lead to a very high communication load, especially if there are signals that change with every Unity physics update. You can set a number of milliseconds here. All changed signals are collected within this defined cycle and after this cycle only one message per signal is sent to the broker.

Security

If True, TLS and Username and Password can be used.

Username

The username. If empty, no username and password will be specified.

Password

The password for the MQTT broker.

TLS

If set to true, TLS encryption is used for communication with the MQTT server.

Delete last session packets

All messages are stored by the realvirtual.io client until they are acknowledged by the server. If the communication is interrupted, there may still be some unacknowledged messages. When you restart the client, the last - unsent - messages will be sent automatically. If this property is set to TRUE, no messages are stored between the different connections and the client always starts with an empty message queue.

© 2022 in2Sight GmbH https://realvirtual.io - All rights reserved. No part of this publication may be reproduced, distributed, or transmitted in any form or by any means, including printing, saving, photocopying, recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

Last updated