MQTT with Java - a protocol for IoT and M2M communication

  • 1,544 views
Uploaded on

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 …

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.

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,544
On Slideshare
0
From Embeds
0
Number of Embeds
3

Actions

Shares
Downloads
37
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. MQTT with Java - a protocol for IoT and M2M communication ! Christian Götz, dc-square
  • 2. IoT?
  • 3. M2M IoT IoE WoT Web of Things Internet of Things Internet of Everything Machine to Machine communication Ubicomp Ubiquitous computing CPS Cyper Physical Systems Pervasive Computing
  • 4. Technology that connects Devices over wired or wireless communication
  • 5. GPSGPS GPS GPS GPS GPS GPS Detected: Traffic Jam!
  • 6. Hi, I’m Christian Götz author & speaker building solutions CEO & co-founder developer @goetzchr
  • 7. IoT is happening now WHY?
  • 8. 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 2020 in billions people devices
  • 9. Open Hardware is everywhere Raspberry Pi is a trademark of the Raspberry Pi Foundation
  • 10. Screenshot from JavaOne 2013 Java back to the roots
  • 11. Screenshot from JavaOne 2013 Java back to the roots
  • 12. HTTP is not suitable WHY?
  • 13. Request/Response Point-2-Point widely known HTTP is too verbose
  • 14. Challenges
  • 15. Constrained devices Bi-directional Scalability Unreliable Networks Push Messaging Security … there are more ;) IoT HTTP IoT challenges
  • 16. MQTT is a good fit WHY?
  • 17. Subscribe
  • 18. Publish
  • 19. Temperaturfühler MQTT-Broker Laptop Mobiles Endgerät publish: “21°C“ publish: “21°C“ publish: “21°C“ subscribe subscribe
  • 20. munich schwabing isarvorstadt people temp people temp / / MQTT topics 101
  • 21. munich schwabing isarvorstadt people temp people temp / / munich/schwabing/temp MQTT topics 101
  • 22. munich schwabing isarvorstadt people temp people temp / / munich/+/temp MQTT topics 101
  • 23. munich schwabing isarvorstadt people temp people temp / / munich/schwabing/# MQTT topics 101
  • 24. munich schwabing isarvorstadt people temp people temp / / # MQTT topics 101
  • 25. On top of TCP Simple Publish/Subscribe Architecture BinaryMinimal Overhead Designed for unreliable networks Data agnostic MQTT facts
  • 26. - 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
  • 27. 1999 2010 2013 2014 Arlen Nipper & Andy Stanford-Clark invent MQTT royalty free OASIS TC formed MQTT becomes Standard MQTT history
  • 28. Push instead of Poll Minimal Bandwidth is important Mobile meets Enterprise Unreliable networks Constrained devices Low Latency MQTT use cases
  • 29. MQTT real world usage https://www.facebook.com/notes/facebook- engineering/building-facebook-messenger/ 10150259350998920
  • 30. Quality of Service Retained Messages Last Will and Testament Persistent Sessions Heartbeats Topic Wildcards MQTT features
  • 31. MQTT features Quality of Services QoS 1 QoS 2 QoS 0 B B B
  • 32. MQTT features Last Will and Testament B Connect LWT device123/status: „offline“ B dropped device123/status: „offline“
  • 33. MQTT features w/o Retained Messages B device1/temp: „23,0“ device1 every 5 min device2 device1/temp: „23,0“ Delay <= 5min
  • 34. MQTT features Retained Messages B device1/temp: „23,0“ R device1 every 5 min device2 device1/temp: „23,0“ R No Delay!
  • 35. MQTT features Persistent Sessions B Connect Subscribe device/+/status device/12/status: „1“ 1st B Re-Connect device/12/status: „1“ 2nd
  • 36. 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
  • 37. MQTT over Websockets
  • 38. MQTT Security Protocol Username/Password Transport TLS, client certificate authentification Broker fine-grained authentication, 3rd party integration
  • 39. Broker implementations What ?
  • 40. MQTT Broker Mosquitto Open Source Ideal for constraint devices Supports Bridging Written in C
  • 41. MQTT Broker HiveMQ High performance broker Open Plugin System for Java Clustering Bridging Scalable to > 100.000 connections Native Websocket support
  • 42. MQTT Broker + others http://mqtt.org/wiki/doku.php/brokers
  • 43. MQTT Broker Public Broker www.mqttdashboard.com
  • 44. Getting Started How ?
  • 45. MQTT Implementation
  • 46. Open Source “Reference Implementation” Many languages: Java, Javascript, Lua, C, C++, Go, Python Active Community JS Library uses MQTT over Websockets Paho facts
  • 47. MqttClient client = new MqttClient( "tcp://localhost:1883", //URI "publisher", //Client ID new MemoryPersistence()); //Persistence Paho Init Connection
  • 48. 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
  • 49. ! ! client.publish("my/topic/123",//topic "Hello!".getBytes(), //message 1, //QoS false); //retained ! ! client.disconnect(); Paho Synchronous API
  • 50. 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
  • 51. 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
  • 52. MQTT Implementation FuseSource MQTT Client
  • 53. Open Source 3 API Styles Automatic Reconnect Maven Central Less active Community FuseSource facts
  • 54. 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
  • 55. BlockingConnection connection = mqtt.blockingConnection(); ! connection.connect(); ! connection.publish( "publisher/status", // topic "online".getBytes(StandardCharsets.UTF_8), QoS.AT_MOST_ONCE, false); FuseSource Blocking API
  • 56. 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
  • 57. 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
  • 58. It’s Showtime Demo
  • 59. Demo from device to the web with MQTT https://github.com/dc-square/mqtt-with-paho-eclipsecon2013 available on
  • 60. Demo Under the hood
  • 61. Danke! Thanks! @goetzchr