Tuesday 22 July 2014

Getting started with DIoTY's Cloud MQTT Broker...

In my first post I briefly explained the possibilities provided by DIoTY.co.  In this post, I'll help you getting started with the basics.  I will show you how to use the MQTT cloud broker with existing software you can freely download.  It's a quick and easy way to check you're correctly set up and to understand how MQTT works.

If you haven't done so yet, first go to DIoTY.co and sign in using a google email address.  You will receive your password to connect to the MQTT broker with your welcome email.  Note that this will take some time so do this before continuing reading this post...

When working with MQTT you will always have a client publishing messages to a topic and one or more clients receiving those messages by subscribing to that topic.

A sample publish and subscribe program comes with the free Mosquitto broker which you can download here.  It is available for most operating systems including Windows, Linux and Mac OS X and comes even with the full source if you which to compile it yourself.  I suggest you follow the installation instructions on the download page.  As you will connect to DIoTY's MQTT broker there is no need to start the mosquitto broker locally and all you are really interested in are the two programs mosquitto_pub and mosquitto_sub.

Assuming you have received your welcome email from DIoTY.co and you managed to download and install Mosquitto you're now ready to test.  For this open a couple of command line windows.  After installation, the mosquitto_pub and mosquitto_sub programs should be in your path but if not, it's probably a good idea to add them.

Publishing your first message:

You can now publish a message with the following command (just replace <your email address>, twice, with the email address you used to sign up and <your password> with the password you receive in your welcome email):

mosquitto_pub -h mqtt.dioty.co -p 1883 -u <your email address> -P <your password> -t /<your email address>/hello -m "Hello DIoTY"

DIoTY.co allows you only to publish in your own topic tree.  This means that the topic where you publish to must start with /<your email address>/.  Messages published outside of your topic tree will just be ignored.

Of course, publishing a message only makes sense when there is also at least one subscriber.

Subscribing to your topic tree:

You can subscribe to all topics in your topic tree with the following command (just replace <your email address> and <your password> like you did when publishing):

mosquitto_sub -h mqtt.dioty.co -p 1883 -u <your email address> -P <your password> -t /<your email address/#

     # is a wildcard and means that /any/subtree/will/match...
     mosquitto_sub keeps listening to the topics you subscribe to until you brake (ctr-c ; cmd-c; kill).

When you have followed this blog, the first thing you learn from this example is that messages published before the subscriber has been started will just be lost.  So whilst your subscriber is still subscribed, open another window and run again the mosquitto_pub command.  Now you will see "Hello DIoTY" appearing in your subscriber window.  Yes, it does work after all ;-)

I can hear people thinking already, when I connect with my mobile device to see the temperature at home, I don't want to wait till the temperature changes and the sensor publishes the new value.  I want to see immediately the last know temperature.  To obtain this, use so called retained publications.  Publish again the same messages as before, but this time add a "-r" to the mosquitto_pub command.  When you afterwards start a new subscriber client (either in a new window or by stopping and starting the one you have running already) you will see the retained message immediately.

Have a look at the documentation mosquitto_pub and mosquitto_sub for other options available.

Since our MQTT broker is in the cloud, you can also download a mobile application like mymqtt on your android device.  Just connect to mqtt.dioty.co, add your username, password and subscription topic and you not only see the published messages on your computer but also on your phone or tablet.

6 comments:

  1. Hi

    I am trying to send and read a test message using MyMQTT on Android KitKat.

    I can connect to the server OK.

    As far as I can tell I can send a message OK, but I do not get it back.

    Can someone Post step by step instructions as to what to put in each field to send a simple 'Hello' message on a 'test' topic.

    I am sure others would find it helpful.

    Thanks

    ReplyDelete
    Replies
    1. OK I have got it.

      You need to specify the topic fully i.e. :-

      /youraccount@gmail.com/requiredtopic eg /youraccount@gmail.com/hello

      Delete
  2. Cloud computing is here and has been embraced by many an organization data room services

    ReplyDelete
  3. How can I delete retained messages?

    ReplyDelete
    Replies
    1. Publish an empty retained message to the topic...

      Delete
  4. To fully understand these brokers, one would really have to consider knowing the different types of brokers available. There are just four types of brokers but other school of thought may think differently. Таможенные Услуги

    ReplyDelete