Lazarus

Programming => Networking and Web Programming => Topic started by: Mi-Ki on December 22, 2024, 09:02:10 am

Title: MQTT Client
Post by: Mi-Ki 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.
Title: Re: MQTT Client
Post by: cdbc on December 22, 2024, 09:35:09 am
Hi
Edited later: The entire MQTT-Framework being worked with in this thread is released on my GitLab repo, referenced HERE (https://forum.lazarus.freepascal.org/index.php/topic,70170.0.html).
With that little code, it's hard to tell...  ;D
You could also take a look here (https://github.com/bkeevil/mqtt) to see if there should be a solution to your conundrum...
Regards Benny
Title: Re: MQTT Client
Post by: Mi-Ki 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 (https://github.com/bkeevil/mqtt) 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.
Title: Re: MQTT Client
Post by: cdbc 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
Title: Re: MQTT Client
Post by: cdbc 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
Title: Re: MQTT Client
Post by: cdbc 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 (https://github.com/bkeevil/mqtt) 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. (https://github.com/Warfley/LazTermUtils)
Title: Re: MQTT Client
Post by: cdbc 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
Title: Re: MQTT Client
Post by: cdbc 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
Title: Re: MQTT Client
Post by: cdbc 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
Title: Re: MQTT Client
Post by: cdbc 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
Title: Re: MQTT Client
Post by: cdbc 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
Title: Re: MQTT Client
Post by: dbannon 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
Title: Re: MQTT Client
Post by: cdbc 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
Title: Re: MQTT Client
Post by: Thaddy 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.
Title: Re: MQTT Client
Post by: cdbc on January 15, 2025, 03:06:25 pm
Hi
Thanks Thaddy, I'm having fun  :D
Regards Benny
Title: Re: MQTT Client
Post by: cdbc on January 16, 2025, 06:08:38 pm
Hi
So all this MQTT-Circus started with me *not* doing 'LNet', for networking...
In order to come full circle, I /forced/ LNet upon myself and made a plugin for LNet as well  ...I was right, /I really don't like LNet, think it's weird in a roundabout sort of way/, but hey - now it's done and I don't have to touch it again, in a foreseeable future  :D
As always, tested to work against Mosquitto and my own broker...
I think now is the time for me to dive deeper into the MQTT-Abyss and have a 'LookSee'... 8-)
Let me know what you think and don't hesitate to ask questions.
Regards Benny
Title: Re: MQTT Client
Post by: cdbc on January 17, 2025, 08:54:07 am
Hi
@Thaddy: Your 'supercomputer' is rambling around in my head  :D
- What do you use it for?
- How do you partition a collective workload?
- I guess, mqtt is used for comms amongst them?
- All the other questions, you can think of, I probably have?!?
Curious as h*ll, is what I am... %)
Please spill...
Regards Benny
Title: Re: MQTT Client
Post by: Thaddy on January 17, 2025, 02:03:11 pm
Depends. currently it is running dummy loads to test some MPC algorithms at the protocol level. When that is stable I will try to implement some MPC tasks from the theory.
I started off with the examples from the Magpi from a few years ago. I built it just out of curiosity with no predefined plans for it. Maybe run some AI models locally.
Title: Re: MQTT Client
Post by: cdbc on January 17, 2025, 02:36:18 pm
Hi Thaddy
Cool  8-)
Quite an interesting little project...
Regards Benny
Title: Re: MQTT Client
Post by: Thaddy on January 17, 2025, 04:11:13 pm
BTW my mqtt code is based on a synapse version, but I am rebasing it to use fcl-net and fcl-web
Title: Re: MQTT Client
Post by: cdbc on January 17, 2025, 04:18:20 pm
Hi Thaddy
I used the /old/(er) fcl-net, because that's what's in 3.2.2, had a look at the newer 'fpsockets' by @Warfley, but I actually don't much like 'Generics'...
I don't think they should be *smeared* allover code, just because you can.
...And the generics libraries pull in a lot of code, that I mostly do not need.
I actually like fcl-net and Synapse the most  ;)
Just my 2 cents worth.
Regards Benny
Title: Re: MQTT Client
Post by: Thaddy on January 17, 2025, 06:10:18 pm
It is more that I prefer fpc-net and fcl-web since they are actively maintained.
And fcl-net has some of its roots in synapse anyway. But yes, I am mostly on trunk/main and that can differ considerably from 3.2.2/3.2 fixes.
Title: Re: MQTT Client
Post by: dbannon on January 18, 2025, 12:07:40 am
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)

In early RasPi times, I designed, on paper, a rasberrypi supercomputer as an April Fools Day joke for our newsletter. At the time, I ran the largest supercomputer south of the Tropic of Cancer (clever claim, most big SCs are well north to cheapen cooling).

My machine used tens of thousand RasPis mounted in a modified IBM tape robot, the idea being that reliability of the Pi's (OK in tens to hundreds, poor in the thousands) could be managed by automatically replacing any faulty hardware. It triggered a storm of suggestions about how we could capture a memory image before replacing a Pi so as to avoid disrupting a large MPI job. Some actually sounded (slightly) feasible.

Sadly, some people did not see the joke, triggering a series of complaints that VPAC was wasting money developing a 32bit ARM system  %)

Davo
Title: Re: MQTT Client
Post by: Thaddy on January 18, 2025, 06:41:06 am
Well, these clusters such as I made exist since the early Raspberry Pi one days, more than 10 years ago. So the April fool's joke did not land for many, because these clusters were not a joke at all but a genuine introduction in MPC (massive parallel compute). Several universities and scientific institutions built RPi clusters for educational purposes. (Cambridge, MIT, Delft to name a few). It is a nice toy to have. The nice thing is that because of mqtt it scales:if you have the odd $250 lying around, build the same contraption, and feel like it, we can add clusters on the go, independent of location. Note power supply is more expensive than the boards. My setup is relatively small, I know of some really silly numbers, at least 5 times mine.
Btw: it is not about the speed, it is about understanding MPC. You would not expect a toy train to reach the same speed as a TGV or Shenzen.
TinyPortal © 2005-2018