Friday, 11 September 2015

MQTT with Mosquitto and Paho: A beginner's tutorial



Using MQTT on Mosquitto and Paho

In the current tutorial, I shall describe the usage of one of the most widely used protocols named “MQTT” using open source server/broker and client applications. I have used my Windows 7 machine for the for the current tutorial.



MQTT

MQTT( Message Queue Telemetry Transport) is used for device data collection. As its name suggests, its main purpose is telemetry, or remote monitoring. Its goal is to collect data from many devices and transport that data to the IT infrastructure. It targets large networks of small devices that need to be monitored or controlled from the cloud. MQTT uses a hub-and-spoke architecture, where all the devices connect to a data concentrator server. The protocol works on top of TCP, which provides a simple, reliable stream of data. MQTT works in “publish subscribe” mode and is a very good means of low power transfer of data from devices to broker with reliability. The messages' payloads are a sequence of bytes, up to 256MB, with a fixed header. The clients can subscribe to these messages and get updated by the broker when new messages arrive. MQTT lets clients and brokers set a "Quality of Service" on messages basis from "fire and forget" to "confirmed delivery". We shall not test the "Quality of Service" in the first tutorial to keep things simple.

Please refer to <http://mqtt.org/faq> for more information on the specific protocol.

The following diagram shows the configuration that we would like to test out in the current tutorial.

Paho

MQTT needs client implementations. The Eclipse Paho project is part of the Eclipse Foundation's M2M (Machine to Machine) mission to provide high quality implementations of M2M libraries and tools. Under the Paho banner, open source client libraries for MQTT are being developed.  MQTT C and Java libraries with Lua, Python, C++ and JavaScript are at various stages of development. In this article we'll be showing how to use the Paho Java MQTT libraries to publish and subscribe.

Mosquitto

In order to receive published messages and send them on to any clients who have subscribed, we need a broker. Mosquitto is one of such brokers which is easy to configure and run for MQTT messages. Mosquitto is open source, so you can download it and run it on your own system, on different operating systems like Windows, Mac OS X, Linux or many other platforms. The Mosquitto broker code is also being contributed to Eclipse as part of a new project.

Mosquitto's default configuration does not use username/password authentication and it accepts all connections on port 1883. It also comes with two clients, mosquitto_pub and mosquitto_sub. mosquitto_pub client is used for publishing simple messages, while the later  is for subscribing to a topic and printing the message that it received. It is also widely used for debugging applications. We shall see the usage of both these clients in the current tutorial.

Downloads required

You need to download the following.

1] Eclipse IDE for Java developers: Source <https://eclipse.org/downloads/>, select the 32 bit or 64 bit version as compatible to your system.

Install eclipse as per the guidelines. Make sure a version of JRE is installed in your machine and eclipse should point to the same JRE folder for execution


2] Mosquitto: Source <http://mosquitto.org/download/> . I have used the binary installation as follows.

mosquitto-1.4.3-install-win32.exe (~200 kB) (Native build, Windows Vista and up)

At the time of installation, you will be prompted to install additional dlls (for win 32) as follows

libeay32.dll, ssleay32.dll  and  pthreadVC2.dll

You need to get the dlls from the URLs specified during download process (on clicking the links, you will be taken to the sources of the dlls directly). You need to copy the dlls is the directory where mosquito is installed. These dlls are mandatory for running mosquito.

Add the path of the folder where Mosquitto executables are copied using the System Properties-> Environment variables (on Windows 7). This will make sure that mosquito executable will run from any place. 


3] Paho : Source : <http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.java.git/

Download the following and extract in a folder (can be under eclipse). I used the “zip” version.


After extraction, add the path of the downloaded files in your CLASSPATH through System Properties-> Environment variables (on Windows 7). Specifically, the following folder should be added to the CLASSPATH for the current exercise.

java-1.0.2\org.eclipse.paho.client.mqttv3


Running the application

On the Eclipse IDE

Open the Eclipse IDE, create a new Java project and then select Configure Convert to Maven project.

Since the Paho MQTT code isn't in Maven Central, we need to include its repository. This needs to be done on the pom.xml file from Eclipse IDE.

Open the pom.xml file and after </version> add

<repositories>
<repository>
<id>paho-mqtt-client</id>
<name>Paho MQTT Client</name>
<url>https://repo.eclipse.org/content/repositories/paho-releases/</url>
</repository>
</repositories>

Then we need to add the dependency for the Mqtt-client code. On the pom.xml file, after </build>, add

<dependencies>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>mqtt-client</artifactId>
<packaging>jar</packaging>
<version>0.4.0</version>
</dependency>
</dependencies>

From Command Prompt

Run Mosquitto.exe (type “mosquitto” from command prompt). One screen will open up



To make sure (or check) whether mosquitto  is running, you can open another command prompt window and run “netstat –an”.  You should be able to see the following screen with boxed line (showing Listening at port 1883).


 Now we have made sure that MQTT broker is running and is listening to port 1883.
Open another command prompt and run the following command. With the options used with mosquitto_sub,  the messages are dumped on the screen.

mosquitto_sub -t "#" –v


Go back to Eclipse IDE and create a new class called PahoDemo. Add the following lines of code.

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class PahoDemo {

         MqttClient client;
        
         public PahoDemo() {}

         public static void main(String[] args) {
           new PahoDemo().doDemo();
         }

         public void doDemo() {
           try {
             client = new MqttClient("tcp://localhost:1883", "pahomqttpublish1");
             client.connect();
             MqttMessage message = new MqttMessage();
             message.setPayload("A single message from first program".getBytes());
             client.publish("pahodemo/test", message);
             client.disconnect();
           } catch (MqttException e) {
             e.printStackTrace();
           }
         }
       }


Output:


You will be able to see the message “pahodemo/test A single message from first program” on the mosquitto_sub screen.



Also, if you open another command prompt and type the following:

mosquitto_pub -t "mosquittodemo/test" -m "Hello"

You will get the same message echoed at the mosquitto_sub screen as follows.


Thus, we managed to get a basic Paho MQTT publish client running.