MQTT

MQTT

Introduction

MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol designed specifically for IoT (Internet of Things) environments. It operates on a publish-subscribe model, allowing devices to publish messages to topics and other devices to subscribe to those topics to receive the messages. MQTT is well-suited for scenarios with limited bandwidth and intermittent connectivity, making it an efficient solution for IoT deployments. Its lightweight nature minimizes protocol overhead, optimizing data transmission and conserving network resources. With MQTT, you can establish reliable and scalable communication between devices and servers, enabling seamless integration and data exchange within your IoT ecosystem.

Key Concepts and Terms

  • Host: The name or IP address of the MQTT broker that is being used. When connecting to an MQTT broker, you will need to specify the host address.
  • Port: The port number identifies the specific MQTT broker service running on the host machine. When connecting to an MQTT broker, you will also need to specify the port number.
  • Username: A unique identifier used to authenticate a user when connecting to an MQTT broker. This is an optional input, depending on the security settings of the broker.
  • Password: A secret code used to authenticate a user when connecting to an MQTT broker. This is also an optional input, depending on the security settings of the broker.
  • Client ID: A unique identifier used to identify a specific MQTT client when connecting to an MQTT broker. Each client that connects to the broker must have a unique client ID.
  • Connection Timeout Seconds: The maximum amount of time that an MQTT client will wait for a connection to be established with the broker. If a connection is not established within this time period, the client will abort the connection attempt.
  • Keep Alive Seconds: The amount of time between MQTT client and broker exchanges of packets that ensure the connection is still active. If the client does not receive any packets from the broker within this time period, it will assume that the connection has been lost and attempt to reconnect.
  • Flatten Modeled Values: The process of converting a complex data structure into a simpler, flattened structure that can be more easily transmitted over an MQTT network. This is particularly useful when sending data from IoT devices that may have limited processing power and memory.
  • Topic: A string used to identify a specific MQTT message or set of messages. MQTT clients can subscribe to specific topics to receive messages that are published to those topics.
  • Quality of Service (QoS): Defines the reliability of message delivery in MQTT. There are three QoS levels:
    • QoS 0: Provides the least reliability, with messages being delivered only once, without any acknowledgment from the receiver.
    • QoS 1: Ensures guaranteed delivery of each message at least once, with acknowledgment required from the receiver.
    • QoS 2: Provides the highest level of reliability, with messages being delivered exactly once, with a two-way handshake process for acknowledgment.


Connection Parameters

To establish a connection with an MQTT broker, the following parameters must be accurately entered:

Parameter

Description

Data Type

Host

The name or IP address of the MQTT broker

String

Port

The port number on which the MQTT broker listens

Int

Username (Optional)

Username to connect to the MQTT broker

String

Password (Optional)

Password to connect to the MQTT broker

String

Client ID (Optional)

Client ID for the connection

String

ConnectionTimeoutSeconds

Timeout in seconds for connection attempt

Int

KeepAliveSeconds

Keep alive time in seconds

Int

FlattenModeledValues

Whether to flatten modeled values

Bool

Inputs

Once the connection with the MQTT broker has been established successfully, the user can subscribe to a specific topic or publish messages to a topic using the appropriate parameters. The "input" page in MQTT typically involves specifying the necessary details related to the topic, message payload, and quality of service (QoS).

Parameter

Description

Data Type

Topic

Topic to subscribe to

String

To view the results of the MQTT subscription or publication, the user can use a client library or an MQTT client tool to monitor the subscribed topic or published messages. The client library or tool will typically display the incoming or outgoing messages in a result window or log. By defining the MQTT topic and selecting the appropriate QoS level, users can effectively subscribe to an MQTT broker. Once the input has been defined, the user can execute the query and view the results to ensure that the data has been successfully transferred to the target table.



Subscribing to Multiple Topics

If multiple topics are to be subscribed to using topics that can be distinguished by index, the index button can be activated. After the index button is opened, words are added from the Word list that appears on the screen. The starting point for the relevant index is written in Min Index. The endpoint is added in Max Index and finally, the increment value is selected for how much the increase will be between min and max. After all these selections, the relevant index numbers can be individually selected from the bottom list or all of them can be selected using the select all button. After these steps, when "create input" is clicked, multiple inputs will be created at the same time.

Outputs

To publish data with MQTT, it is necessary to define an output. This can be done by selecting the appropriate output connector in the MQTT integration platform and configuring the required parameters. To create an output, specify the required details such as the output name, output topic, and QoS.

Parameter

Description

Data Type

Topic

Topic to publish to

String

QoS

Quality of service (0, 1, or 2)

Int



Using EMQX for MQTT

The EMQX interface operates by default on port 18083. By accessing this address, one can establish a connection to the MQTT broker within Maestro. Once the connection is established, it is possible to subscribe to specific topics and view the data that is published or publish data to those topics. By subscribing to the "demo_mqtt" topic that has been created, the relevant data published to that topic will be displayed on the screen. This screen provides a means to perform pub-sub verification through EMQX, as data flow continues to appear on the screen whenever a new topic is published.






    • Related Articles

    • Azure IoT Hub

      Azure IoT Hub is a managed service by Microsoft that enables secure communication and management of Internet of Things (IoT) devices and data. It acts as a central hub for devices to send telemetry data, receive messages, and execute commands. To ...
    • AWS SiteWise

      AWS IoT SiteWise is an AWS service designed to efficiently collect, organize, and analyze industrial data. It enables users to structure and gather data from industrial devices, securely store it in the AWS Cloud, and gain valuable insights to ...