Recent

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

PaulRowntree

  • New member
  • *
  • Posts: 18
    • 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: 1013
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

  • New member
  • *
  • Posts: 18
    • 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

  • New member
  • *
  • Posts: 18
    • 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