Wojciech Galanciak
Senior Eclipse Developer on the MyEclipse and Webclipse products.
Senior Eclipse Developer on the MyEclipse and Webclipse products.
The Internet of Things (IoT) is a fast growing market. The general idea behind IoT it is to connect everything (clothes, devices, houses, public transport, medical devices, etc.) into a set of networks to raise precision, productivity or even define a completely new user experience. This article focuses on different applications of IoT technologies, integration with different devices and utilization of different tools and frameworks.
As defined, the core of IoT is a communication itself. In the modern Internet there is an impressive number of protocols and frameworks used to implement communication channels. This variety is drastically reduced in an IoT environment. In most cases, “things” that are communicating with one another have limited resources (processor, memory, battery, etc.). Fortunately, there are solutions for those restricted requirements. One of them is MQTT – a lightweight connectivity protocol.
According to MQTT.org, it was designed as an extremely lightweight publish/subscribe messaging transport. With this approach there is no direct connection between a publisher (a source of data) and a subscriber (access data requestor). Thanks to that, the publisher does not need to know anything about a data destination. It also does not need to handle any requests because responsibility for pushing data to the outside world is only on its side. The server role is played by a MQTT broker which:
The only common part is a knowledge about the topic, an identifiable name of a particular messages group. For example, “home/widows/state” may refer to the state (opened, closed) of windows in an intelligent house. Each window has its own state detector. If the state changes, then the detector publishes a proper message with this concrete topic to a broker. Then the broker pushes it to all clients (e.g., a mobile phone application) subscribed to this topic which in this example can warn the owner if someone tries to break into his house.
It is important to mention that the subscriber does not know the source of a particular message. This means that all information required for window identification must be a part of the message itself. The message contains only one additional flag – Quality of Service (QoS). MQTT support three levels of QoS:
To find more details about QoS please look at References section.
There is a set of different implementations of MQTT protocol available on the market. We decided to focus on Eclipse Paho – open source implementation under the Eclipse Foundation. One of its main advantages is that it provides implementations for a wide range of languages/technologies, from JavaScript to embedded C. In this article we are focused only on two of them: Java and JavaScript.
The purpose of this article is to present MQTT basis including a MQTT protocol in action. To do that we will create a web application which reads the temperature from a remote sensor device and displays it in a web browser. Instead of a real sensor device we will create a Java application to simulate its behavior. Please refer to the attached sample projects to follow along.
Our goals are to:
Let’s create the source of our temperature data. As mentioned earlier, we will use a Java mock application instead of a real sensor device. Eclipse Paho provides an easy method to use Java MQTT client implementation. The project structure is as follows:
It contains only two elements:
The general workflow is described below.
Like the Java application, we will use Paho, but this time it is a JavaScript implementation of MQTT client. It is just a single library file (mqttws31.js) which needs to be added to a web application.
Here is the project structure:
JavaScript implementation is based on WebSockets for connection with a MQTT server. This approach requires a broker with native WebSockets support or a gateway which forwards communication between WebSockets and TCP.
The client application workflow is presented below.
The user interface is very simple. It contains one Connect/Disconnect button where the label and color depend on the connection state. There is also a nice temperature widget. The whole UI is based on Bootstrap framework.
Both applications work independently. For example, you can open the web application and choose to connect. If there is no Java application running then the temperature widget will not be updated. Once it is started the temperature starts to change.
This article included an introduction to MQTT protocol. The example communication is very basic and does not apply to a real scenario (e.g., see what happens if you launch two Java application instances).
mqtt-temperature-publisher.zip—Sample Eclipse Java project that uses Eclipse Paho to simulate a sensor device.
mqtt-tempterature-client.zip—A simple web application that connects to our simulated sensor to display temperature information.
MQTT – http://mqtt.org
MQTT Quality of Service – http://www.hivemq.com/mqtt-essentials-part-6-mqtt-quality-of-service-levels
Eclipse Paho – http://www.eclipse.org/paho
Bootstrap – http://getbootstrap.com
Temperature Widget – http://codepen.io/anon/pen/GppQZd