Powering your next IoT application with MQTT - JavaOne 2014 tutorial
-
Upload
benjamin-cabe -
Category
Technology
-
view
865 -
download
2
description
Transcript of Powering your next IoT application with MQTT - JavaOne 2014 tutorial
Powering your next IoT application with
MQTT
Julien VermillardBenjamin Cabé
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
Your devoted presenters :-)
Benjamin Cabé / @kartben
● IoT Evangelist at the Eclipse Foundation● Hack things – Arduino, Raspberry Pi, Glass, …● Traveler and photographer
Agenda
Internet of Things 101What protocol should I use?MQTT
What is MQTT?Features overviewMQTT at Eclipse
HANDS-ON!More MQTT cool stuff
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
Tutorial steps
Step 0The greenhouse setup
Step 1Write your first MQTT client
Step 2Create an MQTT bot to consolidate live data
Step 3Live JavaFX dashboard
Photo by Clover_1
What you will need
Eclipse IDEBasic Java knowledgeYour brainzzz
Machine-to-Machine?
Machine-to-Machine?Internet of Things?
Technology that supports
wired or wireless
communication
between devices
❝
IoT protocols
… devices are constrained… network is constrained
… applications are heterogeneous
Different needs
Device ManagementRadio statististics, device configuration, …OMA-DM, TR-069, Lightweight M2M…
Local sensor networksTransmit sensor data, usually over RF or PLCZigbee, X10, Bluetooth Smart, …
End-user applicationsDisplay sensor data on mobile app, dashboards, …HTTP, Websockets, ...
Find your way through the IoT protocols jungle
Continental 1/2/3, Thursday @11.00am
MQTT?
MQTT?
M is for Messaging… (mmmmaybe!) Q is not for Queue
Publish/Subscribe protocolLightweight (bandwidth, battery, …)
PUB
ee232/emp SUB
ee232/
ee232/
emp
21.3
21.3
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
Neat MQTT features
WildcardsQuality of ServiceLast Will & TestamentRetained Messages
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
Examplesmygreenhouse/sensors/#+/sensors/temperature
MQTT Features | QoS
QoS flag allows to control the level of assurance for delivery you want when publishing a message to the broker
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
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
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
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
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
MQTT at Eclipse
Moquette
Eclipse Paho
Open-source implementations of MQTT clientsPick your language!
Java, JavaScript, C/C++, Go, Obj C, Lua, Python …
http://eclipse.org/pahohttps://dev.eclipse.org/mailman/listinfo/paho-dev
Eclipse Mosquitto
Lightweight server implementation of MQTT (MQTT-SN to come), written in CLightweight?
Executable is ~120kB~3MB RAM with 1000 clients connected
http://eclipse.org/mosquittohttps://dev.eclipse.org/mailman/listinfo/mosquitto-dev
Eclipse Moquette
A pure Java broker
Supports QoS 0, 1, 2
Websockets
Event-driven: based on Netty and LMAX disruptor
Hands-on!
Step #0iot.eclipse.orgMQTT broker
YOU :-)
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
Creating a Java Internet of Things Gateway
Continental 1/2/3, Tuesday @2.30pm
End-to-End IoT Solutions with Java and Eclipse IoT
Continental 1/2/3, Today @11.00pm
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
Tutorial source code
Check out:https://github.com/kartben/mqtt-tutorial-javaone2014
Getting started
● Launch Eclipse● Import projects contained on the USB stick
○ File > Import… > Existing projects into workspace
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
Photo Credit:oskay
MQTT Topics best practices
Photo Credit:oskay
That’s a tough question… are there best practices as to how to organize your local filesystem…?
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
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
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 brokerIts main use is keeping subscriptions intact for unreliable endpoints (see the Clean session flag)
Retrieving data
Complete step1 project:
1. Connect to iot.eclipse.org2. Subscribe to:
javaonedemo/eclipse-greenhouse/sensors/#
3. Observe the data coming in4. (optional) Publish a message (‘on’ or ‘o ’) to:
javaonedemo/eclipse-greenhouse/actuators/light
- Think about what QoS is best suited for controlling the LED- To persist or not to persist?
Questions?
Step #2
Leverage retained messages to build a bot that consolidates (min, max, avg) sensor data
Key concepts:● Asynchronous client● Retained messages● Data consolidation
Data consolidation workflow
Data consolidation workflow
“PUBLISH”javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.2
Data consolidation workflow
“PUBLISH”javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.2
javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.2javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.1javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.26javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.0javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.9
Data consolidation workflow
javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.2javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.1javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.26javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.0javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.9
“PUBLISH”greenhouse/LIVE/benjamin-bbb/data/temperaturePayload: 23.2
Local consolidation
Data consolidation workflow
javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.2javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.1javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.26javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.0javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.9
“PUBLISH”greenhouse/LIVE/benjamin-bbb/data/temperaturePayload: 23.2
Local consolidation
“PUBLISH RETAIN”javaonedemo/CONSOLIDATED/…eclipse-greenhouse/sensors/…2014/09/28/09/43Payload: 23.312
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
Questions?
Step #3
Use JavaFX Charts to display live sensor data
JavaFX Charts
Questions?
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/
Other cool MQTT uses
MQTT on embedded devices● Arduino, ● mbed, ● etc.
https://www.eclipse.org/paho/clients/c/embeddedMore info on Ian Cragg’s blog.
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
Thanks!
More questions? Feel free to contact us!
Benjamin Cabé@kartben
Julien Vermillard@vrmvrm