Transcript
Page 1: MQTT with Java - a protocol for IoT and M2M communication

MQTT with Java - a protocol for IoT and M2M communication

!Christian Götz, dc-square

Page 2: MQTT with Java - a protocol for IoT and M2M communication

IoT?

Page 3: MQTT with Java - a protocol for IoT and M2M communication

M2MIoT

IoEWoT

Web of Things

Internet of Things

Internet of Everything

Machine to Machine communication

UbicompUbiquitous computing

CPSCyper Physical Systems Pervasive

Computing

Page 4: MQTT with Java - a protocol for IoT and M2M communication

Technology that connects Devices

over wired or wireless communication

Page 5: MQTT with Java - a protocol for IoT and M2M communication

GPSGPS

GPSGPS

GPS

GPSGPS

Detected: Traffic Jam!

Page 6: MQTT with Java - a protocol for IoT and M2M communication

Hi, I’m Christian Götz

author & speakerbuilding solutions

CEO & co-founder

developer@goetzchr

Page 7: MQTT with Java - a protocol for IoT and M2M communication

IoT is happening now

WHY?

Page 8: MQTT with Java - a protocol for IoT and M2M communication

6,5 per person

DEVICES outnumber people

0,0

15,0

30,0

45,0

60,0

2003 2010 2015 2020

50,0

25,0

12,5

0,5

6,0

19,5

33,0

46,5

60,0

2003 2010 2015 2020

7,67,36,86,3

250 new every sec

http://blogs.cisco.com/news/cisco-connections-counter/

http://www.cisco.com/web/about/ac79/docs/innov/IoT_IBSG_0411FINAL.pdf

2020in billions

people

devices

Page 9: MQTT with Java - a protocol for IoT and M2M communication

Open Hardware is everywhere

Raspberry Pi is a trademark of the Raspberry Pi Foundation

Page 10: MQTT with Java - a protocol for IoT and M2M communication

Screenshot from JavaOne 2013

Java back to the roots

Page 11: MQTT with Java - a protocol for IoT and M2M communication

Screenshot from JavaOne 2013

Java back to the roots

Page 12: MQTT with Java - a protocol for IoT and M2M communication

HTTP is not suitable

WHY?

Page 13: MQTT with Java - a protocol for IoT and M2M communication

Request/Response Point-2-Point widely known

HTTP is too verbose

Page 14: MQTT with Java - a protocol for IoT and M2M communication

Challenges

Page 15: MQTT with Java - a protocol for IoT and M2M communication

Constrained devices

Bi-directional

Scalability

Unreliable Networks

Push Messaging

Security

… there are more ;)

IoTHTTP

IoT challenges

Page 16: MQTT with Java - a protocol for IoT and M2M communication

MQTT is a good fit

WHY?

Page 17: MQTT with Java - a protocol for IoT and M2M communication
Page 18: MQTT with Java - a protocol for IoT and M2M communication
Page 19: MQTT with Java - a protocol for IoT and M2M communication
Page 20: MQTT with Java - a protocol for IoT and M2M communication

Subscribe

Page 21: MQTT with Java - a protocol for IoT and M2M communication

Publish

Page 22: MQTT with Java - a protocol for IoT and M2M communication

Temperaturfühler MQTT-Broker

Laptop

Mobiles Endgerät

publish: “21°C“publish: “21°C“

publish: “21°C“

subscribe

subscribe

Page 23: MQTT with Java - a protocol for IoT and M2M communication

munichschwabing

isarvorstadt

people

temp

people

temp

/ /

MQTT topics 101

Page 24: MQTT with Java - a protocol for IoT and M2M communication

munichschwabing

isarvorstadt

people

temp

people

temp

/ /

munich/schwabing/temp

MQTT topics 101

Page 25: MQTT with Java - a protocol for IoT and M2M communication

munichschwabing

isarvorstadt

people

temp

people

temp

/ /

munich/+/temp

MQTT topics 101

Page 26: MQTT with Java - a protocol for IoT and M2M communication

munichschwabing

isarvorstadt

people

temp

people

temp

/ /

munich/schwabing/#

MQTT topics 101

Page 27: MQTT with Java - a protocol for IoT and M2M communication

munichschwabing

isarvorstadt

people

temp

people

temp

/ /

#

MQTT topics 101

Page 28: MQTT with Java - a protocol for IoT and M2M communication

On top of TCP

Simple

Publish/Subscribe Architecture

BinaryMinimal Overhead

Designed for unreliable networks

Data agnostic

MQTT facts

Page 29: MQTT with Java - a protocol for IoT and M2M communication

- Establishing 1st Connection(compensated after 5,5 min)

+ Reconnect

+ Maintaining Connection

+ Receiving Messages

+ Sending Messages

less battery better throughput

MQTT (with SSL) vs HTTPS

+ Less Overhead

+ Persistent Sessions

+ Pub/Sub Architecture

Source: http://stephendnicholas.com/archives/1217

Page 30: MQTT with Java - a protocol for IoT and M2M communication

1999 2010 2013 2014

Arlen Nipper & Andy Stanford-Clark

invent MQTTroyalty free OASIS TC

formedMQTT becomes Standard

MQTT history

Page 31: MQTT with Java - a protocol for IoT and M2M communication

Push instead of Poll

Minimal Bandwidth is important Mobile meets Enterprise

Unreliable networks Constrained devices

Low Latency

MQTT use cases

Page 32: MQTT with Java - a protocol for IoT and M2M communication

MQTT real world usage

https://www.facebook.com/notes/facebook-engineering/building-facebook-messenger/

10150259350998920

Page 33: MQTT with Java - a protocol for IoT and M2M communication

Quality of Service

Retained Messages Last Will and Testament

Persistent Sessions Heartbeats

Topic Wildcards

MQTT features

Page 34: MQTT with Java - a protocol for IoT and M2M communication

MQTT features Quality of Services

QoS 1

QoS 2

QoS 0B

B

B

Page 35: MQTT with Java - a protocol for IoT and M2M communication

MQTT features Last Will and Testament

BConnect

LWT

device123/status: „offline“

Bdropped

device123/status: „offline“

Page 36: MQTT with Java - a protocol for IoT and M2M communication

MQTT features w/o Retained Messages

Bdevice1/temp: „23,0“

device1

every 5 min

device2

device1/temp: „23,0“

Delay <= 5min

Page 37: MQTT with Java - a protocol for IoT and M2M communication

MQTT features Retained Messages

Bdevice1/temp: „23,0“ R

device1

every 5 min

device2

device1/temp: „23,0“ R

No Delay!

Page 38: MQTT with Java - a protocol for IoT and M2M communication

MQTT features Persistent Sessions

BConnect

Subscribedevice/+/status

device/12/status: „1“

1st

BRe-Connect

device/12/status: „1“2nd

Page 39: MQTT with Java - a protocol for IoT and M2M communication

MQTT-SN Overview

Gateway MQTT-Broker

MQTT-SN MQTT / -SN

• UDP instead of TCP • Topic is just an ID (Preregister/Register) • Sleeping Clients • Different types of Gateways • Discovery Mechanisms

Devices

B

Page 40: MQTT with Java - a protocol for IoT and M2M communication

MQTT over Websockets

Page 41: MQTT with Java - a protocol for IoT and M2M communication

MQTT Security

Protocol

Username/Password

Transport

TLS, client certificate authentification

Broker

fine-grained authentication, 3rd party integration

Page 42: MQTT with Java - a protocol for IoT and M2M communication

Broker implementationsWhat ?

Page 43: MQTT with Java - a protocol for IoT and M2M communication

MQTT Broker Mosquitto

Open Source

Ideal for constraint devices Supports Bridging

Written in C

Page 44: MQTT with Java - a protocol for IoT and M2M communication

MQTT Broker HiveMQ

High performance broker

Open Plugin System for Java Clustering

Bridging Scalable to > 100.000 connections

Native Websocket support

Page 45: MQTT with Java - a protocol for IoT and M2M communication

MQTT Broker

+ othershttp://mqtt.org/wiki/doku.php/brokers

Page 46: MQTT with Java - a protocol for IoT and M2M communication

MQTT Broker Public Broker

www.mqttdashboard.com

Page 47: MQTT with Java - a protocol for IoT and M2M communication

Getting StartedHow ?

Page 48: MQTT with Java - a protocol for IoT and M2M communication

MQTT Implementation

Page 49: MQTT with Java - a protocol for IoT and M2M communication

Open Source

“Reference Implementation”

Many languages: Java, Javascript, Lua, C, C++, Go, Python

Active Community

JS Library uses MQTT over Websockets

Paho facts

Page 50: MQTT with Java - a protocol for IoT and M2M communication

MqttClient client = new MqttClient( "tcp://localhost:1883", //URI "publisher", //Client ID new MemoryPersistence()); //Persistence

Paho Init Connection

Page 51: MQTT with Java - a protocol for IoT and M2M communication

MqttClient client = new MqttClient(...); !MqttConnectOptions connOptions = new MqttConnectOptions(); !connOptions.setKeepAliveInterval(120); connOptions.setWill("my/status", "offline".getBytes(), 2, true); connOptions.setCleanSession(false); connOptions.setUserName("user"); connOptions.setPassword(„pass".toCharArray()); !client.connect(connOptions);

Paho Init Connection

Page 52: MQTT with Java - a protocol for IoT and M2M communication

!!client.publish("my/topic/123",//topic "Hello!".getBytes(), //message

1, //QoS false); //retained !!

client.disconnect();

Paho Synchronous API

Page 53: MQTT with Java - a protocol for IoT and M2M communication

final MqttClient client = new MqttClient(...); client.setCallback(new MqttCallback() { @Override public void connectionLost(Throwable cause) {} !@Override public void messageArrived(String topic, MqttMessage message)throws Exception { System.out.println(new String(message.getPayload())); } !@Override public void deliveryComplete(IMqttDeliveryToken token) {} }); !client.connect(); client.subscribe("#");

Paho Synchronous API

Page 54: MQTT with Java - a protocol for IoT and M2M communication

MqttAsyncClient client = new MqttAsyncClient(...); client.connect(null, new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) { try { client.publish(...); } catch (MqttException e) {}

} @Override public void onFailure(IMqttToken asyncActionToken, Throwable exception) {} }); !client.disconnect();

Paho Asynchronous API

Page 55: MQTT with Java - a protocol for IoT and M2M communication

MQTT Implementation

FuseSource MQTT Client

Page 56: MQTT with Java - a protocol for IoT and M2M communication

Open Source

3 API Styles

Automatic Reconnect

Maven Central

Less active Community

FuseSource facts

Page 57: MQTT with Java - a protocol for IoT and M2M communication

MQTT mqtt = new MQTT(); mqtt.setHost("localhost", 1883); mqtt.setClientId("publisher"); mqtt.setCleanSession(true); mqtt.setWillTopic("publisher/status"); mqtt.setWillMessage("offline"); mqtt.setKeepAlive((short) 60); mqtt.setUserName("mqtt-client"); mqtt.setPassword("mqtt");

FuseSource Init Connection

Page 58: MQTT with Java - a protocol for IoT and M2M communication

BlockingConnection connection = mqtt.blockingConnection(); !connection.connect(); !connection.publish( "publisher/status", // topic "online".getBytes(StandardCharsets.UTF_8), QoS.AT_MOST_ONCE, false);

FuseSource Blocking API

Page 59: MQTT with Java - a protocol for IoT and M2M communication

FutureConnection connection = mqtt.futureConnection(); !Future<Void> f1 = connection.connect(); f1.await(); !Future<Void> f2 = connection.publish( "publisher/status", "online".getBytes(), QoS.AT_LEAST_ONCE, true); !f2.await(); !Future<Void> f3 = connection.disconnect(); f3.await();

FuseSource Future API

Page 60: MQTT with Java - a protocol for IoT and M2M communication

CallbackConnection connection = mqtt.callbackConnection(); connection.connect(new Callback<Void>() { public void onSuccess(Void v) { connection.publish( "publisher/status", "online".getBytes(), QoS.AT_LEAST_ONCE, false, new Callback<Void>() { public void onSuccess(Void v) {} public void onFailure(Throwable value){} }); } ! public void onFailure(Throwable value) {} });

FuseSource Callback API

Page 61: MQTT with Java - a protocol for IoT and M2M communication

It’s ShowtimeDemo

Page 62: MQTT with Java - a protocol for IoT and M2M communication

Demo from device to the web with MQTT

https://github.com/dc-square/mqtt-with-paho-eclipsecon2013

available on

Page 63: MQTT with Java - a protocol for IoT and M2M communication

Demo Under the hood

Page 64: MQTT with Java - a protocol for IoT and M2M communication

Danke! Thanks!

@goetzchr


Recommended