MQTT with Mosquitto broker

MQTT (MQ Telemetry Transport) is an ISO standard (ISO/IEC PRF 20922) publish-subscribe-based messaging protocol for use on top of the TCP/IP protocol.

mqtt


Topics

All messages in MQTT are organized into a tree structure.
This is an example :

room/kitchen/sensor/temperature/1
room/bedroom/sensor/door/3

In MQTT you can use wildcards on topics.
There is two wildcards :
“+” : You can use it many times to say that you select all at this level of the tree
“#” : You can use it just one time to say that you want all that is below in the tree

For example, here you want to subscribe or to publish to all sensors in all rooms.

room/+/sensor/#

Broker

The broker is a TCP server where publishers and subscribers have to connect to.
It manage connections, authentifications and distribution of messages.
The broker we will use in this tutorial is Mosquitto an Eclipse project.


Publisher

A publisher have to connect to the broker and then it can publish some messages on topics.


Subscriber

A subscriber have to connect to the broker and then to subscribe to a topic or to a group of topics
When a message is published onto theses topics the subscriber will received the message as soon as possible.


Mosquitto server

Let’go for an installation of a mosquitto broker under Debian :

root@debian$ apt-get update
root@debian$ apt-get install mosquitto mosquitto-clients
root@debian$ systemctl start mosquitto

Now you have your mosquitto broker up !

root@debian$ systemctl status mosquitto
‚óŹ mosquitto.service - LSB: mosquitto MQTT v3.1 message broker
Loaded: loaded (/etc/init.d/mosquitto)
Active: active (running) since jeu. 2016-12-01 09:17:05 CET; 2h 5min ago

Let’s start with a small test :

In one terminal start a subscriber :

root@debian$ mosquitto_sub -v -t "#"

In another terminal publish a message :

root@debian$ mosquitto_pub -t "test/" -m "hello world!"

And on your subscriber you must have :

root@debian$ mosquitto_sub -v -t "#"
test/ Hello world




Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Leave a Reply