Sunday, 21 February 2016

Eclipse Open IoT Challenge 2.0 - Update #8 : IOT VAIDYA CLOUD - INSIDE OUT

This is probably one of the last updates on our project and will shed light on the iotvaidya – cloud in detail.So in this particular article we will try to explain what was done on the cloud side and how we wired up IBM Bluemix and other services with our project.

Okay! So let's begin.

Using IBM Bluemix is quite easy.It takes only few steps to get things up and running.I have explained in detail how to create an IOTF service and use it to subscribe to the events from any device  here.In the article I have shown how can we use the credentials on the “device” side to publish or subscribe to the events. If you want to use the IOTF service to subscibe or publish as an “application” hosted on IBM Bluemix, you will require API access key and Authentication token like the one shown below:

We will be using above access key and token(token will be generated along with API key ; it is one time and can be viewed only once.) in our node server code to “subscibe” to any event being published by the device( our raspberry pi gateway).
Once we are ready with the IOTF service with both kinds of credentials i.e, the one we'll use in our “device” and the one we'll use in our nodejs “application”, we'll navigate to the CATALOG tab and choose “SDK for Node.js”  

You will have to name your application and hit CREATE.

The nodejs application created! This application will be a very basic starter applcation and you can view it by hitting the route. The route is the URL of your application. In our case it is “”

Now, we don't want a simple starter application.We want to modify it according to our needs so we'll navigate to “Start Coding”. There are many ways to customize and manage your application like Eclipse, CF Command line interface and GIT. We will choose CF (Cloud Foundary) Command line interface.For this purpose you'll have to install CF Command Line Interface on your local machine.Follow the steps shown on the page.It is very easy.Then download the starter application, unzip it, modify it and simply push it using CF commands.It is all given on this page.


Now that we have the starter application directory with us.Let us modify the contents and include necessary codes and modules to build backend and front end of IOT VAIDYA CLOUD, backend first.We will remove everything from the directory except,manifest.yml and package.json.
Create an app.js file.This is the main file in which we will write the logic and server code to manage our project.
Install following packages by using npm command “npm install package_name --save”

  • express
  • body-parser
  • fs
  • request
  • multer
  • mongoose
  • ibmiotf
  • path
  • sinch-auth
  • sinch-messaging
Include following lines in app.js after installing above packages.

Create host and port variables and assign them default and environment values as follows:

Listen on the assigned port.

This is pretty much what needs to be done for a basic server.Next, we'll add codes for giving features to our node server.Let's take IOTF service first.Remember the access key and token we were talking about, it will be used here to access IOTF service from IBM Bluemix.

Replace values in config variable according to the credentials you have. "Your device name" is the name of the device(Raspberry Pi) registered with IOTF service.Before writing the code to subscribe to the data, we will have to wire-up our server with mongodb database. We are using free database provided by Mongolab

Follow these steps to obtain the information required to connect to your MongoLab-hosted database(s):

  • Log in to the MongoLab management portal
  • Navigate to the MongoDB deployment that you wish to connect to
  • At the top of the screen, you will see a box with the connection information 

The first string is what you would use if you were to connect using the mongo shell and the second string which we'll be using is the standard connection URI string that MongoDB’s drivers and client libraries use.
Next, we create Schemas for doctor and patient data dSchema and pSchema respectively.Then we create doc model and patient model to save data. Now that we've connected our server with mongodb, time has come to subscribe to any data being published and to save it in our database.

The above code subscribes to the "payload"  being published by IOT VAIDYA gateway built on Raspberry Pi.The payload structure is as shown in images above.It hase "name","age","contact","village","temp","pulse","ecg" and "gsr". Apart from this, the payload also has another key : value  pair which is "photo". We are not saving photo in the mongodb but we are saving it on local directory located at "/public/dist/img/patient/profile/" (stores profile pictures of patients) and "/public/dist/img/patient/symptom/" (stores symptom pictures of patients)

The pictures subscribed are base64 encoded. So, we write a code to convert base64 string into image as shown in the code snippet.

We have subscribed and saved the patient information in our database and local directory.Now we will write code to handle Doctor Registration and Login. The Registration and Login pages are form type and use POST method.We will use method to handle information posted from these pages.


Include above code in app.js. Multer adds a body object and a file or files object to the request object. The body object contains the values of the text fields of the form, the file or files object contains the files uploaded via the form. Basically we are using it to access individual data like name,email and password from registration page. The registration page posts the information to "/register"  route and this information is accessed through the request object "req". d is the doc model object and is used to save the textual information.Image is uploaded using upload() method from multer. More info on multer here.
The registration page looks like this:


Similarly, we write code to handle login information.Authentication is being done by simple comparing email id and password pair with the one in the database.

The value YOUR_API_KEY  can be found on this page : . After successful login, you will be redirected to '/entry?'+n where 'n' is the doctor's name you used to register.If the login is not successful, you will be redirected to the login page.
The log in page looks like this:

Okay, let's assume we have successfully logged into our account.The next page will show a table consisting of a list of patients from different regions.It looks as shown below :

This entry page may change in the next 2 days as we are working on including some extra feature to the website.

The patient data is retrieved from a jQuery Ajax call to the mongodb database as shown in the code snipped below.The response is parsed and traversed key by key to access each value. data_set is an array of arrays which stores rows of patient data. For drawing the table we have used jquery.dataTables.min.js. Following code snippet is written in entry.html inside <script></script> tags.

The html code for the table is as shown below:


Notice a small bell on the upper-right corner of the table page.The doctor will be notified whenever new patient will be added to the database.Currently it just shows number of new patients added since you last logged in.The logic is shown in following code snippet:

"test" is the collection name where patients are stored and "temp" is the collection where count of the patient is stored. If the count is greater in test than the value of "num" in temp, the notification div value is set to the difference of the two counts else, the bell shows "0".The internal ajax call uses PUT request to update the value of "num" in the temp collection.
The HTML code is as shown below:


Once you click on "Report", you are taken to report.html page which shows you the detailed report of the patient which includes ECG,Heart Rate,Temperature and Galvanic Skin Response. 
We will not explain the entire HTML and javascript code here as it will make this already long post too long.We have used few charting plugins and themes for making the UI the way it looks.The entire project is put up on our github.The report page is populated from the data received by making jquery ajax calls to our database.

Okay, let's pretend that Rakesh is a real doctor, and he thinks that Rahul is suffering from maleria.He can send this message and the nesessary prescription using the text editor under FEEDBACK tab.



Note how the feedback is posted on /message route.The feedback is handled using following code snippet in our app.js file.

msg stores the message text and is included in the t_url string which requests the translation of the included message using GET.This translation API is free and is provided by Next, we send the translated message to the registered patient.Messaging API is provided by Sinch. The reason we are not using Twilio from IBM Bluemix is that it sends a default header message with every SMS.We get a key and a secret to use the API.
Usage : var auth = sinchAuth("key", "secret"); 

So our patient Rahul will receive a text messages like the one shown below:

Translation:"Rahul you may be suffering from Maleria"

We are planning to add few more features to it to give it a final touch.These include Calendar and Google Maps.

To help doctors note down tasks to be done on a particular day

After finishing the project development on local machine, we have to push it on IBM Bluemix using CF Command line interface.Type following command in your terminal:
cf push iotvaidya -c "node app.js"

Through this update we showed you the cloud and website part of our project in an almost detailed fashion.Update on the Gateway and sensors part will follow in few days.After that we will publish a demo video of our project.Entire project is put up on our github page.So you can check out all the codes that I talked about in this blog post.Stay connected for next few really amazing updates on IOT VAIDYA.Get new updates on Tumblr as well.Follow us on Twitter.

No comments:

Post a Comment