Skip to Content
Author's profile photo Frank Schuler

Analyse your Raspberry Pi I2C sensor data with the HANA Cloud Platform, predictive services

Inspired by Thierry BRUNET’s excellent blog series HANA Cloud Platform predictive services – Starter Kit, I got HCPps working on my HCP trial account.

In terms of what data to analyse, I decided for I2C temperature information from my Raspberry Pi, inspired by Murali Shanmugham’s excellent blog series Capture event streams from IoT devices and perform predictive analytics using HCP (but without the need for SDS) and similar to Verify your HANA Cloud Platform Internet of Things latency.

To make this work, there are 10 main steps:

  1. Fit an I2C sensor to your Raspberry PI. I chose a combination of a MCP9801 temperature sensor with a DS1337 real time clock module:
  2. Enable I2C on your Raspberry PI:
  3. Find the I2C address of your sensor. In my case the temperature sensor is at 0x4f and the real time clock module at 0x68:
  1. Send your sensor data to your HANA Cloud Platform, IoT service:
    import com.pi4j.io.i2c.I2CBus;
    import com.pi4j.io.i2c.I2CFactory;
    import com.pi4j.io.i2c.I2CDevice;
    import java.net.URL;
    import java.net.HttpURLConnection;
    import java.io.DataOutputStream;
    import java.io.InputStream;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    public class C4PA {
    	public static void main(String[] args) {
    		try {
    		  	I2CBus i2cBus = I2CFactory.getInstance(I2CBus.BUS_1);
    	    	I2CDevice mcp9801 = i2cBus.getDevice(/*YourI2CSensorAddress*/);
    		    byte[] buffer = new byte[2];
    		    HttpURLConnection con = null;
    		    URL url = new URL("https://iotmms/*YourHCPID*/.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/data//*YourDeviceID*/");
    		    con = (HttpURLConnection)url.openConnection();
    		    con.setRequestMethod("POST");
    		    con.setRequestProperty("authorization", "Bearer /*YourAuthCode*/");
    		    con.setRequestProperty("content-type", "application/json;charset=utf-8");
    		    con.setUseCaches(false);
    		    con.setDoOutput(true);
    		    DataOutputStream wr = new DataOutputStream (con.getOutputStream());
    		    int bytes = mcp9801.read(0, buffer, 0, 2);
    		    System.out.println("Temp: " + ((double) buffer[0] - (double) buffer[1] / 256));
    		    wr.writeBytes("{\"mode\":\"sync\",\"messageType\":\"/*YourMessageType*/\",\"messages\":[{\"temperature\":\"" + ((double) buffer[0] - (double) buffer[1] / 256) + "\"}]}");
    		    wr.close();
    		    InputStream is = con.getInputStream();
    		    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    		    StringBuilder response = new StringBuilder();
    		    String line;
    		    while((line = rd.readLine()) != null) {
    				response.append(line);
    				response.append('\r');
    		    }
    		    rd.close();
    		    System.out.println(response);
    		} catch (Exception e) {
    		    e.printStackTrace();
    		}
    	}
    }​

    I schedule a respective cron job:

    java -classpath .:classes:/opt/pi4j/lib/'*' C4PA

  1. Bind your iotmms Java application to the same HCP HANA MDC Data Source as your aac4paservices Java Application:

  2. Configure a sql Processing Service Mapping to store your IoT data in the schema that you set up for HCPps:

  3. Check that your IoT data is in fact stored in the schema that you set up for HCPps:

  4. Register your IoT data table with HCPps via the API Console:

  5. Run a query like that for the two most prominent temperature outliers:

  6. Examine the result, i.e. temperature 13 outliers of which 27.5 and 30.5 are the 2 most prominent ones:

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hello Frank,

      That's really a nice blog but I have a small query here. In my case I am pulling the data from IoT in HCI whenever it arrives and sending that to ECC for further actions. My current binding for IoT is default and I am using the OData API in HCI to read.

      Now if I am creating a new binding for Iotmms, it does not allow saying there's one default already exist. That means I need to delete the existing binding with default Iot schema and create a new one have the entries in predictive schema.

      Is there a way I can directly read/query the existing IoT table with predictive APIs without disturbing the existing bindings? I mean is it necessary for predictive service to have the entries in it's own schemas to perform the analytics?

      This will really help.

      Regards

      Amitabh