Recent

Author Topic: MQTT Client  (Read 10386 times)

Mi-Ki

  • Jr. Member
  • **
  • Posts: 76
MQTT Client
« on: December 22, 2024, 09:02:10 am »
Good morning.
I have Mosquitto MQTT Broker on my raspberry.
I have a switch that I control via the terminal and it works.

Code: Pascal  [Select][+][-]
  1. mosquitto_pub -t 'zigbee2mqtt/0xa4c138dfe4ce8c20/set' -m '{ "state_left": "TOGGLE" }'

How to do this via lazarus?
https://github.com/cutec-chris/TMQTTClient
This works but writes an error.
Code: Pascal  [Select][+][-]
  1. MQTTClient.Publish('zigbee2mqtt/0xa4c138dfe4ce8c20/set', '{ "state_left": "TOGGLE" }');  

Is this library good or used by another?
Thank you.
« Last Edit: December 22, 2024, 09:21:52 am by Mi-Ki »

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: MQTT Client
« Reply #1 on: December 22, 2024, 09:35:09 am »
Hi
With that little code, it's hard to tell...  ;D
You could also take a look here to see if there should be a solution to your conundrum...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Mi-Ki

  • Jr. Member
  • **
  • Posts: 76
Re: MQTT Client
« Reply #2 on: December 23, 2024, 08:45:47 am »
Hi
With that little code, it's hard to tell...  ;D
You could also take a look here to see if there should be a solution to your conundrum...
Regards Benny

Hi.
I have MQTT installed but I don't know how to do it.
Any sample, demo?
Thank you.
« Last Edit: December 23, 2024, 08:47:45 am by Mi-Ki »

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: MQTT Client
« Reply #3 on: December 23, 2024, 09:13:34 am »
Hi
There are 3 demo apps included in directory 'Apps', I thought there would be something you could use...?
I don't do 'LNet' but give me some time, then I can rewrite the demos to use 'Synapse' or 'Indy'...
I've just swiftly glanced over it, as I haven't needed it yet...
The 'MQTTClient' looks promising.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: MQTT Client
« Reply #4 on: December 27, 2024, 04:20:56 pm »
Hi
Ok, so I did some testing and ended up forking Bond Keevil's MQTT-framework, because as I tried to make it work with Synapse instead of LNet, I found his implementation lacking and he himself states, that he's not putting any more work into that.
Quote
https://github.com/cutec-chris/TMQTTClient
This works but writes an error.
Nope, tested against mosquitto and my own new synapse-mqtt-broker (finished last night), IT DOES NOT WORK!
...So hang on and I'll see, what can be done, /client-wise/.
bkeevil's code-base is good and I can build on that, maybe even support protocol 5 and introduce plugin-architecture, for the network-backend...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: MQTT Client
« Reply #5 on: December 27, 2024, 07:33:35 pm »
Hi

Here's the synapse dependent broker project attached:

Regarding the 4 digit numbers in use:
 - look at your phone's dial-keypad, almost every number has 3..4 letters
    asociated with it, e.g.: 3 = (def) & 7 = (pqrs), so you can spell a little...
The forked & changed 'mqttserver' unit is necessary for this to work... It won't work with bkeevil's version of the code!
Bond Keevil's code can be found HERE and it works nicely with 'LNet'.
My own fork, is private for the time being.
It has been tested against 'Eclipse Mosquitto's own apps, to work.
Regards Benny

edit:
Oooopsss: forgot to mention, it makes use of @Warfley's excellent terminal library found HERE.
« Last Edit: December 27, 2024, 07:39:14 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: MQTT Client
« Reply #6 on: December 28, 2024, 10:30:46 am »
Hi
Forgot this...
If you really need it, just rebase it on 'TQueue' from 'contnrs' unit...
...or buy the excellent book by Julian Bucknall: "Tomes of Delphi, Algorithms and Datastructures"  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: MQTT Client
« Reply #7 on: December 29, 2024, 07:18:38 pm »
Helllllloooooooo  ;D
Right, attached is a preliminary sneak preview of a first working /Synapse dependent/ MQTT-Client...  :-X
I've added the missing 'Queue'-piece of the puzzle, based on TQueue from 'contnrs'...
Plus I made the net-connection a plugin-interface... so this project needs the forked 'mqtt.client.pas'!!! -contained in the zip.
Well, anyway here it is, PRE-ALPHA
Tested against mosquitto's apps and my own broker to work
Have fun  :D
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: MQTT Client
« Reply #8 on: January 08, 2025, 07:49:44 pm »
Hi
So, I've been fiddling with the client, because it came out the door in a hurry.
I've fixed a lot of things and tried even more, I had hoped this was a good candidate for a 'lazy/sleeping thread' -backend, but alas, I can't seem to get the MQTT-framework to accept a repeat connection with the original values...
The lazy backend does work and makes use of my new 'ITSQueue' and 'InterlockedExchange' amo. You can follow it in the live log, how it goes to sleep when 'Paused' and back to life, but the MQTT won't play nice, till now at least  :-X
I've zipped a working app with common and forked files, a bit nicer ui and (I think) no mem-leaks  ;D
Again it's been tested against 'Mosquitto' and my own broker, to work.
If you want to play with the 'Lazy' /branch/ there's a compiler switch {-$define lazy} you need to enable by removing the hyphen in front
As always - Have Fun
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: MQTT Client
« Reply #9 on: January 12, 2025, 10:48:04 pm »
Hi
For those of you who are more *Indy* inclined, here's an Indy-Netlink-plugin for you, together with 2 updated views and the other 2 polished plugins for Synapse  ...oh and the api-interface has been updated slightly, but should compile and run with the other components I've uploaded  :D
Note: The Indy itself, you'll have to provide yourself...
In the hope it can be of use to someone  8-)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: MQTT Client
« Reply #10 on: January 15, 2025, 10:50:07 am »
Hi
Sorry, but I can't help myself... this is funny  :D
Right, so here is a piece of kit, that'll run with fpc >= 3.2.2 straight out of the box, NO external libs required...! (...other than mqtt & mine)
This plugin uses fpc's own 'fcl-net' technology... Nifty huh?  8-)
The zip contains an updated 'Main.View' too, 'cause these plugins are 'Mutually Exclusive'.
In the hope, it can be of use to someone  ;D
Have fun Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

dbannon

  • Hero Member
  • *****
  • Posts: 3212
    • tomboy-ng, a rewrite of the classic Tomboy
Re: MQTT Client
« Reply #11 on: January 15, 2025, 11:16:07 am »
OK, so I almost commented further up. See, I looked at using MQTT on a project a while ago, but was put off with all the layers necessary. When I saw you playing with Synapse, then Indy, I thought, "whats wrong with fcl-net, must be a reason " ?

Nicely done Benny, I'll have a play in the unlikely event I get some spare time !

Thanks, bet its good too.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: MQTT Client
« Reply #12 on: January 15, 2025, 12:39:01 pm »
Hi Davo
Thanks mate  :)
You know, when you go to the sandbox to play a little
...and suddenly your momma's calling 'Dinner is ready' - one gets submerged in having fun  :D
(That analogy is my wife's from last night).
I too found it too tightly coupled in the layers, so I tried to go the plugin trail, to see if I could decouple it a bit...
This architecture works from 'so/dll's too.
Please do, have a play and let me know what you think...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: MQTT Client
« Reply #13 on: January 15, 2025, 01:33:29 pm »
It is better than my own -unpublished but working- implementation.
I use mqtt for controlling a cluster of rpi's ( 16 x Raspberry Zero 2 W for 64 cores in total , super computing for less than 250 euro)
Nice work, Benny.
« Last Edit: January 15, 2025, 01:37:44 pm by Thaddy »
But I am sure they don't want the Trumps back...

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: MQTT Client
« Reply #14 on: January 15, 2025, 03:06:25 pm »
Hi
Thanks Thaddy, I'm having fun  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

 

TinyPortal © 2005-2018