User Tools

Site Tools


mqttics

This is an old revision of the document!


N O T I C E

ICS's SSL is enabled by default, which is required for MQTT to function. However, with SSL enabled, the compiled EXE file will increase significantly. If SSL is not use, it can be disabled by commenting out {$DEFINE USE_SSL} inside \Source\Include\OverbyteIcsDefs.inc file. This is in #273 in ICS v9.4


Using ICS v9.3

  1. Add IcsMQTTClient component

** if not receiving msg from any subscribe topic, set SSLCLISECURITY to sslclisecnone and AUTOSUBSCRIBE to TRUE

To connect:

  MQTTClient.Host := 'hostname';
  MQTTClient.Port := 1883;
  MQTTClient.Activate (true);

To subscribe:

  mqttclient.Subscribe('topic',qtAT_LEAST_ONCE);

*note, DO NOT subscribe immediately after activate. Give some time first, if want to do almost immediate add this setting in mqttclient:

  mqttclient.AutoSubscribe := true;

To Publish:

   MQTTClient.Publish('topic', msgstring , qtAT_LEAST_ONCE, true);

Taken from ICS demo, to show some info on the connection:

procedure TForm1.btnshowClick(Sender: TObject);
var
  j : integer;
  x : cardinal;
begin
  mmo1.Lines.Add ('');
  mmo1.Lines.Add('------ Client ' + string (MQTTClient.Parser.ClientID) + ' -------');
  mmo1.Lines.Add (format ('Username "%s" Password "%s"', [MQTTClient.Parser.Username, MQTTClient.Parser.Password]));
  mmo1.Lines.Add (format ('Keep Alive "%d" Retry Time "%d" Max Retries "%d"', [MQTTClient.Parser.KeepAlive, MQTTClient.Parser.RetryTime, MQTTClient.Parser.MaxRetries]));
  mmo1.Lines.Add (format ('Will Topic "%s" Message "%s" @ %s', [MQTTClient.Parser.WillTopic, MQTTClient.Parser.WillMessage, QosNames [MQTTClient.Parser.WillQos]]));
  mmo1.Lines.Add ('Subscriptions ----');
  for j := 0 to MQTTClient.Subscriptions.Count - 1 do
    begin
      x := cardinal (MQTTClient.Subscriptions.Objects[j]) and $03;
      if (cardinal (MQTTClient.Subscriptions.Objects[j]) shr 8) and $ff = $ff then
        mmo1.Lines.Add ('  "' + MQTTClient.Subscriptions[j] + '" @ ' + QOSNames[TMQTTQOSType (x)] + ' Acked.')
      else
        mmo1.Lines.Add ('  "' + MQTTClient.Subscriptions[j] + '" @ ' + QOSNames[TMQTTQOSType (x)]);
    end;
end;

Below is fully working sample:

unit main;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OverbyteIcsMQTT, OverbyteIcsWndControl,
  OverbyteIcsSslX509Certs,McJSON;
type
  Tiotmqttsql = class(TForm)
    mmo: TMemo;
    mqttclient: TIcsMQTTClient;
    edtpublish: TEdit;
    btnpublish: TButton;
    slx509crts1: TSslX509Certs;
    icsmqtclnt1: TIcsMQTTClient;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure mqttclientMsg(Sender: TObject; aTopic: UTF8String; const aMessage: String; aQos: TMQTTQOSType; aRetained: Boolean);
    procedure btnpublishClick(Sender: TObject);
    procedure mqttclientOnline(Sender: TObject);
    procedure mqttclientOffline(Sender: TObject; Graceful: Boolean);
    procedure mqttclientClientID(Sender: TObject;
      var aClientID: UTF8String);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  iotmqttsql: Tiotmqttsql;

const
   mqtttopic : string = 'topicname';
   mqtthost  : string = 'sample.com';

implementation

{$R *.dfm}

procedure Tiotmqttsql.FormShow(Sender: TObject);
begin
   mmo.lines.clear;
   mqttclient.AutoSubscribe := true;
   mqttclient.host := mqtthost;
   mqttclient.Port := 1883;
   mqttclient.KeepAlive := 10;
   mqttclient.Activate(true);
   mqttclient.Subscribe(mqtttopic,qtAT_LEAST_ONCE);
end;

procedure Tiotmqttsql.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   mqttclient.Destroy;
end;

procedure Tiotmqttsql.mqttclientMsg(Sender: TObject; aTopic: UTF8String; const aMessage: String; aQos: MQTTQOSType; aRetained: Boolean);
begin
   mmo.Lines.Add(FormatDateTime('yyyy-MM-dd',now())+': '+aMessage+#13#10);
end;

procedure Tiotmqttsql.btnpublishClick(Sender: TObject);
begin
   mqttclient.Publish(mqtttopic,edtpublish.Text,qtAT_LEAST_ONCE,true);
end;

procedure Tiotmqttsql.mqttclientOnline(Sender: TObject);
begin
//   mmo.Lines.add('Online');
end;

procedure Tiotmqttsql.mqttclientOffline(Sender: TObject;  Graceful: Boolean);
begin
//   mmo.Lines.add('Offline');
end;

procedure Tiotmqttsql.mqttclientClientID(Sender: TObject; var aClientID: UTF8String);
begin
   mmo.Lines.add('Client ID: '+aClientID);
end;

end.
mqttics.1743342468.txt.gz · Last modified: (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki