+ All Categories
Home > Technology > Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Date post: 29-Nov-2014
Category:
Upload: benjamin-cabe
View: 865 times
Download: 2 times
Share this document with a friend
Description:
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.
62
Powering your next IoT application with MQTT Julien Vermillard Benjamin Cabé
Transcript
Page 1: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Powering your next IoT application with

MQTT

Julien VermillardBenjamin Cabé

Page 2: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 3: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Your devoted presenters :-)

Benjamin Cabé / @kartben

● IoT Evangelist at the Eclipse Foundation● Hack things – Arduino, Raspberry Pi, Glass, …● Traveler and photographer

Page 4: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Agenda

Internet of Things 101What protocol should I use?MQTT

What is MQTT?Features overviewMQTT at Eclipse

HANDS-ON!More MQTT cool stuff

Page 5: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 6: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 7: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

What you will need

Eclipse IDEBasic Java knowledgeYour brainzzz

Page 8: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Machine-to-Machine?

Page 9: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Machine-to-Machine?Internet of Things?

Page 10: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Technology that supports

wired or wireless

communication

between devices

Page 11: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

IoT protocols

… devices are constrained… network is constrained

… applications are heterogeneous

Page 12: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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, ...

Page 13: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Find your way through the IoT protocols jungle

Continental 1/2/3, Thursday @11.00am

Page 14: Powering your next IoT application with MQTT - JavaOne 2014 tutorial
Page 15: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

MQTT?

Page 16: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

MQTT?

M is for Messaging… (mmmmaybe!) Q is not for Queue

Publish/Subscribe protocolLightweight (bandwidth, battery, …)

Page 17: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

PUB

ee232/emp SUB

ee232/

ee232/

emp

21.3

21.3

Page 18: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 19: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Neat MQTT features

WildcardsQuality of ServiceLast Will & TestamentRetained Messages

Page 20: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 21: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

MQTT Features | QoS

QoS flag allows to control the level of assurance for delivery you want when publishing a message to the broker

Page 22: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 23: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 24: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 25: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 26: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 27: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

MQTT at Eclipse

Moquette

Page 28: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 29: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 30: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Eclipse Moquette

A pure Java broker

Supports QoS 0, 1, 2

Websockets

Event-driven: based on Netty and LMAX disruptor

Page 31: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Hands-on!

Page 32: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Step #0iot.eclipse.orgMQTT broker

YOU :-)

Page 33: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 34: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Creating a Java Internet of Things Gateway

Continental 1/2/3, Tuesday @2.30pm

Page 35: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

End-to-End IoT Solutions with Java and Eclipse IoT

Continental 1/2/3, Today @11.00pm

Page 36: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 37: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Tutorial source code

Check out:https://github.com/kartben/mqtt-tutorial-javaone2014

Page 38: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Getting started

● Launch Eclipse● Import projects contained on the USB stick

○ File > Import… > Existing projects into workspace

Page 39: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 41: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

MQTT Topics best practices

Photo Credit:oskay

Page 42: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

MQTT Topics best practices

Photo Credit:oskay

That’s a tough question… are there best practices as to how to organize your local filesystem…?

Page 43: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 44: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 45: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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)

Page 46: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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?

Page 47: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Questions?

Page 48: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Step #2

Leverage retained messages to build a bot that consolidates (min, max, avg) sensor data

Key concepts:● Asynchronous client● Retained messages● Data consolidation

Page 49: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Data consolidation workflow

Page 50: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Data consolidation workflow

“PUBLISH”javaonedemo/eclipse-greenhouse/sensors/temperaturePayload: 23.2

Page 51: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 52: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 53: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 54: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 55: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Questions?

Page 56: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Step #3

Use JavaFX Charts to display live sensor data

Page 57: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

JavaFX Charts

Page 58: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Questions?

Page 59: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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/

Page 60: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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.

Page 61: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

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

Page 62: Powering your next IoT application with MQTT - JavaOne 2014 tutorial

Thanks!

More questions? Feel free to contact us!

Benjamin Cabé@kartben

[email protected]

Julien Vermillard@vrmvrm

[email protected]


Recommended