Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member232287
Active Participant
0 Kudos

Introduction


In this blog post I will describe how you can forward IoT data from SAP Cloud Platform Internet of Things for the Cloud Foundry Environment to a custom endpoint on Cloud Foundry. IoT Services supports several kinds of message processing. You can find all of them here. For our purpose we'll use the HTTP forwarding. Of course Cloud Foundry is not the only platform to which you can forward your IoT messages.

When do you need this kind of processing?

In most of the cases you'll forward your IoT data to SAP Leonardo IoT. There the data is stored and you can do a lot of cool stuff, like operate rules on your data, analyse your data, build applications etc. But sometimes you might have scenarios where you don't want to pull the data but get it pushed to you. For example to forward the data using web sockets and enable live monitoring. Then you can use the processing features of IoT Services.

In the first part of the blog, I'll show you how to create and deploy the endpoint and in the second part we will create the HTTP forwarding.

Prerequisites:

 

Deploy the HTTP Endpoint


In this blog we will use Node.js to create our custom HTTP endpoint. I will not go into the depth of Node.js development here. Instead I'll just show a working example. There are a lot of really good Node.js tutorials out there.

First of all create a new directory with three files like this:



Let's first have a look at server.js. This file contains our JavaScript code to handle the incoming measurements. In our simple example we'll just print the incoming data. Therefore we create an Express.js server with a POST endpoint and Basic Authentication.
const express = require('express');
const basicAuth = require('express-basic-auth');
const app = express();

app.use(express.json());
app.use(basicAuth({
users: { "user": "password" }
}))

app.post('/measures', (req, res) => {
res.send('');
console.log(JSON.stringify(req.body));
})

app.listen(process.env.PORT || 3000, () => console.log(`Example app listening on port ${process.env.PORT || 3000}!`))

Next we have to define the dependencies in the package.json.
{
"name": "tutorial",
"version": "1.0.0",
"description": "",
"main": "server.js",
"dependencies": {
"express": "^4.16.4",
"express-basic-auth": "^1.2.0"
},
"scripts": {
"start": "node server.js"
},
"author": "Jan Reichert"
}

The app would now already work locally. But to deploy it to Cloud Foundry we need the manifest.yml. Here we describe how the app should be deployed on Cloud Foundry. Please change the name of your application as it has to be unique across SAP Cloud Platform Cloud Foundry.
applications:
- name: myiotendpoint
buildpacks:
- https://github.com/cloudfoundry/nodejs-buildpack#v1.6.31
memory: 128M
disk_quota: 128M
instances: 1

No we are ready to deploy our application. Open a terminal and log in to your Cloud Platform account using
cf login

Enter your e-mail and password and select your org and space. Afterwards you can deploy your application with
cf push

The last step is to read the logs so we can see the data which will be send to our custom endpoint.
cf logs myiotendpoint

 

Create the HTTP Forwarding


The HTTP forwarding is also configured using a JSON file. So the next step is to create a file called forwarding.json with the following content:
{
"processingServices": [
{
"name": "sql"
},
{
"name": "http",
"properties": {
"processing.http.url": "https://myiotendpoint.cfapps.eu10.hana.ondemand.com/measures",
"processing.http.headers": [
{
"name": "Content-Type",
"value": "application/json"
},
{
"name": "Authorization",
"value": "Basic dXNlcjpwYXNzd29yZA=="
}
]
}
}
]
}

Please be advised that the next step will restart the IoT Services Instance. That means that you'll have a data loss for a few minutes until the instance is restarted.

With
cf update-service NAME_OF_YOUR_IOT_INSTANCE -c PATH_TO_JSON_FILE

you can now upgrade the forwarding information of your IoT Services instance (documentation).

To test it, start sending data to any device within you IoT Services instance. You should see the messages in the logs of your Cloud Foundry application.
4 Comments