11

I have recently released my MQTT Client for Delphi, which can now be found and downloaded here . TMQTTClient is a non-visual Delphi Client Library for the IBM Websphere MQ Transport Telemetry protocol. It allows you to connect to a Message Broker that uses MQTT such as the Really Small Message Broker (RSMB) which is freely available for evaluation on IBM Alphaworks. Alternatively for it’s larger brother you can try the IBM Lotus Expeditor product.

I felt that I should explain a little bit about what RSMB is and why you might want to use either RSMB or another broker that supports MQTT for a project. RSMB is a small and ultra-lightweight Message Broker, it takes only 50KB of Code and can run in 150KB of Memory or less (obviously you might find this varies slightly for you). A resource consumption level which makes it ideal for situations where it is required to run on low power servers (such as the Viglen MPC-L which runs on just 10W) or in resource constricted environments.

The MQTT Protocol itself is equally compact but no less powerful and is perfect for running on Low Bandwidth or unreliable connection environments where you may have many sensors connected via different means. MQTT is a Publish / Subscribe model where a client can subscribe to a particular “topic” (verbose example /home/kitchen/temperature) and will then receive messages from Publishers on that topic (for example 18 degrees celsius). Potentially very powerful as I’m sure you’ll agree, you can understand why the Pervasive messaging group at IBM are particularly excited about this technology.

RSMB also has the very useful feature of being able to bridge with other Message Brokers which allows messages to be passed on between brokers.

 

What do you actually use this for?

I use RSMB and MQTT as the messaging transport for my Home Monitoring and Automation Network as a result of the great talks I heard from the great IBM pervasive messaging team that I met when I attended Homecamp last November. In order to demonstrate it’s usage, it is probably easiest to explain a scenario which is implemented on my home network. 

Example: I currently have my CurrentCost unit hooked up to my home server which publishes it’s electricity usage and temperature information to an MQTT Topic to my RSMB Server. This data is then published to subsribers such as an “ambient orb” which, I’m currently building based on the Arduino platform, that glows a different colour depending on how the current usage compares to the normal average for this particular time of day (Glowing Red, for example, if I’m consuming a lot more than normal).

This is a very similar setup to the one that powers Andy Standford-Clark’s Twittering house and to the Ambient orb setup that Nick O’Leary describes (with photos) in his own blog post here. The Key point is that having the lightweight messaging protocol MQTT as the backbone makes adding and removing devices easy. (no matter what shape or size they may be)

Nick O’Leary has written a very handy MQTT Client for the Arduino platform which allows my ambient orb to get the latest consumption data from the CurrentCost unit without them having to talk directly to each other.  There are also MQTT Client reference  implementations for Java, C and 3rd party implementations for Ruby and an unofficial Perl library. 

Why not use a more widely compatible transport such as one based on XML?

It’s true that in the example provided above the transport used could be a Web Service or a simple RSS feed but what makes MQTT particularly useful is that it uses a Publish/Subsribe model which lends itself more to real time data than either of the above. MQTT is also considerably more lightweight which allows you to fit a client into a device like the Arduino without the On-the-wire and library XML Parsing overhead. 

Ok, how can I get started?

Downloading and Setting up RSMB is very easy, you’ll need an IBM.com ID to download but then setup simply involves extracting the zip file and running the appropriate executable (Windows and Linux binaries are included). To use TMQTTClient you simply need to download the component and extract the two units in TMQTTClient. You will also need to make sure you have the excellent Synapse Communications components available on your search path somewhere.

After that you could publish messages to your broker telling you that you had a new email from your Delphi code in this sort of manner:

uses MQTT;

var
	MQTTClient: TMQTTClient;
begin
	MQTTClient := TMQTTClient.Create('localhost', 1883);
	MQTTClient.Connect;
	MQTTClient.Publish('/messaging/email/john', '1 New Message');
	MQTTClient.Disconnect;
	MQTTClient.Free;
end;

Subscribing to a topic is just as easy and you can create an event handler to handle incoming Publish messages on any particular topic:

uses MQTT;
var
	MQTTClient: TMQTTClient;

procedure TmyForm.btnConnectClick(Sender: TObject);
begin
	MQTTClient := TMQTTClient.Create(eIP.Text, StrToInt(ePort.Text));
	MQTTClient.OnPublish := OnPublish;
	MQTTClient.Connect;
        MQTTClient.Subscribe('/messaging/email/john');
	MQTTClient.Publish('/messaging/email/john', '1 New Message');
end;

procedure TmyForm.OnPublish(Sender: TObject; topic, payload: string);
begin
	memoStatus.Lines.Add('Publish Received. Topic: '+ topic + ' Payload: ' + payload);
end;

Having written this, I can now focus on building out the rest of my home monitoring network (details to come another time) which will be super easy (I hope!) now that it is based on a MQTT/RSMB system.

Tags: , , ,

11 Comments

  1. RaiulBaztepo on the 28th March 2009 remarked #

    Hello!
    Very Interesting post! Thank you for such interesting resource!
    PS: Sorry for my bad english, I’v just started to learn this language 😉
    See you!
    Your, Raiul Baztepo

  2. jamie on the 29th March 2009 remarked #

    Raiul,
    I’m glad that you enjoyed the post and found it interesting. Your English seems to be good so don’t let that stop you from commenting at any time in the future! Hope you were able to find something useful from it.

  3. Mark tsai on the 1st April 2014 remarked #

    Jamie,
    Do you have deploy to android or iOS deivce? I can’t receive message on memoStatus.
    can you tell me show to fix it if you have encountered same problem.
    thanks.
    Mark

  4. jamiei on the 3rd April 2014 remarked #

    Hi Mark,

    I’m afraid that this was written long before the iOS compatible versions of Delphi. You might need to re-write the sample for FireMonkey.

    Thanks

    Jamie

  5. pelican on the 1st November 2014 remarked #

    Hi,

    I couldn’t find the source anywhere, the links are dead. Could you share the source please?

    Best regards,

    pel

  6. jamiei on the 29th December 2014 remarked #

    @pelican: Yes, the link on this page should now work: http://jamiei.com/blog/code/mqtt-client-library-for-delphi/

    I still need to post version 2 at some point.

  7. Mohamed on the 7th May 2015 remarked #

    Hi

    I wanted to try this QMTT protocol on my Embarcadero XE8 and I couldn’t succeed.
    The error message in my environment Windows 7 32 bit is:

    [dcc32 Fatal Error] MQTT.pas(36): F2613 Unit ‘blcksock’ not found.

    Any advice on how to get further?

    Many thanks.

  8. Mohamed on the 7th May 2015 remarked #

    Sorry but it still doesn’t compile. The error message is:

    [dcc32 Fatal Error] MQTT.pas(36): F2613 Unit ‘blcksock’ not found.

  9. jamiei on the 7th May 2015 remarked #

    @Mohamed: Hi, Did you see that you need to have the excellent Synapse library on your project path (http://synapse.ararat.cz/doku.php).

  10. Mohamed on the 7th May 2015 remarked #

    @Jamiei: Hi. Indeed, I hadn’t this synapse library installed on my machine. I did install it and still no success in compiling MQTT example. The new error messages say:
    [dcc32 Error] synautil.pas(2062): E2003 Undeclared identifier: ‘ShortMonthNames’
    [dcc32 Fatal Error] blcksock.pas(106): F2063 Could not compile used unit ‘synautil.pas’
    This looks like an undeclared identifier called ShortMonthNames.
    Any advice to me?
    Many thanks in advance.

  11. jamiei on the 11th May 2015 remarked #

    @Mohamed: Are you using the latest version of the Synapse library? Might be worth checking that it is compatible with your version of Delphi?

Leave a Comment