Recent

Author Topic: Can MQTT disconnected Client reconnect to MQTT Broker?  (Read 4679 times)

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 451
  • Programming is FUN only when it works :)
    • Cool Technology
Can MQTT disconnected Client reconnect to MQTT Broker?
« on: March 09, 2018, 10:01:34 pm »
 :(
Hi
Lazarus 1.2.4
FPC 2.6.4
Mosquitto Broker 1.4.15

In my program, I need to use MQTT protocol. So, I downloaded the latest MQTT libraries I can find on the Internet for Lazarus. Plus, I am using the latest and the greatest Mosquitto MQTT for the Broker. Everything works as it should. From my program I am able to PUBLISH topics and also SUBSCRIBE to topics. However, every once in awhile at random, my program's MQTT connection disconnects on its own. Then, I can't get my program to reconnect again, unless I completely shutdown my program and start it again. Then, it reconnects again and communicate with no problem. Keepalive is set to 40 seconds for my program's MQTT Client in the library file.

Why is my program randomly loosing connection to MQTT Broker and Is there anyway at all to reconnect again? I tried to implement re-connection to the MQTT Broker, but it simply fails to connect at all.

Any help will be greatly appreciated. Thanks,

Code: Pascal  [Select][+][-]
  1. unit webinterface;
  2.  
  3. {$mode objfpc}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  9.   ExtCtrls, StdCtrls, ComCtrls, Inifiles, Signals, Alarms, MQTT, MQTTreadthread;
  10.  
  11. type
  12.  
  13.   { TWebFrm }
  14.  
  15.   TWebFrm = class(TForm)
  16.     ApplyBtn: TButton;
  17.     Button1: TButton;
  18.     StatusBar1: TStatusBar;
  19.     Timer1: TTimer;
  20.     Wpassword: TLabeledEdit;
  21.     Whost: TLabeledEdit;
  22.     procedure ApplyBtnClick(Sender: TObject);
  23.     procedure FormCreate(Sender: TObject);
  24.     procedure Timer1Timer(Sender: TObject);
  25.     procedure myPublish(sender:TObject; topic, payload:Ansistring);
  26.     procedure myconnect(Sender:TObject; test:LongInt);
  27.     procedure OnPingMe(sender:TObject);
  28.   private
  29.     { private declarations }
  30.   public
  31.     { public declarations }
  32.     Procedure UpdateSignal;
  33.   end;
  34.  
  35. var
  36.   WebFrm: TWebFrm;
  37.   MQTTClient2:TMQTTClient;
  38.   pingrec, pingsent:Boolean;
  39.   pingmecnt:integer;
  40.  
  41. implementation
  42.  
  43. procedure TWebFrm.ApplyBtnClick(Sender: TObject);
  44. var
  45.   webini:TInifile;
  46. begin
  47.  
  48.   if MQTTClient2.isConnected=false then
  49.   begin
  50.        if MQTTClient2 <> nil then
  51.           MQTTClient2.Free;
  52.        MQTTClient2 := TMQTTClient.Create(Whost.Text,strtoint(Wpassword.Text));
  53.        MQTTClient2.OnPublish:=@myPublish;
  54.        MQTTClient2.OnConnAck:=@myconnect;
  55.        MQTTClient2.OnPingResp:=@OnPingMe;
  56.        MQTTClient2.Connect;
  57.        Timer1.Enabled:=true;
  58.   end;
  59. end;
  60.  
  61. procedure TWebFrm.myPublish(sender: TObject; topic, payload: Ansistring);
  62. var
  63.   InSig:TSignal;
  64. begin
  65.   topic:=ExtractFilename(topic);
  66.   InSig:=FindSignal(topic);
  67.  
  68.   if InSig <> nil then
  69.   if (InSig.f.WebEnabled=true) and (InSig.f.RemoteAccess = traReadWrite) then
  70.   begin
  71.  
  72.       InSig.SetInteger(strtoInt(payload));
  73.   end;
  74. end;
  75.  
  76. procedure TWebFrm.myconnect(Sender: TObject; test:LongInt);
  77. begin
  78.      StatusBar1.Panels[0].Text:='Connected to the MQTT Broker';
  79. end;
  80.  
  81. procedure TWebFrm.OnPingMe(sender:TObject);
  82. begin
  83.      pingrec:=true;
  84.      pingsent:=false;
  85. end;
  86.  
  87. procedure TWebFrm.FormCreate(Sender: TObject);
  88. var
  89.   webini:Tinifile;
  90. begin
  91.   pingsent:=false;
  92.   pingrec:=false;
  93.   pingmecnt:=0;
  94.   stopmessage:=false;
  95.   MQTTClient2 := TMQTTClient.Create('localhost',1883);
  96.   MQTTClient2.OnPublish:=@myPublish;
  97.   MQTTClient2.OnConnAck:=@myconnect;
  98.   MQTTClient2.OnPingResp:=@OnPingMe;
  99.   MQTTClient2.Connect;
  100.  
  101.   if MQTTClient2.isConnected then
  102.   begin
  103.       StatusBar1.Panels[0].Text:='MQTT Client Connected to Broker';
  104.       MQTTClient2.Subscribe('belt/set/#');
  105.       Timer1.Enabled:=true;
  106.   end;
  107.  end;
  108.  
  109. procedure TWebFrm.Timer1Timer(Sender: TObject);
  110. begin
  111.   Timer1.Enabled:=false;
  112.   UpdateSignal;
  113. end;
  114.  
  115. procedure TWebFrm.UpdateSignal;
  116. var
  117.   I, mqsec:Integer;
  118.   stat1,memregstr:string;
  119.   lastAlmState:TAlarmState;
  120.   tmpstr,ValueStr:string;
  121. begin
  122.   if MQTTClient2.isConnected=false then exit;
  123.  
  124.   for I:=0 to SignalList.Count-1 do
  125.   begin
  126.        if (TSignal(SignalList.Items[I]).f.WebEnabled=true) then
  127.        begin
  128.             tmpstr:=TSignal(SignalList.Items[I]).f.TagName;
  129.  
  130.             if (TSignal(SignalList.Items[I]).GetValueStr = 'OFF') then
  131.                ValueStr:='0'
  132.             else if (TSignal(SignalList.Items[I]).GetValueStr='ON') then
  133.                ValueStr:='1'
  134.             else
  135.                ValueStr:= inttostr(round(TSignal(SignalList.Items[I]).AsPercent));
  136.  
  137.             MQTTClient2.Publish('belt/status/'+tmpstr, ValueStr, false);
  138.                         sleep(1);
  139.        end;
  140.   end;
  141.  
  142.   inc(pingmecnt);
  143.  
  144.   //if ping is sent and there is no reply, then try to reconnect.
  145.   if (pingsent=true) and (pingrec=false) then
  146.   begin
  147.      if MQTTClient2<>nil then
  148.      begin
  149.         MQTTClient2.Disconnect;
  150.         MQTTClient2.Free;
  151.      end;
  152.      MQTTClient2 := TMQTTClient.Create('localhost',1883);
  153.      MQTTClient2.OnPublish:=@myPublish;
  154.      MQTTClient2.OnConnAck:=@myconnect;
  155.      MQTTClient2.OnPingResp:=@OnPingMe;
  156.      MQTTClient2.Connect;
  157.   end;
  158.  
  159.   //ping every 5 minutes
  160.   if (pingmecnt>=30) then
  161.   begin
  162.      pingrec:=false;
  163.      pingsent:=true;
  164.      pingmecnt:=0;
  165.      MQTTClient2.PingReq;
  166.   end;
  167.   Timer1.Enabled:=true;
  168. end;
  169.  
  170. initialization
  171.   {$I webinterface.lrs}
  172.  
  173. end.
  174.  
  175.  


Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 451
  • Programming is FUN only when it works :)
    • Cool Technology
Re: Can MQTT disconnected Client reconnect to MQTT Broker?
« Reply #1 on: March 13, 2018, 03:59:25 pm »
Anyone? :)

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: Can MQTT disconnected Client reconnect to MQTT Broker?
« Reply #2 on: March 13, 2018, 04:09:20 pm »
Where can we find the mqtt lib you are using ... there are many ...

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 451
  • Programming is FUN only when it works :)
    • Cool Technology
Re: Can MQTT disconnected Client reconnect to MQTT Broker?
« Reply #3 on: March 13, 2018, 04:29:17 pm »
oh ... okay... Here you go... I attached it to this message...
Thank you for the reply.

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: Can MQTT disconnected Client reconnect to MQTT Broker?
« Reply #4 on: March 14, 2018, 09:20:49 am »
Quote
ExtCtrls, StdCtrls, ComCtrls, Inifiles, Signals, Alarms, MQTT, MQTTreadthread;

And these two : Signals, Alarms ?

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 451
  • Programming is FUN only when it works :)
    • Cool Technology
Re: Can MQTT disconnected Client reconnect to MQTT Broker?
« Reply #5 on: March 15, 2018, 07:17:59 pm »
Those files are not related to MQTT itself, even though they are being used in this file. If I upload those files, then I mind as well upload the other 100 files from my project. LOL.... My question is ONLY related to MQTT and its Client Connections to the broker. 

 

TinyPortal © 2005-2018