MQTT with Java - a protocol for IoT and M2M communication

Preview:

DESCRIPTION

Our digital world is growing rapidly and we have more devices connected to the internet than ever. On top of that each second 80 new devices are added, which introduces new challenges to communication between these devices. MQTT is a lightweight and scalable protocol that shifts the request/response paradigm of the web as it is today to an event-driven publish and subscribe architecture, which is a perfect fit for Internet of Things and M2M use cases. This talk answers the following three questions: Why do we need a paradigm shift, HTTP has been proven to be a good fit for the web? What is MQTT and how does it help to overcome the challenges we have today? How can everybody build their own MQTT application with the implementation that are available for Java developers ? In the last part we will dive into Eclipse Paho and the FuseSource client library and round up the talk with an live demonstration.

Citation preview

MQTT with Java - a protocol for IoT and M2M communication

!Christian Götz, dc-square

IoT?

M2MIoT

IoEWoT

Web of Things

Internet of Things

Internet of Everything

Machine to Machine communication

UbicompUbiquitous computing

CPSCyper Physical Systems Pervasive

Computing

Technology that connects Devices

over wired or wireless communication

GPSGPS

GPSGPS

GPS

GPSGPS

Detected: Traffic Jam!

Hi, I’m Christian Götz

author & speakerbuilding solutions

CEO & co-founder

developer@goetzchr

IoT is happening now

WHY?

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

Open Hardware is everywhere

Raspberry Pi is a trademark of the Raspberry Pi Foundation

Screenshot from JavaOne 2013

Java back to the roots

Screenshot from JavaOne 2013

Java back to the roots

HTTP is not suitable

WHY?

Request/Response Point-2-Point widely known

HTTP is too verbose

Challenges

Constrained devices

Bi-directional

Scalability

Unreliable Networks

Push Messaging

Security

… there are more ;)

IoTHTTP

IoT challenges

MQTT is a good fit

WHY?

Subscribe

Publish

Temperaturfühler MQTT-Broker

Laptop

Mobiles Endgerät

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

publish: “21°C“

subscribe

subscribe

munichschwabing

isarvorstadt

people

temp

people

temp

/ /

MQTT topics 101

munichschwabing

isarvorstadt

people

temp

people

temp

/ /

munich/schwabing/temp

MQTT topics 101

munichschwabing

isarvorstadt

people

temp

people

temp

/ /

munich/+/temp

MQTT topics 101

munichschwabing

isarvorstadt

people

temp

people

temp

/ /

munich/schwabing/#

MQTT topics 101

munichschwabing

isarvorstadt

people

temp

people

temp

/ /

#

MQTT topics 101

On top of TCP

Simple

Publish/Subscribe Architecture

BinaryMinimal Overhead

Designed for unreliable networks

Data agnostic

MQTT facts

- 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

1999 2010 2013 2014

Arlen Nipper & Andy Stanford-Clark

invent MQTTroyalty free OASIS TC

formedMQTT becomes Standard

MQTT history

Push instead of Poll

Minimal Bandwidth is important Mobile meets Enterprise

Unreliable networks Constrained devices

Low Latency

MQTT use cases

MQTT real world usage

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

10150259350998920

Quality of Service

Retained Messages Last Will and Testament

Persistent Sessions Heartbeats

Topic Wildcards

MQTT features

MQTT features Quality of Services

QoS 1

QoS 2

QoS 0B

B

B

MQTT features Last Will and Testament

BConnect

LWT

device123/status: „offline“

Bdropped

device123/status: „offline“

MQTT features w/o Retained Messages

Bdevice1/temp: „23,0“

device1

every 5 min

device2

device1/temp: „23,0“

Delay <= 5min

MQTT features Retained Messages

Bdevice1/temp: „23,0“ R

device1

every 5 min

device2

device1/temp: „23,0“ R

No Delay!

MQTT features Persistent Sessions

BConnect

Subscribedevice/+/status

device/12/status: „1“

1st

BRe-Connect

device/12/status: „1“2nd

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

MQTT over Websockets

MQTT Security

Protocol

Username/Password

Transport

TLS, client certificate authentification

Broker

fine-grained authentication, 3rd party integration

Broker implementationsWhat ?

MQTT Broker Mosquitto

Open Source

Ideal for constraint devices Supports Bridging

Written in C

MQTT Broker HiveMQ

High performance broker

Open Plugin System for Java Clustering

Bridging Scalable to > 100.000 connections

Native Websocket support

MQTT Broker

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

MQTT Broker Public Broker

www.mqttdashboard.com

Getting StartedHow ?

MQTT Implementation

Open Source

“Reference Implementation”

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

Active Community

JS Library uses MQTT over Websockets

Paho facts

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

Paho Init Connection

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

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

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

client.disconnect();

Paho Synchronous API

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

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

MQTT Implementation

FuseSource MQTT Client

Open Source

3 API Styles

Automatic Reconnect

Maven Central

Less active Community

FuseSource facts

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

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

FuseSource Blocking API

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

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

It’s ShowtimeDemo

Demo from device to the web with MQTT

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

available on

Demo Under the hood

Danke! Thanks!

@goetzchr

Recommended