IoT demo: Run tracker with HCP, SAPUI5, IOT-MMS & Android [PART 1]
Last year, in October 2015, I built an technical demo using the then freshly launched IoT MMS on Hana Cloud Platform. Since then, I met quite a few people interested in the technical setup of this application. I finally found the time to create a document for future reference on this project. This article isn’t meant as a tutorial or step-by-step instruction guide, but merely aimed at those who have some (but not much) experience with HCP.
Since there is a lot to cover, I will split up the different parts of the application over different blog posts. I’ll try to keep the time between blog posts to a minimum.
So, the grand idea behind this was to create a run tracker that would use an Android phone to connect to a heartbeat sensor, and use the phone’s metric for other parameters, such as speed, location, activity etc. This info would then be sent to the HCP for further analysis. The ultimate goal would be to create a ‘coach’ app to monitor one’s physique and create an individual training plan for each user based on some predictive algorithm. Sounds a bit ambitious you say? Yes indeed, I am no data scientist nor a physician. Also, I’m convinced you need a lot more parameters to judge someone’s fitness. And… minor detail: we had 3 weeks to finish this, thus we were forced to drop some 😉 features…
We limited the scope to a simple app that allows someone to monitor a runner in real time while wearing a Polar heartbeat band and carrying an Android phone. While this isn’t really a spectacular concept it allows us to use a lot of the features of HCP. This solution isn’t far from perfect and could be further elaborated, look at it as a Proof of Concept.
Keep in mind we built this on a Trial Instance so we had to create some workarounds to bypass some of the trial limitations.
This is what the final SAPUI5 app looks like:
Let’s begin by explaining the architecture. On the side of the runner we have an Android phone connected to an heartbeat sensor gathering different metrics. These metrics have to end up somehow in the HCP’s database. Since we are using an Android phone which has the capabilities and horse power comparable to a normal pc we could use various approaches to do this: build a hybrid Fiori app or use SAP’s SDK for Android and leverage HCP Mobile Services. But, for the sake of using new SAP technology, we’re going to work with the Internet of Things Message Management Service. This service uses oauth and json to receive messages from “things”.
The IoT-MMS service is in fact a Java application with a SAPUI5 frontend. When deploying it for the first time it will create it’s own HANA schema. We will change the schema to HANA XS so we can access the data later on in an XS app.
In the XS app we will create the Odata/Rest services to provide our SAPUI5 app with the required data and avoid doing too much calculations on the client.
IoT MMS configuration
In this first part I will explain how the configuration of the IoT MMS on HCP is done.
First we’ll need to subscribe to the IoT MMS, you can do so under Services from your HCP trial main menu.
You can then click through to the service or find it under subscriptions.
But first I’ll change the database binding to a HANA XS DB, so we can later access the database schema from an XS app. If I remember correctly, you could change this afterwards as well
Under Databases & Schemas create a new schema:
Next create a binding with the iotmms java application:
To start the IoT cockpit go back to Subscriptions in the HCP main menu and click on the iotcockpit application under java applications, or you can go through Services as well.
When you launch the application url the cockpit shows up:
If it’s a fresh install no device types, message types or devices will be present. As you can see, I’ve been playing around a bit…
We need 3 message types for the Android device to communicate. The first message type will be a metrics message. Which contains gps location, speed, heartbeat etc. The second type is used for sending messages from and to the device, to the user. And the last one is used to mark the start of a new running session.
First we create a device type. This yields an ID and a registration token. You can use the registration token to automate device registrations using a REST API. In this demo we do the registrations manually, but in a productive environment this is far from user-friendly or secure. You could write a manual tool in SAPUI5, or create a automated tool in Node.js, Java or, for the heretics, even in .NET! As long as it talks REST.
Next I created the message types, these message types define the structure of the messages sent & received. They are linked to the Android device type. Although I think a message type should be able to be linked to multiple device types (imo this is a shortcoming of the IoT MMS) since I want communication between SAPUI5 & Android using the same message type…
Next I created the InboundSensorData message type:
All these messages are linked to the ‘Android’ device type.
For our first device we generate an id and token in de Devices pane.
Make sure you save the token somewhere for future use, you cannot retrieve it (but you can reset it), this token grants your device access to your IoT MMS
We now have everything set up to communicate between HCP and our device.
This concludes the first part! If you’d like to test connectivity you could use the built-in message client or use a REST tool like Advanced Rest Client (Google Chrome) or Postman. Don’t forget the Authentication header with the ‘Bearer ‘ + oauth token in order for it to work. The message client will tell you all about this…
Until next time!