Skip to Content
Technical Articles

Connecting to IoT devices using Azure IoT Hub and visualizing the data using SAP HANA Cloud & Cloud Analytics

In this blog post I would like to share my experience in setting up services for an IoT based scenario using SAP Cloud Platform and Microsoft Azure. The scenario which I am working on is aimed at providing real-time insights into data like temperature and humidity for monitoring food in transit. This is not an actual PoC which was done for a customer nor is probably the best approach to implement this scenario. I was experimenting with some of the new capabilities on both the platforms and still working on refining the approach.

To begin with, I have used a raspberry Pi with temperature and humidity sensor to capture the data and prepare it to be sent to the Azure IoT hub. There is a tutorial which is already available which shows you how to setup the Raspberry Pi and connect it with Azure IoT Hub.

I have followed the steps and created a device in Azure IoT Hub. To view the telemetry data which is being sent to this device, you can install Azure IoT Explorer and monitor the registered device.

If you want to quickly try and simulate a device sending data, you can also follow these steps in this quick start guide to have a program send a simulated telemetry data.

I have used Azure Functions to receive the sensor data from Azur IoT hub and pass that on to an OData API which is exposed from SAP HANA Cloud. All the sensor data will eventually reside in SAP HANA Cloud.

When defining a function, I have used the IoT Hub template and provided my “Event Hub connection” for the sensor data to be passed on to this function.

Within the function, I have written a small piece of code to send the sensor data payload to an OData API endpoint exposed from SAP HANA Cloud.

Here is the complete source code which I used to invoke the OData API passing a payload

var httpMod = require('https');

module.exports = function (context, IoTHubMessages) {
    context.log(`JavaScript eventhub trigger function called for message array: ${IoTHubMessages}`);

    var count = 0;
    var totalTemperature = 0.0;
    var totalHumidity = 0.0;
    var deviceId = "";

    IoTHubMessages.forEach(message => {

        count++;
        totalTemperature += message.temperature;
        context.log(`Processed message totalTemperature: ${totalTemperature}`);
        totalHumidity += message.humidity;
        context.log(`Processed message totalHumidity: ${totalHumidity}`);

    });

    var timeStamp = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
    var temp1 = totalTemperature/count;
    var humid1 = totalHumidity/count;
   

    const body = JSON.stringify({
        TIME_STAMP: timeStamp,
        TEMPERATURE: temp1,
        HUMIDITY: humid1
    })

    const options = {
        hostname: '<<OData Endpoint>>',
        port: 443,
        path: '/devices.xsodata/signals',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': body.length,
            'Accept': '*/*'
        }
    }

    var response = '';
    const customReq = httpMod.request(options, (res) => {
        context.log(`statusCode: ${res.statusCode}`)

        res.on('data', (d) => {
            response += d;
        })

        res.on('end', (d) => {
            context.res = {
                body: response
            }
            context.done();
        })
    })
    customReq.on('error', (error) => {
        context.log.error(error)
        context.done();
    })

    customReq.write(body);
    customReq.end();
};

On the SAP Cloud Platform side, I have created a HANA Cloud instance and also setup SAP WebIDE to create database artefacts like table/view etc.

In the below screen, you can see that I have created a Multi-target application (AzureIoT) in SAP WebIDE which consists of a database module “storage” and a nodejs Module “nodepp”.

I have created a table using .hdbtable as show below.

COLUMN TABLE "devices" (
	 "TIME_STAMP" NVARCHAR(25),
	 "TEMPERATURE" DECIMAL(20,3)  COMMENT 'Temperature',
	 "HUMIDITY" DECIMAL(20,3)  COMMENT 'Humidity')

This table has been exposed as an OData service in devices.xsodata file.

service {
	"devices"
	  as "signals";
}

I have also exposed the table as a view as this would be consumed using SAP Analytics Cloud.

Once, the project has been built and deployed, all the releavant apps and services will be shown in the subaccount. In the below screenshot, you can see the nodeapp which represents my OData service

I can use the endpoint provided by this app and start to interact with the underlying table using the OData service.

Since these database artefacts are within a HDI container,  I had to ensure that the user which is used to connect from SAP Analytics Cloud has the relevant roles. Look for the “access role” in the HANA Cockpit.

The connectivity between SAP Analytics Cloud and SAP HANA Cloud has been simplified and I used use the step documented in this blog post. As you can see below, the view has been exposed as chart to visualize the temperate and humidity data from sensors.

I hope this blog post gave you some ideas on ow you could leverage some of the services across both the platforms to address certain scenarios. If you do have any questions, I would encourage you to post a question in the relevant forums.

 

1 Comment
You must be Logged on to comment or reply to a post.