Implementing A Blazing Fast IoT Network With MQTT

What is IoT?

The IoT term defines the concept of internet-connected physical devices that can send and/or receive data. To exchange data easily, a protocol of communication must be used in order of any devices can communicate with another one. The only requirement is that all devices must communicate in an agreed way. For IoT, the MQTT protocol is one of the top choices.

What is MQTT?

MQTT is short for Message Queuing Telemetry Transport. It’s a communication protocol Machine-to-Machine (M2M) designed initially for networks with low bandwidth and preferably bi-directional. It was developed for gas and oil industries in 1999, a time when the network bandwidth was typically very low. Its usage was extended to IoT, automation and is used by Facebook for its well-known Messenger product.

Here is an overview of MQTT

See below a schema of a typical MQTT protocol architecture:

Overview of an IoT set up with MQTT

What are MQTT entities?

This architecture is Client/Server with an implementation of publish/subscribe. There are three roles in this protocol :

  • The Broker : The broker acts like the server of a network using MQTT. All messages go through it but its only role is to send messages to the targeted clients.
  • The Publishers : these entities send data.
  • The Subscribers : these entities receive data.

A client application using the MQTT protocol can be a publisher or a subscriber or even both.

How do I build an MQTT message?

To summarize: an MQTT message is built with a title that we name Topic. The Topic is a string written with some rules. Some data can be exchange in addition of the Topic to complete the message. We can draw a parallel between MQTT call and REST call like this by comparing the MQTT Topic with URL endpoint in REST. Subscribers ‘listen’ to a message Topic and Publishers send message with a specified Topic. The Broker forwards messages to Subscribers that listen the specified Topic in the message.

A client of MQTT network can subscribe to multiple Topics and publish multiple Topics too. MQTT is an extremely flexible messaging protocol to use in your Delphi and C++ Builder applications.

What are the rules of writing MQTT Topics?

MQTT Topics are structured in a hierarchy similar to folders and files in a file system using the forward slash ( / ) as a delimiter.

Some exemples

  • /
  • /house
  • /house/livingroom/conditioner
  • /house/livingroom/lights

MQTT protocol introduces some possible wildcard in the Topic :

  • # (hash character) – multi level wildcard
  • + (plus character) -single level wildcard

Then a client subscribing to /house/# will listen all message starting with /house .

A client subscribing to /house/+/lights will listen to all messages which match the “lights” wildcard specifications (for example living room, kitchen bathroom). It can be used to switch on or off all lights of the house.

To develop a Delphi MQTT client, we will use ready-made component provided by TMS Software : TTMSMQTTClient component available for Windows/Android/IOS/Linux. TMS Software provides an installation package with a trial version here.

You can use the TTMSMQTTClient component to easily implement MQTT

Let’s have a look at the component provided by TMS Software.

How do I connect to an MQTT server?

This component provides of course several properties. To connect to a MQTT server the minimum settings to set are :

  • BrokerHostName : host name where the MQTT Broker can be reached. In the demo application the host name is used.
  • BrokerPort : communication port to reach the server, by default the value is 1883
  • ClientID : user must set an unique identifier to assure unicity of the Client on the MQTT network
  • Credentials : some MQTT server may use authentication with login/password. This information must be set in this property.
  • UseSSL : set to true in case of secured channel of communication

When these properties are ready a call to MQTTClient.Connect(); does the job.

This component uses the Indy component suite to make asynchronous connections. An event OnConnectedStatusChanged is fired when the status of connection changed (success or failure).

How can I make my Delphi program subscribe to an MQTT Topic ?

Once the connection is successful, we can subscribe to a topic by calling the method MQTTClient.Subscribe(aTopic);

An event OnSubscriptionAcknowledged to acknowledge the subscription is fired by the component.

How do I publish an MQTT Topic?

When an MQTT client wants to send data, just publish a message by calling the method : MQTTClient.Publish(aTopic, messageData, aQualityofService, aRetain);

  • aTopic is the Topic of the message in string
  • messageData are the additional data exchange with the message, it can be raw data, or formatted in Json or XML. It s a string.
  • aQualityOfService is an enum regarding the quality of the message (qosAtMostOnce , qosAtLeastOnce , qosExactlyOnce)
  • aRetain is a boolean value to indicate if the server must keep the published message and resend it as soon as a new client subscribe to this related Topic. Server just keep last message of the same Topic.

How do I retrieve MQTT messages?

Just implement the event OnPublishReceived or OnPublishReceivedEx. In case of multiple subscription don’t forget to test the Topic received. The additional data is received as bytes but a simple conversion transforms them into string : jsonString := TEncoding.UTF8.GetString(APayload);

What are the benefits of using MQTT in my programs?

As all data exchanges are standardized in the protocol, the developer does not need to worry about how the server is implemented. As a result your Application may work by using any public MQTT server. It’s an huge productivity benefit.

Let’s download a complete demo of automation applications regarding automatic curtain and temperature control in an house. The monitoring is implemented in FireMonkey and can be hosted in a Windows or Android OS app. The room controller is implemented in VCL and can be hosted on Windows. This demo is located at

Try it yourself !

Download the trial version of TMS Software MQTT client and start yourself to explore MQTT capability.