Connecting the dots for an IoT scenario
What is Internet of Things (IoT)?
Today IoT is a buzz word in the market. But, what is IOT- Internet of Things?
Internet of things is all about collecting and managing massive amount of data from a rapidly growing network of devices and sensors, processing the data, and then sharing it with other connected things. Though we have already seen this in a limited scope like security systems in our home, car’s self-monitoring capabilities etc., still there exists a lot more devices to be interconnected to make it a truly connected world.
Figure 1: A connected world (Source)
Here is an example,
- Your morning might be a little different with the advent of IoT. The alarm goes off a little earlier than usual because our home’s smart hub has detected a traffic condition suggesting an unusual slow commute.
- The weather sensor warns of a significantly high pollen count, and you decide to wear your suit with the sensors that track air quality and alert you to allergens that could trigger an allergic attack.
- You have time to check your messages at the kitchen e-screen. The test results from your recent medical check-up are in, and there’s a message from your doctor that reiterates his recommendations for a healthier diet. And accordingly you can shop online for your weekly grocery. The e-screen on the refrigerator door suggests as per your doctor’s recommendation, yogurt and fresh fruit for breakfast.
The details of the morning will vary from person to person, but this type of a scenario is not very far away. Incredible advances in devices, sensors, and machine-to-machine (M2M) connectivity make the Internet of Things possible and represent an unprecedented opportunity for businesses to can make it happen. Data is getting prime importance in the IOT scenario and the same goes for analytics and processing of this big data. Since we are foreseeing huge data processing and analytics needs in the future, cloud seems to be most obvious answer to the problem at hand. Amazon has already developed the cloud platform for IOT web services as part of its AWS offering.
Some Facts in support of IoT:-
- Gartner estimates the total economic value from internet of things across industries will reach 1.9 trillion worldwide.
- Today 11 percent of all data is machine-generated. IDC estimates that by 2020, the number will rise to 40 percent, with somewhere between 20 and 50 billion devices fuelling that growth.
So what are the dots involved in connecting the devices, making it talk to each other? Since I practice Java, I shall look for the possibilities in Java for IoT implementation and success in the coming years.
IoT and Java
The abstractness involved in IoT is primarily attributed to Service Gateways.
Service Gateways can connect the devices that communicate via specific protocols, store and parse the information, and send them over to cloud servers for processing and analytics. In turn the server can control the device according to the processed information.
For ensuring sustainable interoperability in an IoT world there are two dominant architectures for data exchange protocols:
- BUS based (REST etc.)
- Broker based (MQTT, CoAp etc.)
For our typical implementation, I am going to take up MQTT later on.
Gateways are the essential part of all IoT devices, managing the following important functionalities:
1) Hardware and field abstraction – Sensor Connectivity, I/O Access etc.
2) Network and connectivity – Connectivity is a bigger challenge here since there are numerous devices with varying connectivity capabilities. Low bandwidth, High Latency, network issues, frequently network disruptions may render the device with very limited resources available.
3) Manage application – We should be able to start/stop, install/uninstall application remotely. We should be able to update specific modules without interrupting the functioning of the device as a whole.
4) Managing the connections for the data exchange – MQTT broker connections etc.
When I look at IoT working in the realms of Java, I see Eclipse KURA.
Eclipse KURA is an eclipse IOT project that provides the platform for building IOT gateways. As an Open java middleware for IOT gateways, this handles the abstraction involved with the connectivity of the devices. Kura runs on jvm and leverages OSGi, which helps in managing the application into bundles or modules, remote administration of the application (such as stop/start, install/uninstall etc). Kura APIs offer easy access to the underlying hardware including serial ports, GPS, watchdog, USB, GPIOs, I2C, etc. It also offer OSGi bundle to simplify the management of network configurations, communication with IoT servers, and remote management of the gateway. So our application will sit on the KURA middle ware and leverage the services provided from this platform.
Now we move on to the application part and the protocols involved in connecting with cloud services and devices.
Typically here we have the application code which gathers the information and controls the device. Sometimes it is required to consume some services, which might be cloud based, for processing the data gathered from these devices. This employs bus based data exchange protocols like REST. KURA provides the API layer to connect to the cloud based services. For getting the telemetry data from the devices typically we use the MQTT protocol (broker based data exchange).
MQTT: – MQ Telemetry Transport
How we connect things at the periphery of the network is all about MQTT. MQTT is an event and message oriented protocol allowing devices to asynchronously communicate efficiently across constrained networks to remote systems. MQTT is one of the ways to get data from devices, sensors, actuators, embedded controllers, mobile etc to intelligent centralized systems for analysis and processing and to handle the constraints of the network like high latency, low bandwidth, battery drains fast and the message could not be send /receive, All these problems are solved by MQTT message system,
MQTT is a message protocol which implements the publish/subscribe paradigm. The application client library publishes messages to the topic, and the topic could be any subject of interest. Once the message is published, it goes into the broker. The consumers/subscribers connect to that broker expressing interest on receiving messages on a given topic and the broker will push the relevant messages to these subscribers. If the topic does not have any subscriber, those messages will be discarded.
Naming convention for the topics uses hierarchy as shown below:
State, district etc. are sub topics under the main topic ‘Country’. The subscribers can subscribe to absolute topics or can use wildcards to include all sub topics within a given topic.
<country>/# will have topics including all the states falling under that country.
Say if the topic name is India/Kar/Mysore/560014/temperature, I would be interested to know the temperature of the city Mysore only.
There are two types of subscriptions
1) Durable subscription: Once the subscription is in place, broker will forward messages to the subscriber immediately if the subscriber is connected. If the subscriber is not connected, messages are stored in broker until the next time the subscriber is connected.
2) Non-Durable subscription: The life of the message is for that period the subscriber is connected to the broker. Once disconnected, the message will be lost.
Client can publish a message based on an event as well, like the device is about to die or battery is getting drained, in those events we can configure the client to publish the message to the broker for HELP.
Benefits of MQTT vs HTTP
1. MQTT is low latency, push delivery of messages from client to server and server to client (bi-directional). This helps to bring the event oriented architecture to the web. In the other hand HTTP is push from the client to server but pull from server to client, which works to be expensive.
2. MQTT messages will be delivered even when the network breaks. HTTP messages are lost once there is a break in the network.
a. So eclipse kura projects uses the PAHO library which is a client library for MQTT messaging system.
Client PAHO library API usage
MqttClientPersistence per=new MqttClientPersistence(“/tmp”);
MqttClient client=new MqttClient(“tcp://iot.eclipse.org:1883”,”client1”,per)
Here MQTT client takes broker connection details like hostname, port and the client ID. The client ID should be unique across the broker, and optional persistence storage place.
Once the client is created we need to create the message and then publish the message to the particular topic as shown below
MqttMessage msg=new MqttMessage(“Message”.getbytes());
We could add callback mechanism for messages to know its delivery status, and to check the exception if the connection is lost. There exists many client implementations for MQTT brokers, one of them is eclipse PAHO and many tools are freely available to work around specificities for the messaging system. For example: MQTTspy, Elcipse Paho, WMQTT client utility etc.
Now that we have the connecting pieces for a typical IoT implementation, let us looks at the Gateways.
The Eclipse KURA platform sits ON Java embedded edition or micro edition depending on the device capabilities and this platform can connect as many devices as possible.
For example :- RASPBERRY PI with the KURA platform could be able to hold the software and the application code to connect to the device we are looking for using the connectivity part of the platform like GPIO , USB etc.
In short KURA platform turns Raspberry PI as the IoT gateway.
What is RaspBerry pi?
It is a credit card sized computer with lots of interfaces to connect to TV, computer, standard keyboard etc. It is portable, robust and connects to real work objects.
Business application: Java code leverages the services and library provided by the KURA platform to connect to the devices underlying the system and propagate back message to the device using one of the possible M2M space protocol, such as MQTT using the Paho client library.
Once we have the IOT gateway in place, we can make different devices, talk to each other and act accordingly. In my upcoming posts I shall speak about my experiences with Raspberry Pi and IoT implementations.