Skip to Content

We have already covered all the steps related to hardware setup and configuration.

We are going now to focus our attention on SAP HANA and interfacing it with our SAP UI5 frontend.

We are going to cover the SAP HANA setup in this partSAP HANA IoT Internet of Things (IoT): Raspberry, Arduino Uno, XSJS & SAPUI5

Steps which we are going to follow for configuring SAP HANA

Step1: Create the Data Definition for our HANA XS which will be storing the data.

Step2: Create the SQL procedures to do data operations.

Step3: Create the XSJS services to act as a Gateway between the SQL Procedures calls and the service calls for Raspberry Pi and SAPUI5 application.

Step1 – Create the Data Definition for HANA XS

We  are going to name it Demo Schema .hdbschema. Note here that demoApp.demo01.app02 is the package path and SHIOT_02 is the project name:

  1. namespace demoApp.demo01.app02.SHIOT_02.Data;
  2. @Schema: ‘DemoSchema’
  3. context demo02sensorNetwork {
  4. type sensor_key : String(10);
  5. @Schema: ‘DemoSchema’
  6. context demo02sensorNetwork {
  7. type sensor_key : String(10);
  8. @Catalog.tableType : #COLUMN
  9. Entity demo02sensor_info_MD {
  10. key ID: sensor_key;
  11. DESC: String(200);
  12. };
  13. @Catalog.tableType : #COLUMN
  14. Entity demo02sensor_active_TS {
  15. key ID: sensor_key;
  16. key time_stamp: UTCTimestamp;
  17. value : Integer;
  18. };

Once you have activate the code, you need to create two tables demo02sensor_active_TS anddemo02sensor_info_MD .demo02sensor_active_TS: Stores the transactional data of the sensors (readings with timestamp).demo02sensor_info_MD: Stores the Master Data of the sensors (Sensor Id’s)

Step2 – Create the SQL procedures to do data operations

We have to create two SQL procedures for data operations: insert_sensor_reading.hdbprocedure andsensor_read.hdbprocedure.As the names suggest, the  first one is going to insert sensor data in sensor table and second one is going to read the recent sensor data from the table.insert_sensor_reading.hdbprocedure is inside the folder Procedures. It is taking sensor ID and sensor Reading as input:PROCEDURE

  1. “DemoSchema”.“demoApp.demo01.app02.SHIOT_02.Procedures::insert_sensor_reading” (
  2. IN SENSORID NVARCHAR(10),
  3. IN sensor_reading INTEGER )
  4. LANGUAGE SQLSCRIPT AS
  5. BEGIN
  6. /*****************************
  7. Inserting sensor Data
  8. *****************************/
  9. insert into “demoApp.demo01.app02.SHIOT_02.Data::demos02sensorNetwork.demo02sensor_active_TS”
  10. VALUES(:SENSORID, CURRENT_TIMESTAMP , sensor_reading
  11. );
  12. END;

sensor_read.hdbprocedure is inside the folder Procedures. It is taking sensor ID as input and returns one data set ofdemo02sensor_active_TS.PROCEDURE

  1. “DemoSchema”.“demoApp.demo01.app02.SHIOT_02.Procedures::sensor_read(
  2. IN id NVARCHAR(10),
  3. OUT result “DemoSchema“.”demoApp.demo01.app02.SHIOT_02.Data::demo02sensorNetwork.demo02sensor_active_TS“)
  4. LANGUAGE SQLSCRIPT
  5. SQL SECURITY INVOKER
  6. –DEFAULT SCHEMA
  7. READS SQL DATA <u>AS</u>
  8. BEGIN
  9. /*****************************
  10. Reading sensors Data
  11. *****************************/
  12. result = select *
  13. from “DemoSchema“.”demoApp.demo01.app02.SHIOT_02.Data::demo02sensorNetwork.demo02sensor_active_TS
  14. where “ID” = :id and “time_stamp” = (select max(“time_stamp“) from
  15. DemoSchema“.”demoApp.demo01.app02.SHIOT_02.Data::demo02sensorNetwork.demo02sensor_active_TS
  16. where “ID”= :id);
  17. END;

Step3: Create the XSJS services

Create the XSJS services to act as a Gateway between the SQL Procedures calls and the service calls for Raspberry Pi and SAP UI5 application:We have two services.The first one is getSensorReading.xsjs which reads the recent sensor reading for the sensor id, passed in the URL. It uses sensor_read.hdbprocedure for database call.

  1. var sensorId = $.request.parameters.get(“id”);
  2. var body = “error”;
  3. var data ={
  4. “id”:“error”,
  5. “timestamp”:“error”,
  6. “value”:0
  7. };
  8. body = sensorId;
  9. if(sensorId === undefined){
  10. $.response.setBody( “Invalid key !!!”);
  11. }
  12. else{
  13. $.response.contentType = “text/plain”;
  14. $.response.setBody(sensorId);
  15. try {
  16. var conn = $.db.getConnection();
  17. var query = ‘call \”demoApp.demo01.app02.SHIOT_02.Procedures::sensor_read\”(?,?)’;
  18. var cst = conn.prepareCall(query);
  19. cst.setString(1, sensorId);
  20. var rs = cst.execute();
  21. conn.commit();
  22. rs = cst.getResultSet();
  23. while(rs.next()){
  24. data.id = rs.getNString(1);
  25. data.timestamp= rs.getTimestamp(2) ;
  26. data.value= rs.getInteger(3);
  27. }
  28. body = JSON.stringify(data);
  29. conn.close();
  30. } catch (e) {
  31. body = e.stack + “\nName:”+ e.name+“\nMsg” + e.message;
  32. $.response.status = $.net.http.BAD_REQUEST;
  33. }
  34. }
  35. $.response.contentType = “text/plain”;
  36. $.response.setBody(body);

Then after that, we use putSensorReading.xsjs which reads the recent sensor reading for the sensor id and sensor value passed in the URL. It uses insert_sensor_reading.hdbprocedure for database call to store the data.


  1. var sensorId = $.request.parameters.get(“id”);
  2. var sensorReading = $.request.parameters.get(“value”);
  3. sensorReading = parseInt(sensorReading,10);
  4. var body = “error”;
  5. if(sensorId === undefined){
  6. $.response.setBody( “Invalid key !!!”);
  7. }
  8. else{
  9. $.response.contentType = “text/plain”;
  10. $.response.setBody(sensorId);
  11. try {
  12. var conn = $.db.getConnection();
  13. var query = ‘call \”demoApp.demo01.app02.SHIOT_02.Procedures::insert_sensor_reading\”(?,?)’;
  14. var cst = conn.prepareCall(query);
  15. cst.setString(1, sensorId);
  16. cst.setInteger(2, sensorReading);
  17. var rs = cst.execute();
  18. conn.commit();
  19. //as no record returned
  20. if(rs == 0){
  21. body = true;
  22. }
  23. conn.close();
  24. } catch (e) {
  25. body = e.stack + “\nName:”+ e.name+“\nMsg” + e.message;
  26. $.response.status = $.net.http.BAD_REQUEST;
  27. }
  28. }
  29. $.response.contentType = “text/plain”;
  30. $.response.setBody(body);

In the real world setup we need also to assign the security key to each sensor and pass it to verify the readings.

Also it would be better to have the timestamp taken from the sensor source and store it in the database but for keeping the coding and complexity minimum we are focusing on the core steps.

In part 6, we are going to setup the SAPUI5 App and integrate it with the backend.

To report this post you need to login first.

Be the first to leave a comment

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

Leave a Reply