Recent

Author Topic: fp MQTT with user/password support ?  (Read 6421 times)

PaulRowntree

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
fp MQTT with user/password support ?
« on: December 02, 2018, 05:10:26 am »
Hi!I have been looking for a Laz/fp MQTT package that will connect to an external MQTT broker; this requires that the package support username and password fields.  The codes that I have seen most often (Delphi ported code by Jamie Ingilby, modified by ZiCog, then modified by heX) [ https://github.com/heX16/mqtt-free-pascal/blob/master/TMQTTClient/MQTT.pas ] does not support these fields, although it looks like some initial code was started in this direction (VariableHeaderConnect ) but not completed and not actually used in the main code.
Does anyone know of a functioning fp MQTT package with this capability? 
Thanks!
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1875
Re: fp MQTT with user/password support ?
« Reply #1 on: December 02, 2018, 10:14:01 am »
Have a look at the MQTT connect specs:
https://www.hivemq.com/blog/mqtt-security-fundamentals-authentication-username-password

You see that you can add a username and password into this connection request.

Example:
Code: Pascal  [Select][+][-]
  1. FH := FixedHeader(MQTT.CONNECT, 0, 0, 0);
  2. VH := VariableHeaderConnect(40);
  3. SetLength(Payload, 0);
  4. AppendArray(Payload, StrToBytes(FClientID, true));
  5. AppendArray(Payload, StrToBytes('username', true));
  6. AppendArray(Payload, StrToBytes('password', true));
  7. RL := RemainingLength(Length(VH) + Length(Payload));
  8. Data := BuildCommand(FH, RL, VH, Payload);
  9.  

PaulRowntree

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
Re: fp MQTT with user/password support ?
« Reply #2 on: December 02, 2018, 11:59:18 pm »
Hi DonAlfredo, and thank you!  Yes, that is the general way to do it. There are also 2 bits that need to be set in the FixedHeader, and I will try this tonight.  Oddly, all versions of the Delphi-forked MQTT code have this in the ReadThread connect code,

 //  Build CONNECT message
          FH := FixedHeader(MQTT.CONNECT, 0, 0, 0);
          VH := VariableHeaderConnect(40);
          SetLength(Payload, 0);
          AppendArray(Payload, StrToBytes(FClientID, True));
          AppendArray(Payload, StrToBytes('lwt', True));           
          AppendArray(Payload, StrToBytes(FClientID + ' died', True));

          RL := RemainingLength(Length(VH) + Length(Payload));
          Data := BuildCommand(FH, RL, VH, Payload);
(BOLD sections highlighted by me) It looks like 'lwt' and FClientID+' died' are hard-coded Will topic/message information to be sent if the connection dies.  Does everyone use this same topic/message, or is this a vestigial bit of code?
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

PaulRowntree

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
Re: fp MQTT with user/password support [SOLVED]
« Reply #3 on: December 03, 2018, 07:46:47 am »
Setting the special bits, and appending fields as DonAlfredo suggested, works like a charm. 

Will share code if anyone is interested.Thanks!
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

Anders Gustav

  • Newbie
  • Posts: 2
Re: fp MQTT with user/password support [SOLVED]
« Reply #4 on: February 18, 2019, 09:46:44 pm »
...

Will share code if anyone is interested.Thanks!

Indeed I am! :)

Michael Collier

  • Sr. Member
  • ****
  • Posts: 329
Re: fp MQTT with user/password support [SOLVED]
« Reply #5 on: July 29, 2025, 02:12:32 pm »
Setting the special bits, and appending fields as DonAlfredo suggested, works like a charm. 

Will share code if anyone is interested.Thanks!

I'm interested in the solution to this :)

I can see where to append fields
Code: Pascal  [Select][+][-]
  1. AppendArray(Payload, StrToBytes('username', true));
  2. AppendArray(Payload, StrToBytes('password', true));

But not sure about setting 'special bits' can anyone point me to the code for that please?




cdbc

  • Hero Member
  • *****
  • Posts: 2725
    • http://www.cdbc.dk
Re: fp MQTT with user/password support ?
« Reply #6 on: July 29, 2025, 02:31:44 pm »
Hi
Saw the title... If you're interested in MQTT, there's a thread HERE, that points to a complete MQTT-Framework written in FPC/Lazarus repo on my GitLab, you could have a look at  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

 

TinyPortal © 2005-2018