When it comes to connecting physical objects from daily life to the internet, you’re faced with several challenges. MQTT is a protocol for the Internet of Things that addresses the aforementioned challenges and makes it possible to build scalable sensor networks. This tutorial aims to give you a hands-on experience with the MQTT protocol and walk you through the creation of an end-to-end M2M/Internet of Things application, using open source Java components such as Eclipse Paho, Mosquitto, and Kura. You will leave the session knowing all the cool features of MQTT and how you can integrate it into your Java solutions.
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Powering your next IoT application with MQTT
1. Powering your next
IoT application with
MQTT
Julien Vermillard
Benjamin Cabé
2. Your devoted presenters :-)
Julien Vermillard / @vrmvrm
● Software Engineer at Sierra Wireless
● Implementing various protocols for our cloud
service (AirVantage)
● Apache Software Foundation member
● Eclipse committer on Californium,
Wakaama and Leshan
3. Your devoted presenters :-)
Benjamin Cabé / @kartben
● IoT Evangelist at the Eclipse Foundation
● Hack things – Arduino, Raspberry Pi, Glass, …
● Traveler and photographer
4. Agenda
Internet of Things 101
What protocol should I use?
MQTT
What is MQTT?
Features overview
MQTT at Eclipse
HANDS-ON!
More MQTT cool stuff
5. Tutorial goal
Go from our first dead simple MQTT client app to
a complete end-to-end solution for …
reporting,
consolidating and
charting sensor data
… coming from a connected greenhouse
6. Tutorial steps
Step 0
The greenhouse setup
Step 1
Write your first MQTT client
Step 2
Create an MQTT bot to
consolidate live data
Step 3
Live JavaFX dashboard
Photo by Clover_1
7. What you will need
Eclipse IDE
Basic Java knowledge
Your brainzzz
11. IoT protocols
… devices are constrained
… network is constrained
… applications are heterogeneous
12. Different needs
Device Management
Radio statististics, device configuration, …
OMA-DM, TR-069, Lightweight M2M…
Local sensor networks
Transmit sensor data, usually over RF or PLC
Zigbee, X10, Bluetooth Smart, …
End-user applications
Display sensor data on mobile app, dashboards, …
HTTP, Websockets, ...
13. Find your way through the
IoT protocols jungle
Continental 1/2/3, Thursday @11.00am
18. MQTT history
… it’s not new!
Invented in 1999 (Andy Stanford-Clark, Arlen Nipper)
Royalty-free since 2010
Being standardized at OASIS - final spec to be
released soon
19. Neat MQTT features
Wildcards
Quality of Service
Last Will & Testament
Retained Messages
20. MQTT Features | Wildcards
● The number sign (#) is a wildcard character that
matches any number of levels within a topic.
● The plus sign (+) is a wildcard character that
matches only one topic level
Examples
mygreenhouse/sensors/#
+/sensors/temperature
21. MQTT Features | QoS
QoS flag allows to control the level of assurance
for delivery you want when publishing a message
to the broker
22. MQTT Features | QoS 0
QoS flag allows to control the level of assurance
for delivery you want when publishing a message
to the broker
A message published with QoS=0 will be received
at most once (“fire & forget”) by subscribed clients
23. MQTT Features | QoS 1
QoS flag allows to control the level of assurance
for delivery you want when publishing a message
to the broker
A message published with QoS=1 will be received
at least once (acknowledged delivery) by
subscribed clients
24. MQTT Features | QoS 2
QoS flag allows to control the level of assurance
for delivery you want when publishing a message
to the broker
A message published with QoS=2 will be received
exactly once (assured delivery) by subscribed
clients
25. MQTT Features | Last Will & Testament
IoT devices can come & go on the network quite
often and in a very unpredictable way
Last Will & Testament allows to notify interested
parties to an abnormal disconnection of a client
The Last Will & Testament (if any) is part of the
initial connection message
26. MQTT Features | Retained messages
The Retained flag allows a published message to
be stored on the broker, so as possible receivers
can subscribe later and still receive the message
29. Eclipse Mosquitto
Lightweight server implementation of MQTT
(MQTT-SN to come), written in C
Lightweight?
Executable is ~120kB
~3MB RAM with 1000 clients connected
http://eclipse.org/mosquitto
https://dev.eclipse.org/mailman/listinfo/mosquitto-dev
30. Eclipse Moquette
A pure Java broker
Supports QoS 0, 1, 2
Websockets
Event-driven: based on Netty and LMAX disruptor
33. Step #0
The Kura application can be seen as an MQTT client
that:
● publishes sensor data to topics
● is subscribed to commands
Root topic:
javaonedemo/eclipse-greenhouse/
Sensor data on sensors/... subtopic,
Actuators on actuators/... subtopic
34. Creating a Java Internet of Things
Gateway
Continental 1/2/3, Tuesday @2.30pm
36. Contents of the USB stick
● Eclipse IDE for Windows, Linux and OS X
● Sample projects to be imported in your workspace
+ Paho java client JAR file
● Completed projects
38. Getting started
● Launch Eclipse
● Import projects contained on the USB stick
○ File > Import… > Existing projects into workspace
39. Step #1
Use the Java MQTT client from Paho to exchange
our first messages with a broker
Key concepts:
● Connect/disconnect to a broker
● Topic structuring best practices
● Publish/subscribe messages
● QoS
42. MQTT Topics best practices
That’s a tough question… are there best practices
as to how to organize your local filesystem…?
Photo Credit:oskay
43. MQTT Topics best practices
Photo Credit:oskay
● Topic hierarchy is what will allow you to do
smart wildcard matching
○ objectID/temp is better than temp/objectID
● No need to start with a ‘/’
● Persist your topics if you can afford it
Interesting thoughts in this session from IoT Day Grenoble https://www.youtube.
com/watch?v=yQAsjGFewk8
44. MQTT for Java API
MqttClient
● Main class allowing to connect to a broker
MqttMessage
● What you send to or receive from a broker
● Arbitrary binary payload (ie byte array)
MqttCallback
● For a given MqttClient, the MqttCallback is
where you handle received messages, delivery ACK,
and loss of connection
45. Connecting to a broker
MqttClient#connect(serverURI, clientId)
URI can be something like:
● tcp://iot.eclipse.org:1883
● ssl://my.secured.broker:8883
The client ID must be unique on a given broker
Its main use is keeping subscriptions intact for
unreliable endpoints (see the Clean session flag)
46. Retrieving data
Complete step1 project:
1. Connect to iot.eclipse.org
2. Subscribe to:
javaonedemo/eclipse-greenhouse/sensors/#
3. Observe the data coming in
4. (optional) Publish a message (‘on’ or ‘oDzDz’) to:
javaonedemo/eclipse-greenhouse/actuators/light
- Think about what QoS is best suited for controlling the LED
- To persist or not to persist?
54. MqttAsyncClient
Same as MqttClient but allowing to perform
operations asynchronously!
● i.e. it will allow us to asynchronously publish a message
from within the messageArrived callback
● Methods are not very different from MqttClient except that
they optionally accept an
IMQTTActionListenerCallback (called when
operation has finished),
and return an IMqttToken to be used for tracking the
completion of a task
59. Other cool MQTT uses
MQTT over WebSockets
● Build a web UI with no backend
● Broker support in Mosquitto,
Moquette, …
● Many JS-based time-series
graph engines: D3.js, Rickshaw, …
https://www.eclipse.org/paho/clients/js/
60. Other cool MQTT uses
MQTT on embedded devices
● Arduino,
● mbed,
● etc.
https://www.eclipse.org/paho/clients/c/embedded
More info on Ian Cragg’s blog.
61. Other cool MQTT uses
MQTT on Google Glass
● You just need a regular
Java Paho client!
● “OK Glass, control the roof!”
● You might want to check out the Android
service available in Paho’s Java ‘develop’ branch
62. Thanks!
More questions? Feel free to contact us!
Benjamin Cabé
@kartben
benjamin.cabe@eclipse.org
Julien Vermillard
@vrmvrm
jvermillard@sierrawireless.com