Blog

All Blog Posts  |  Next Post  |  Previous Post

Using TMS MQTT Capabilities to Send Data in an Embarcadero Delphi FMX App

Wednesday, June 4, 2025

Softacom is a certified consulting partner of TMS Software and an official consulting and training partner of Embarcadero. They are experts in migration and modernization software built using legacy languages and platforms or obsolete technologies, using modern programming languages, frameworks and databases.

Previously, Softacom explored how to use FlexCel TMS library features in an Embarcadero Delphi FMX app with an Excel template. But Delphi applications aren’t limited to office automation. They also play an increasing role in modern technologies like smart homes and IoT (Internet of Things).

Today, smart home and IoT (Internet of Things) technologies are widely used in everyday life to control air temperature, manage lighting automatically, and more. For data exchange between embedded systems (such as microcontrollers and single-board computers), the MQTT protocol is becoming increasingly popular. Let's take a brief look at what MQTT is.
Message Queuing Telemetry Transport (MQTT) is a lightweight messaging protocol designed for data transmission between resource-constrained devices and across unreliable networks. It is particularly well-suited for Internet of Things (IoT) applications, where fast, reliable, and energy-efficient communication between sensors, devices, and servers is essential.


Let's look at the features of MQTT:

  • Publisher/Subscriber model: Devices can publish messages to a specific topic, and any other devices subscribed to that topic will receive the messages.
    TMS Software Delphi  Components
  • Lightweight Protocol: With a minimal header size (just 2 bytes) and low overhead, MQTT is ideal for low-power devices and bandwidth-constrained networks.
  • Message Broker: All messages are routed through a central server (broker), which handles the delivery of messages between clients (in our case, a remote Mosquito broker will be used).
  • Security: MQTT supports user authentication and can work via TLS/SSL.

How does it all work? Imagine you have a smart home. A temperature sensor publishes data to the topic home/livingroom/temperature, and a mobile app subscribed to that topic receives real-time updates.

In this article, we will demonstrate the capabilities of the TMS MQTT library for transferring data between two Embarcadero Delphi FMX applications running on different platforms. We’ll create two applications.

The client application publishes a message to the topic ‘softacom/chat’. The server application subscribes to the same topic ‘softacom/chat’.

The client sends messages that include both text and a numeric value, which the server app uses to update the color of its form.

The server application receives both the color and the text message from the client. It updates the form’s color based on the received numeric value. The text message is also displayed to the user.

Let's walk through the installation and setup of the TMS MQTT library. To do this, visit the official page: https://www.tmssoftware.com/site/tmsmqtt.asp

To download the trial version, click the “FREE TRIAL” button.
TMS Software Delphi  Components
 
After selecting your version of Embarcadero Delphi (in our case, version 12), click “DOWNLOAD.”
 TMS Software Delphi  Components

Once the installer has finished downloading, run it and click “Next.”
TMS Software Delphi  Components

Next, accept the terms of the license agreement and click “Next.”
TMS Software Delphi  Components

Now, you’ll need to get a trial key for the TMS MQTT library. Click “Request Trial Key.”
 TMS Software Delphi  Components

Now, you need to enter your name (it can be any name) and email address (your own) to receive the license key. Then, click “SUBMIT.”
 TMS Software Delphi  Components

A license key will be sent to your email
 TMS Software Delphi  Components

Open an incoming message from TMS.
 TMS Software Delphi  Components

Then enter your Registration Email and Registration Code and click “Next.”
 TMS Software Delphi  Components

Wait for the TMS MQTT library installation to complete.

We use the remote Mosquitto MQTT broker (test.mosquitto.org) for both client and server. In this example, port 1883 is used, with no encryption or authorization enabled.

 TMS Software Delphi  Components

Let's take a look at the Embarcadero Delphi FMX client application. The TTMSMQTTClient component is used to send a numeric value (to change the color of the server application form) along with a text message.
 TMS Software Delphi  Components

Keep the TTMSMQTTClient component settings at their defaults. But it is a good idea to verify the BrokerPort property (set it to 1883 if there is a different value). Also, ensure the UseSSL property is set to False (make sure that the checkbox is unchecked).
 TMS Software Delphi  Components

To enter a text message, we use the TEdit component.
TMS Software Delphi  Components

To select the color of the server application’s form, we use a set of related TRadioButton controls.
 TMS Software Delphi  Components

The data packet (consisting of the message and a numeric value for setting the server form’s color) is sent when the “SEND” button (TButton) is clicked.
TMS Software Delphi  Components

To store the topic (as the string constant ChatChannel) and numeric values as an enumeration for setting the color of the server application form (type TTypeColor), let's create a shared module for both the client and server application: projectConsts.pas.
 
TMS Software Delphi  Components
The source code for the projectConsts.pas module is shown below.

unit projectConsts;
interface
type TTypeColor = (RED, GREEN, BLUE, YELLOW, WHITE);
const
  ChatChannel = 'softacom/chat';
implementation
end.

To set the numeric value of the server form color and then pass it to the server application, we declare a private field FColorSelect (type TTypeColor).
 
TMS Software Delphi  Components

In the click handlers of each TRadioButton, we assing a corresponding color value (for example, number 0 for red FColorSelect := TTypeColor.RED, number 4 for white FColorSelect :=TTypeColor.WHITE).

TMS Software Delphi  Components
 
In the onCreate method of the main form, we set a connection to the remote Moscuitto broker.

TMS Software Delphi  Components
 
In the onClick handler of the “SEND” button, we publish a message containing the client application’s ID (TMSMQTTClient.ClientID, generated automatically), the text message (EditMessage.Text), and the value of the selected color (Integer(FColorSelect).ToString).
 
These three parameters are separated by the ‘#’ symbol. We assign the value of the string with three parameters and ‘#’ separators to the local string variable StrSend. Finally, we publish the message via MQTT (TMSMQTTClient.Publish(ChatChannel, StrSend)).

TMS Software Delphi  Components

The program code of the Main.pas module is shown below.

unit Main;
interface
uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Memo.Types,
  FMX.ScrollBox, FMX.Memo, FMX.Controls.Presentation, FMX.StdCtrls,
  TMS.MQTT.Global, TMS.MQTT.Client, FMX.Edit, ProjectConsts;
type
  TMQTTClientMainForm = class(TForm)
    BtnSend: TButton;
    TMSMQTTClient: TTMSMQTTClient;
    EditMessage: TEdit;
    MQTTLabel: TLabel;
    MQTTToolBar: TToolBar;
    TitleLabel: TLabel;
    LabelSelect: TLabel;
    RadioRed: TRadioButton;
    RadioGreen: TRadioButton;
    RadioBlue: TRadioButton;
    RadioYellow: TRadioButton;
    RadioWhite: TRadioButton;
    procedure FormCreate(Sender: TObject);
    procedure BtnSendClick(Sender: TObject);
    procedure RadioRedClick(Sender: TObject);
    procedure RadioGreenClick(Sender: TObject);
    procedure RadioBlueClick(Sender: TObject);
    procedure RadioWhiteClick(Sender: TObject);
    procedure RadioYellowClick(Sender: TObject);
  private
    { Private declarations }
    FColorSelect : TTypeColor;
  public
    { Public declarations }
  end;
var
  MQTTClientMainForm: TMQTTClientMainForm;
implementation
{$R *.fmx}
procedure TMQTTClientMainForm.BtnSendClick(Sender: TObject);
var StrSend : string;
begin
  StrSend := TMSMQTTClient.ClientID + '#' + EditMessage.Text + '#'
    + Integer(FColorSelect).ToString;
  TMSMQTTClient.Publish(ChatChannel, StrSend);
end;
procedure TMQTTClientMainForm.FormCreate(Sender: TObject);
begin
  TMSMQTTClient.BrokerHostName := 'test.mosquitto.org';
  TMSMQTTClient.Connect();
end;
procedure TMQTTClientMainForm.RadioRedClick(Sender: TObject);
begin
  FColorSelect := TTypeColor.RED;
end;
procedure TMQTTClientMainForm.RadioGreenClick(Sender: TObject);
begin
  FColorSelect := TTypeColor.GREEN;
end;
procedure TMQTTClientMainForm.RadioBlueClick(Sender: TObject);
begin
  FColorSelect := TTypeColor.BLUE;
end;
procedure TMQTTClientMainForm.RadioYellowClick(Sender: TObject);
begin
  FColorSelect := TTypeColor.YELLOW;
end;
procedure TMQTTClientMainForm.RadioWhiteClick(Sender: TObject);
begin
  FColorSelect :=TTypeColor.WHITE;
end;
end.
Let's look at the Embarcadero Delphi FMX server application. The TTMSMQTTClient component is used to subscribe to the ‘softacom/chat’ topic in order to receive a numeric value for changing the form color and a text message from the client. The component is configured similarly to the client application. You can use the default settings.
 
TMS Software Delphi  Components

In the onCreate method of the main form, we connect to the Moscuitto broker, just like in the client application
 
TMS Software Delphi  Components

We will change the form’s color based on the numeric values received from the client. To enable this, we need to adjust some settings. 
In the onShow method, set the form’s fill style to solid: (MQTTServerForm.Fill.Kind := TBrushKind.Solid). 
Also, set the default color for the form: (MQTTServerForm.Fill.Color := TAlphaColors.Antiquewhite).

TMS Software Delphi  Components
 
To set the color based on the numeric value received from the client via MQTT, we create an array of colors using the TAlphaColor type.

TMS Software Delphi  Components

To subscribe to the topic ‘softacom/chat’ (stored in the string constant ChatChannel), we use the OnConnectedStatusChanged event of the TTMSMQTTClient component. The input parameter for the TMSMQTTClient.Subscribe method will be our topic 'softacom/chat' (string constant ChatChannel).

TMS Software Delphi  Components

In the OnPublishReceived event of TTMSMQTTClient, we handle incoming messages from the client. We declare the following local variables:
  • Msg: a string that contains the full message received from the client, with parameters separated by ‘#’;
  • SArr: a typed dynamic array (type TArray<string>) that contains parameters received from the client (client ID, message and numeric value for setting the form color) without ‘#’ delimiters;
  • ColorCode: an integer that will store the color number of the main form in the Colors array.

The APayload parameter (type TBytes) contains the message from the client in binary format. The line Msg := TEncoding.UTF8.GetString(APayload) converts the message to string format.
 
The code fragment SArr := Msg.Split([‘#’]), specifically the Split method, allows extracting parameters (client ID, text message and form color number for the form) from the string received from the client.
 
The Split method essentially breaks the string into parts based on the delimiter (in our case, ‘#’).

The individual parts of the received string are stored in the SArr array. You can retrieve specific parameters by indices.
 
For example, the color number in the Colors array can be obtained with index 2: (ColorCode := Integer.Parse(SArr[2])). 
Client ID and text message are stored at indices 0 and 1, respectively: (ShowMessage(‘ID : ’ + SArr[0] + #10#13 + ‘Message : ’ + SArr[1])).
 
In this example, ShowMessage displays the client ID and the text message to the user. The following line sets the form color according to the client’s selection: MQTTServerForm.Fill.Color := Colors[TTypeColor(ColorCode)] 

TMS Software Delphi  Components

Program code of the Main.pas module for the server application:
unit Main;
interface
uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Memo.Types,
  FMX.ScrollBox, FMX.Memo, FMX.Controls.Presentation, FMX.StdCtrls,
  TMS.MQTT.Global, TMS.MQTT.Client, ProjectConsts;
const
    Colors: array[TTypeColor] of TAlphaColor = (TAlphaColors.Red,
      TAlphaColors.Green, TAlphaColors.Blue,
      TAlphaColors.Yellow, TAlphaColors.White);
type
  TMQTTServerForm = class(TForm)
    TMSMQTTClient: TTMSMQTTClient;
    procedure FormCreate(Sender: TObject);
    procedure TMSMQTTClientConnectedStatusChanged(ASender: TObject;
      const AConnected: Boolean; AStatus: TTMSMQTTConnectionStatus);
    procedure TMSMQTTClientPublishReceived(ASender: TObject; APacketID: Word;
      ATopic: string; APayload: TBytes);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  MQTTServerForm: TMQTTServerForm;
implementation
{$R *.fmx}
procedure TMQTTServerForm.FormCreate(Sender: TObject);
begin
  TMSMQTTClient.BrokerHostName := 'test.mosquitto.org';
  TMSMQTTClient.Connect();
end;
procedure TMQTTServerForm.FormShow(Sender: TObject);
begin
  MQTTServerForm.Fill.Kind := TBrushKind.Solid;
  MQTTServerForm.Fill.Color := TAlphaColors.Antiquewhite;
end;
procedure TMQTTServerForm.TMSMQTTClientConnectedStatusChanged(ASender: TObject;
  const AConnected: Boolean; AStatus: TTMSMQTTConnectionStatus);
begin
  if AConnected then
  begin
    TMSMQTTClient.Subscribe(ChatChannel);
  end;
end;
procedure TMQTTServerForm.TMSMQTTClientPublishReceived(ASender: TObject;
  APacketID: Word; ATopic: string; APayload: TBytes);
var
  SArr: TArray<string>;
  Msg: string;
  ColorCode : Integer;
begin
  Msg := TEncoding.UTF8.GetString(APayload);
  SArr := Msg.Split(['#']);
  ColorCode := Integer.Parse(SArr[2]);
  MQTTServerForm.Fill.Color := Colors[TTypeColor(ColorCode)];
  ShowMessage('ID : ' + SArr[0] + #10#13 + 'Message : ' + SArr[1]);
end;
end.

Let's test how the Embarcadero Delphi FMX client and server applications work on the Windows platform.
 TMS Software Delphi  Components

TMS Software Delphi  Components
 
Client application (Android platform). Server application (MS Windows platform).
TMS Software Delphi  Components

TMS Software Delphi  Components
 
Written by Softacom, reviewed by Bruno Fierens.


Aaron Decramer




This blog post has not received any comments yet.



Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post