Recent

Author Topic: Mosquitto MQTT and Lazarus: how do you receive payload?  (Read 3468 times)

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 426
  • Programming is FUN only when it works :)
    • Cool Technology
Mosquitto MQTT and Lazarus: how do you receive payload?
« on: January 26, 2018, 06:12:32 pm »
 :)
I am really liking this MQTT protocol and am looking to use it in my current project. However, I am having an issue.

I wrote a mini test program to test Mosquitto Broker (MQTT). So far, I am able to publish a topic and send its payload and receive the payload on the other end, but I can't seem to read or receive a payload for a topic into my test program. All I can do is SUBSCRIBE to a TOPIC. I thought MQTT broker communicates with publisher and subscribers back and forth or two ways.

Code: Pascal  [Select][+][-]
  1. ...
  2. uses MQTT;
  3. ...
  4. var
  5.     MQTTClient2:TMQTTClient;
  6. ...
  7.  
  8. procedure TForm1.BCButton3Click(Sender: TObject);
  9. begin
  10.   MQTTClient2.Publish('Emergency_Stop','1');
  11. end;
  12.  
  13. procedure TForm1.BCButton4Click(Sender: TObject);
  14. begin
  15.   MQTTClient2.OnPublish:=@myPublish;
  16.   MQTTClient2.Subscribe('Emergency_Stop');
  17. end;
  18.  
  19. procedure TForm1.FormCreate(Sender: TObject);
  20. begin
  21.     MQTTClient2 := TMQTTClient.Create('localhost',1883);
  22.     MQTTClient2.Connect;
  23. end;
  24.  
  25. procedure TForm1.Destroy;
  26. begin
  27.     MQTTClient2.Disconnect;
  28.     MQTTClient2.Free;
  29. end;
  30.  
  31. procedure TForm1.myPublish(sender:TObject; topic, payload:string);
  32. begin
  33.      Memo1.Lines.Add(topic+':'+payload);
  34. end;
  35.  

So, how do you setup your program to receive a topic and a payload from the Mosquitto Broker? I assume you need to have some sort of event for this. Any help will be greatly appreciated.
« Last Edit: January 26, 2018, 09:23:45 pm by Awesome Programmer »

jamie

  • Hero Member
  • *****
  • Posts: 3791
Re: Mosquitto MQTT and Lazarus: how do you receive payload?
« Reply #1 on: January 26, 2018, 11:06:39 pm »
I don't know that component but from judging from the names you are using I would say
there must be a "MQTTServer" object in there somewhere?
 
the client usually connects to a server and if you want to go the other way, you need a server on
your end..

The only true wisdom is knowing you know nothing

kupferstecher

  • Sr. Member
  • ****
  • Posts: 407
Re: Mosquitto MQTT and Lazarus: how do you receive payload?
« Reply #2 on: January 27, 2018, 01:17:39 am »
What library did you take? When I looked for MQTT a half year before, there were like three branches of a Lazarus-MQTT-Library (don't remember the name). And all had some different example code. The receival looked a bit complicated, one example used a statemachine to send frequent messages to tell the broker that the client is still there in case there was no recent data transfer. But I didn't follow it any further, so I can't give any example code. So the question also is what the library already covers and what has to be done in user code.

Edit: I was refering to the following code:
https://github.com/ZiCog/mqtt-free-pascal/blob/master/examples/embeddedApp/embeddedApp.pas
« Last Edit: January 27, 2018, 08:23:49 am by kupferstecher »

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 426
  • Programming is FUN only when it works :)
    • Cool Technology
Re: Mosquitto MQTT and Lazarus: how do you receive payload?
« Reply #3 on: February 01, 2018, 04:54:21 pm »
 :) :) :) :) :D :D :D :D :D
Yahoooooooooooooo!!!!!!!

I just figured out how to receive payloads from MQTT protocol. All the payloads is received within OnPublish event. Anytime any clients publish topics, it is received in your onpublish event. :)

AWESOME!!!!!!!

Thank you fellow programmers. :)

Mi-Ki

  • New Member
  • *
  • Posts: 30
Re: Mosquitto MQTT and Lazarus: how do you receive payload?
« Reply #4 on: October 26, 2020, 07:08:06 am »
This unit does not login?  %)
Username and Password to TMQTT

 

TinyPortal © 2005-2018