MQTT, All You Need to Get Started

- - posted in Technical | Tagged as IOT, MQ, MQTT, Messaging | Comments

So there are numerous post out there for MQTT implementation. I recently did a POC at work, and I wanted to keep a list of things I need to know if I wanted to return or do more with MQTT. Hence this post.

MQTT

MQTT is a machine-to-machine (M2M)/“Internet of Things” connectivity protocol. It was designed as an extremely

lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.

Demo Architecture

Cannot Display Architecture diagram, open this link https://dl.dropboxusercontent.com/u/118338831/MQTT_Arch.jpg

Installing software

Installing Node.JS


Please follow the instructions to install nodejs. After that, install some modules using the Node Package Manager:

Command for node modules
1
npm install -g mqtt url request

If you want to debug or auto-reload of the script, also install nodemon:

Command for nodemon
1
npm install -g nodemon




Installing MQTT Broker - Mosquitto (or Checkout HiveMQ)


Source: http://jpmens.net/2013/09/01/installing-mosquitto-on-a-raspberry-pi/

Commands for mosquitto
1
2
3
4
5
6
curl -O http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key     
sudo apt-key add mosquitto-repo.gpg.key   
rm mosquitto-repo.gpg.key 
cd /etc/apt/sources.list.d/   
sudo curl -O http://repo.mosquitto.org/debian/mosquitto-repo.list 
sudo apt-get update

Now go ahead and install Mosquitto proper. There are three packages:

-Mosquitto is the MQTT broker (i.e. server)
-mosquitto-clients are the command-line clients, which I recommend you install
-python-mosquitto are the Python bindings, which I also think you should install all three packages together require about 665Kb of space, which we can easily afford even on the tiny Pi.

Commands for mosquitto and client installations
1
sudo apt-get install mosquitto mosquitto-clients python-mosquitto

Mosquitto/’s default configuration means it is set up to not use username/password authentication and accepts all connections on port 1883. It also comes with two clients, mosquitto_pub and mosquitto_sub, the latter of which will be useful when you are debugging your applications. Running:

Command for subscribing
1
mosquitto_sub -t "#" -v

will dump all new messages to the broker. Remember the quotes around the topic, especially with the “#” wildcard on Unix as, unquoted or unescaped, that marks the start of a comment and would see the rest of the command discarded. If you leave that command running and, in another window, run

Command for publishing
1
'mosquitto_pub -t "mosquittodemo/test" -m "Hi"'

then you should see the mosquitto_sub session list the message.



Installing MQTT Client - MQTTWarn


This program subscribes to any number of MQTT topics (which may include wildcards) and publishes received payloads to one or more notification services, including support for notifying more than one distinct service for the same message.

For example, you may wish to notify via e-mail and to Pushover of an alarm published as text to the MQTT topic home/monitoring/+.

Full Info here

Requirements

You’ll need at least the following components:

-Python 2.x (tested with 2.6 and 2.7)
-An MQTT broker (e.g. Mosquitto)
-The Paho Python module: pip install paho-mqtt

Installation

-Clone this repository into a fresh directory.

Clone the repo
1
git clone https://github.com/jpmens/mqttwarn.git

-Copy mqttwarn.ini.sample to mqttwarn.ini and edit to your taste
-Install the prerequisite Python modules for the services you want to use
-Launch mqttwarn.py


MQTT Android Client


Android Service

The Paho Android Service is an interface to the Paho Java MQTT client library that provides a long running service for handling sending and receiving messages on behalf of Android client applications when the applications main Activity may not be running.

The Paho Android Service provides an asynchronous API

Source:

http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.java.git/

Check out Paho project here

All you need to get started is:

Code for creating client and publishing a message
1
2
3
4
5
6
client = new MqttClient("tcp://ipAddressofBroker:1883", "pahomqttpublish1");    
client.connect();    
MqttMessage message = new MqttMessage();     
message.setPayload("A single message".getBytes());    
client.publish("home/monitoring/+", message);    
client.disconnect();


Test Broker Setup


To test your Broker setup you can install MyMQTT from google play.

In few days I am gonna write myself a post about Google Cloud Notification service (GCM). The easiet notification service for Android notifications.


That’s all folks. Have Fun.