My journey into the Cloud (4); First test using MQTT

February 1, 2016 | 01:03
My first MQTT Client
My first MQTT Client
In the last installment I set about describing the MQTT messaging protocol. It has many interesting features and is well suited to a system which handles messages between multiple message producers (sensors etc.) and multiple message consumers (PCs, smartphones etc.). Best of all, the protocol is fully open source and has good support from the community.

For the developer there are two official web sites: Firstly, where, amongst other things, you can also download the protocol spec. There is also the Eclipse Paho projects website at This site is maintained by the Eclipse IOT Working Group whose members include companies such as Cisco, Huawei and Bosch. Here you can find versions of MQTT implemented in many different programming languages.

It certainly aroused my interest – I was keen to try it out for myself. As luck would have it there are some working MQTT brokers already implemented for testing in ‘The Cloud’. I chose to use the Mosquitto test server although other alternatives are also available. There is the usual warning in the text to be careful and don’t inadvertently publish anything sensitive; anybody could be reading it!

It didn’t really matter in our case, we are just intending to pass a few messages out from our (self programmed) Client and then to receive them again. To start off we will simply use a PC as the platform. There are open-source libraries for Java, Javascript, Python and others on the Paho site. I chose the M2Mqtt library for C#/.NET. You can find comprehensive and helpful documentation for newbies here:

Links from the official MQTT site often end up pointing out into the void so here is the direct web link to the source code at Github:

The download is a Visual Studio Solution (Project folder) which should be opened in the (community edition) free Visual Studio 2015 IDE. First you will need to add the Solution from another project which takes the user interface and the application code of the self-programmed Client. Then this new Project is given a reference in the M2Mqtt-Library to allow the functions to be accessed in the code using

using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

Following the guide, programming using the library proved to be really simple. You can download the resulting complete Solution below. I have also stored the executable application into a folder (here you need the .NET Framework 4.5 software framework, which is often already installed in Windows OS but can also be downloaded for free).

My test client is able to receive its own messages when the same subject title is entered into the Topic text boxes und then by choosing the ‘Subscribe’ option. Once a text entry is made in the field ‘Text to publish’ and the publish button is clicked, the message is sent out to the net and after a short delay appears in the ‘Received text’ box. With the same client opened on different PC platforms you can pass messages back and forth. There was no problem for me to send messages from our publishing office out to my office at home and also to chat with a colleague (here it’s convenient to use your forename as ‘Topic to publish’ and the recipient’s forename in ‘Topic to subscribe’).

So a Broker in the network certainly simplifies things compared with the more traditional solution using your own web server with a fixed IP address and all the necessary router configuration setup and associated stress involved.
There is nothing quite like the sweet smell of success to spur you on to the next challenge – more on that in the next installment!
Loading comments...
related items