Getting Started With IBM Watson IoT Platform

So this Thursday turned out to be a quite a productive day thanks to the unexpected invitation to the IBM Analytics workshop sponsored by my company. Mr. Rajesh M Jeyapaul and Mani Madhukar from IBM India conducted well composed sessions on IBM Watson IoT platform and Analytics platform. The session consisted of two parts, where the 1st part focused on creating your own IoT application using the IBM Watson IoT Platform while  the 2nd part was about analyzing data with Data Science Experience (DSX). In this article I’ll be writing about how to get your hands dirty with Watson IoT platform to create your own IoT application and store the data into IBM Cloud.

XaaS consists of several cloud computing models in which IBM Cloud combines the PaaS (Platform as a Service) and IaaS (Infrastructure as a Service) models through a catalog of cloud services to assist in rapid implementation of a wide variety of business applications. With IBM Cloud in the picture, you are relieved off the concerns of making your own heavy investments to run and test your app. From development sandboxes to distributed production environments, IBM Cloud offers all kinds of services, containers and tools you might need. So whether its deployment, security or expansion, none if it is of your concern now!

In this tutorial, let me get you started with the Internet of Things Platform Starter boilerplate which is one of several boiler plates, available in the IBM Cloud dashboard.

1. The Dashboard of Possibilities

Load the IBM Cloud dashboard and create yourself a free account.  Once you login to the dashboard, click on the Catalog menu item at the top. You might get the “Lite” filter applied on the search box, which indicates the boiler plates that could be tried for free with no restrictions on the period of usage. Remove the filter to witness the wide variety of projects that could be created without breaking a sweat to setup the environment. From bare metal servers to block chain services, possibilities are countless! Clicking on the boiler plate would provide you with a brief description which is pretty much self explanatory. Once you are done exploring, click on Internet of Things Platform Starter to create our project. This would create you a sample NodeJS application which could be used to collect and store data in the cloud.

Here’s a sneak peek into the architectural diagram in concern.

Capture.PNG

Credits: Kashyap Ravichandran and Rajesh K Jeyapaul

2. Setting up The Project

Enter a name for your project in the App Name text box and choose a region to deploy your application in. Let me specify the name as mayooran-iot-test. and region to be US South.

NOTE: App Name has to be UNIQUE!

Once you fill these two in, rest of the text box values would be generated for you. So now your form should look something like this.

Capture

As you scroll down you could see the technologies involved with this boiler plate. We will be presented with platform consisting of Node JS SDK, Watson IoT Platform (MQ Broker) and Cloudant NoSQL for database. Of course we have chosen the Lite plan here, which is the free plan that lets you play around with the projects and services. Without further ado, click on the Create button. Now you will be redirected to your project page where you could see the state of your environment as shown below.

Capture

Once everything is set to go, you would see the state change to started/awake! (Give it some time folks, takes a couple of minutes!) Now visit the dashboard home page and you should your project listed. Below is how mine looks.

Capture

Under Cloud Foundry Services, click on the Internet of Things Platform under Service Offering column(mayooran-iot-test-iotf-service in my case). Now you’ll be presented with the Watson IoT platform Service page as shown below. Click on Launch to kick things off!

Capture.PNG

3. Creating a Device And Fetching Data

Upon clicking Launch on the above image, you’ll be landing on the page shown below.

Capture.PNG

Click on the highlighted Devices icon to create a new device and connect it to the platform. On this Devices page, click on Device Types from the menu and click on +Add Device Type button as shown below.

Capture.PNG

On the device type creation form, leave the Type as Device. Enter a Name for the device and a Description if you like and click Next.  Just to be sure here’s what I have now.

Capture

On the Device Information form you get now, you can leave the information empty and click Done. Upon successful addition you should now see the page shown below. Click on Register Devices.

Capture

On the device creation form, specify a Device ID and click Next.

Capture

Again you may leave the Device Information form empty as shown below and click Next.

Capture.PNG

On the Device Security form, specify an Authentication Token which would be used when establishing connection from the device and click Next.

Capture.PNG

click Done from the device information page as shown below.

Capture.PNG

You’ll now be directed to the Device Drilldown page where you can check the Device Credentials, Connection Information, Recent Events etc. Make a note of the credentials, as you will need this when connecting your device to the cloud.

Capture.PNG

4. Generating Data From Your Mobile Device

Now let us use the mobile app Lyfas to generate the pulse data which will be storing in IBM Cloud. If you are not in possession of an Android mobile, use the IoT Sensor Simulator to generate your data. Android users, follow the below steps to capture pulse data from your mobile and stream.

Step I : Open the installed Lyfas application. Enter a unique ID when prompted for (Your mobile number would do).

Capture.PNG

Step II:  Click on the menu, and select Settings.

Capture.PNG

Step III: Under Blumix Settings specify the credentials you had noted down when we were setting up the device earlier, and click Save. Some other options are using an API key or using a MQTT broker. Below is how it would look with respect to the settings I had specified.

Screenshot_20171201-204223

Step IV: Now in order to capture your pulse data, click on Start Pulse from the drop down as shown below.

Capture.PNG

Step V: Then hold your finger on the flash light to capture your pulse data. Perform this as gentle as possible, to avoid skin burns. Now you should see the data being captured and a graph drawn on the screen.

Step VI: Now click on Stream toggle to start streaming the captured pulse data. This will ensure that the captured data is pushed to the IBM Watson IoT Cloud.

Capture

NOTE: If you are unable to see the streaming data, there could be a TLS authentication issue. In order to fix this temporarily, you can set the Security Level to TLS Optional. This could be set by clicking on the Settings icon in the dashboard, and selecting TLS Optional from the drop down menu for Security Level.

Now you if you go back to the IBM Watson IoT platform dashboard and select the device, you could see the Recent Events menu. Under this, you could find the pulse sensor data that was streamed from your mobile device!

Capture.PNG

If you click on the State tab, you could find the most recent data that was transmitted.

Capture.PNG

5. Node-RED In Action

Now that we’ve got the data into the cloud, we could play around with the node-red run time we have been provided. Let’s now go back to your bluemix dashboard, and click on the Node-RED url as shown below.

Capture.PNG

When the page loads, click on Next as shown below.

Capture

On the second window, you can disable the security settings (As this a simple demo) as shown below and click Next.

Capture

Now click on Finish on the final window to launch the node-red page. Then click go to your Node-RED flow editor as shown below.

Capture.PNG

Node-RED allows a visual programming approach to build IoT applications using a simple flow based approach. If you are interested in learning the basics of Node-RED, the documentation here provides you with every information you need.

On the flow editor, double click on IBM Iot App In to configure the device from which we will be gathering the input. Fill in the form with the Device Type and Device Id as shown below. You may leave the rest of the details with the default values. and click Done. Now Click the Deploy button at the top of the editor to complete deployment of the flow.

Capture.PNG

Now when you start the cam pulse again and start streaming data from your mobile app, you will see the live data appearing on the debug tab as shown below.

Capture.PNG

Now let us store this data in the cloudant database instance we were provided with. Scroll down on the list of nodes available at the left side of the editor and choose cloudant out node from the storage category as shown below.

Capture.PNG

Drag and drop this node on to the flow editor, and double click on it to start configuring. Service will be automatically loaded for you hence you just need to specify the Database as shown below.

Capture.PNG

Click on Done and then click Deploy.

Now go back to your dashboard and click on the Cloudant NoSQL DB instance as shown below.

Capture.PNG

On the Service Details page, click on Launch.

Now you could see the database instance created as below. Publish some data again and you could see the data flow into mydb instance.

Capture.PNG

Click on the mydb instance, to view the data that has been stored.

Capture.PNG

Well that’s it folks! You’ve stored your sensor data in a cloudant database instance through a Node-RED editor with minimal connection configurations and did you even bother installing anything or setting up any software? Well that’s what IBM Cloud brings to the picture. All you ever did connect the mobile app that publishes sensor data and just created a flow consisting of two nodes to get into the cloudant database.

I hope that would’ve given you a teaser to the main movie called IBM Cloud and its endless possibilities. Once you have figured out these steps, it shouldn’t take you more than 10-15 minutes! So I’ll wrap up the capabilities of IBM Watson IoT platform and in the next tutorial let’s see how we can use the analytic capabilities of IBM Watson. I’ll leave you here with a teaser for that! 🙂

Entire credits go to Mr. Rajesh K Jeyapaul and Mani Madhukar from IBM India for conducting such an informative and productive session in such a limited time, ensuring total participation!!

 

Advertisements

Creating a MQTT client using Javascript for NodeJS and browser

Recently I came across this requirement where I had to implement a MQTT client using Javascript for NodeJS and browser. It is easier than you think it is using the npm mqtt package . Latest release till date is 1.11.2 but if you run into any errors while using browserify on the created client, then I advice switching back to 1.8 version because of a dependency issue with the dependency mqtt-packet . Install the mqtt dependency using


npm install mqtt --save

You could then require the module as shown below.


var mqtt    = require('mqtt');

Create a simple client passing in the MqttOptions object. Information on the properties has been given on the git hub page but it is very basic. If you’re someone new to MQTT, you’re better off reading the explanation provided in HiveMQ essentials series.

Create a simple MqttOptions as shown below passing in the basic parameters.


var mqttOptions = {
clientId: 'f1b948b7-2114-4c8e-962f-d15f4cf90abe',
protocolId: 'MQTT',
protocolVersion: 4,
keepalive: 10000,
clean: false,
reconnectPeriod: '1000',
will: willMessage
};

The above would require a will message object which would be used to notify connected clients about another disgracefully disconnected client. Creating it would be done as follows.


var willMessage = {
topic: 'WillMessage',
payload: 'This is the last will message',
qos: 2,
retain: true
};

Use the above created MqttOptions object to establish the connection to the Mosquitto broker.


var client = mqtt.connect("mqtt//:localhost:1883", mqttOptions);

URL could be one of  ‘mqtt’, ‘mqtts’, ‘tcp’, ‘tls’, ‘ws’, or ‘wss’. I’ll cover establishing secure connection methods in the up coming tutorials.

Once the connection has been established you can publish and subscribe to messages as shown below.


client.subscribe('someTopic');

client.publish('someTopic','someMessage');

You could also hook onto the following callbacks and implement logic accordingly.

  • connect – function(connack) {}
  • reconnect – function() {}
  • close – function() {}
  • offline – function() {}
  • error- function(error) {}
  • message – (topic, message, packet) {}
  • packetsend – function(packet) {}
  • packetreceive – function(packet) {}

Below is an example implementation of the connect call back mentioned above.

 


client.on('connect', function () {

console.log('client connected');

});

client.on('message', function (topic, message) {

console.log(message.toString());

});

Auto reconnect when failing to connect to the server has already been implemented through the library and you could see this by hooking onto the ‘reconnect’ method. I have come across an abnormal behavior where the previously subscribed topics have been lost after successfully connecting on a reconnect, though I have set clean property in MqttOptions object to false. Check whether you encounter this issue and if so, keep track of the subscribed topics in a list and subscribe to them again in the reconnect callback.

You could terminate the client using client.disconnect method.

I hope that would have given a clear idea as to how to create a MQTT client using Javascript for NodeJS. You could use browserify to create a version of the file that could be used in browsers. As I mentioned at the start of this post, if you encounter an errors while browserifying the file, always switch back to the 1.8 version in which it works fine.

So I’ll wind up this post for here now. I’ll add another post on how to establish ssl/tls and wss connections through the clients! 🙂

Getting started with MQTT and Mosquitto

What it is and why you would need it

If you’re looking for a light weight messaging protocol then MQTT would an answer you could consider. It follows the publish-subscribe mechanism but of course you could tweak it to suit one-one messaging as well. MQTT has been quite a trending topic these days with the evolution of the Internet of Things. Objectives of this protocol is to have high reliability through assurance of delivery while playing with minimum network bandwidth. Clear evidence of this being achievable through the protocol has been visible through the usage of it in IOT where sensors, mobile devices and embedded computers use it for messaging purposes.

Recently I came across the need for a messaging bus where we first came up with the implementation of our own messaging bus. This had taken about 5-7 milliseconds for end-to-end delivery of messages. Later on we came across MQTT and it took only one millisecond for end-to-end delivery of messages. It could also support websocket connections which helped us remove our own implementation of a websocket client from the project as well. FAQ page of MQTT would provide you with answers for most of the questions that would’ve risen in you by now!

Mosquitto – The messaging broker

The messaging broker we had used is Mosquitto which is an open source project from Eclipse that implements the MQTT protocols 3.1 and 3.1.1.

You could download the latest version of Mosquitto from here . Simply run the mosquitto executable from the downloaded folder and you’re good to go. You could enable verbose logging with the -v parameter and configurations could be loaded from a specific file through the usage of -c parameter. Read more about the configurations from here . Running the mosquitto executable would create a non-secure connection through the default port 1883 and secure encrypted connections could network connections and authentications could be established through SSL. Download the latest version of open SSL and copy the libeay32.dll, ssleay32.dll and libssl32.dll files to your mosquitto installation folder. Apart from that, download the pthreadvc2.dll and place that in the mosquitto installation folder as well.

In order to configure the server for certificate authentication, follow these steps and generate a certificate authority certificate and key, server key and a server certificate by creating a CSR and signing it with your CA key. Place the below entries into the configuration file and restart the mosquitto broker.


listener 8883
cafile certs/ca.crt
certfile certs/server.crt
keyfile certs/server.key
require_certificate true

Default port 8883 has been used in this scenario and setting the require_certificate to true would require the client to provide a valid certificate in order to establish the connection. This could be set to false if clients are not expected to be authenticated through their certificates.

Websocket support also needs to be explicitly enabled. This requires libwebsockets and a step by step instruction set on how to achieve this could be found here . You could also enable SSL authentication through websockets and a sample configuration would look like shown below.

listener 9002 127.0.0.1
protocol websockets
cafile certs/ca.crt
certfile certs/server.crt
keyfile certs/server.key
require_certificate true

That’s it and you have setup your mosquitto with additional websocket and SSL support!

MQTT could be the answer for any of your requirements of a lightweight messaging protocol even if it doesn’t involve IOT just like in my case! Hope this would’ve given an idea of what MQTT is and how to setup Eclipse’s Mosquitto broker. Soon I’ll follow this up with tutorials on create clients using Java and NodeJS. Happy messaging folks! 🙂