I struggle for days with strange behaviour reading payload from a TMSMQTT Client component.
I found that misteriously, TMSMQTTClient was not able to read correctly the payload, injecting
lots of garbage into the program stream.
I then add another two components from two different software libraries, with the same identical parameters connections and subscribes and I found that there were no erros at all in those readings.
So after wasting a lot of time trying to find errors in other part of the application I was developing, I finally found that TMSMQTT is faulty!
I tried to get an answer from TMS Support, but no solution from them!
Just a screenshot below:
And here is the code:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, TMS.MQTT.Global, Vcl.StdCtrls,
Vcl.ExtCtrls, TMS.MQTT.Client, iotcore, iottypes, iotmqtt, TMS.MQTT.Logging;
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, TMS.MQTT.Global, Vcl.StdCtrls,
Vcl.ExtCtrls, TMS.MQTT.Client, iotcore, iottypes, iotmqtt, TMS.MQTT.Logging;
type
TForm1 = class(TForm)
TMSMQTTClient1: TTMSMQTTClient;
Memo1: TMemo;
Panel1: TPanel;
BtnConnectTmsMQTT: TButton;
BtnBtnSubscribeTmsMQTT: TButton;
Button3: TButton;
iotMQTT1: TiotMQTT;
BtnConnectIPWorksMQTT: TButton;
BtnSubscribeIPWorksMQTT: TButton;
Memo2: TMemo;
Button6: TButton;
procedure TMSMQTTClient1PublishReceived(ASender: TObject; APacketID: Word;
ATopic: string; APayload: TArray<System.Byte>);
procedure BtnConnectTmsMQTTClick(Sender: TObject);
procedure BtnBtnSubscribeTmsMQTTClick(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure BtnConnectIPWorksMQTTClick(Sender: TObject);
procedure iotMQTT1MessageIn(Sender: TObject; PacketId: Integer;
const Topic: string; QOS: Integer; Message: string;
MessageB: TArray<System.Byte>; Retained, Duplicate: Boolean);
procedure BtnSubscribeIPWorksMQTTClick(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TForm1 = class(TForm)
TMSMQTTClient1: TTMSMQTTClient;
Memo1: TMemo;
Panel1: TPanel;
BtnConnectTmsMQTT: TButton;
BtnBtnSubscribeTmsMQTT: TButton;
Button3: TButton;
iotMQTT1: TiotMQTT;
BtnConnectIPWorksMQTT: TButton;
BtnSubscribeIPWorksMQTT: TButton;
Memo2: TMemo;
Button6: TButton;
procedure TMSMQTTClient1PublishReceived(ASender: TObject; APacketID: Word;
ATopic: string; APayload: TArray<System.Byte>);
procedure BtnConnectTmsMQTTClick(Sender: TObject);
procedure BtnBtnSubscribeTmsMQTTClick(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure BtnConnectIPWorksMQTTClick(Sender: TObject);
procedure iotMQTT1MessageIn(Sender: TObject; PacketId: Integer;
const Topic: string; QOS: Integer; Message: string;
MessageB: TArray<System.Byte>; Retained, Duplicate: Boolean);
procedure BtnSubscribeIPWorksMQTTClick(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BtnConnectTmsMQTTClick(Sender: TObject);
begin
begin
TMSMQTTClient1.ClientID := 'jnckljsdnlasncdol';
TMSMQTTClient1.KeepAliveSettings.KeepAliveInterval := 30;
TMSMQTTClient1.LastWillSettings.Topic := 'wills/' + TMSMQTTClient1.ClientId;
TMSMQTTClient1.LastWillSettings.WillMessage := TMSMQTTClient1.ClientId + ' was disconnected ungracefully!';
TMSMQTTClient1.BrokerHostName := '192.168.1.6';
TMSMQTTClient1.BrokerPort := 1883;
TMSMQTTClient1.Connect(True);
TMSMQTTClient1.KeepAliveSettings.KeepAliveInterval := 30;
TMSMQTTClient1.LastWillSettings.Topic := 'wills/' + TMSMQTTClient1.ClientId;
TMSMQTTClient1.LastWillSettings.WillMessage := TMSMQTTClient1.ClientId + ' was disconnected ungracefully!';
TMSMQTTClient1.BrokerHostName := '192.168.1.6';
TMSMQTTClient1.BrokerPort := 1883;
TMSMQTTClient1.Connect(True);
end;
procedure TForm1.BtnBtnSubscribeTmsMQTTClick(Sender: TObject);
begin
TMSMQTTClient1.Subscribe('#', qosAtMostOnce);
end;
begin
TMSMQTTClient1.Subscribe('#', qosAtMostOnce);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
begin
TMSMQTTClient1.Disconnect;
end;
procedure TForm1.BtnConnectIPWorksMQTTClick(Sender: TObject);
begin
begin
ioTMQTT1.ClientId := 'baubaublckjblbqei';
ioTMQTT1.CleanSession := true;
ioTMQTT1.KeepAliveInterval := 30;
ioTMQTT1.WillTopic := 'wills/' + ioTMQTT1.ClientId;
ioTMQTT1.WillMessage := ioTMQTT1.ClientId + ' was disconnected ungracefully!';
ioTMQTT1.Connect('192.168.1.6',1883);
ioTMQTT1.CleanSession := true;
ioTMQTT1.KeepAliveInterval := 30;
ioTMQTT1.WillTopic := 'wills/' + ioTMQTT1.ClientId;
ioTMQTT1.WillMessage := ioTMQTT1.ClientId + ' was disconnected ungracefully!';
ioTMQTT1.Connect('192.168.1.6',1883);
end;
procedure TForm1.BtnSubscribeIPWorksMQTTClick(Sender: TObject);
begin
// Basic, subscribe to some topic filters, all at the same QoS level.
ioTMQTT1.Subscribe('#', 1);
end;
begin
// Basic, subscribe to some topic filters, all at the same QoS level.
ioTMQTT1.Subscribe('#', 1);
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
ioTMQTT1.Disconnect;
end;
begin
ioTMQTT1.Disconnect;
end;
procedure TForm1.iotMQTT1MessageIn(Sender: TObject; PacketId: Integer;
const Topic: string; QOS: Integer; Message: string;
MessageB: TArray<System.Byte>; Retained, Duplicate: Boolean);
begin
Memo2.Lines.Add(TEncoding.UTF8.GetString(MessageB));
end;
const Topic: string; QOS: Integer; Message: string;
MessageB: TArray<System.Byte>; Retained, Duplicate: Boolean);
begin
Memo2.Lines.Add(TEncoding.UTF8.GetString(MessageB));
end;
procedure TForm1.TMSMQTTClient1PublishReceived(ASender: TObject;
APacketID: Word; ATopic: string; APayload: TArray<System.Byte>);
begin
Memo1.Lines.Add(TEncoding.UTF8.GetString(APayload));
end;
APacketID: Word; ATopic: string; APayload: TArray<System.Byte>);
begin
Memo1.Lines.Add(TEncoding.UTF8.GetString(APayload));
end;
end.