Skip to Content

As in my previous respective two blogs Connect a Lego Mindstorms NXT to the HCP Internet of Things Services via a Raspberry Pi over Bluetooth and Display your Lego Mindstorms sensor data on a Fiori Overview Page and deploy it to the HCP Portal Fiori Launchpad, IoT information is often stored in the HCP for statistical and analytical purposes for which any form of latency does not really matter.

For scenarios however, where based on the IoT data real-time decisions have to be taken, latency becomes very important. Therefore, I wanted to get a feeling for the latency with my HCP IoT services. To determine these, I came up with the following scenario:

POST.jpg

  1. I send IoT payload into the HCP with a timestamp.
  2. I receive the response back from HCP that all has been received successfully.
  3. I send another timestamp together with the original timestamp into the HCP.

For a reliable timestamp, I fitted my Raspberry Pi with a DS1307 compatible real-time clock module that also contains a temperature sensor so that I could get both the IoT payload as well as the timestamp from the same kit:

RTC Module.png

So, with a little Java program, I read the temperature sensor and send it into the HCP with a timestamp and on receipt of the response message, I take another timestamp and send that into the HCP as well together with the original timestamp for comparison:

/wp-content/uploads/2016/10/raspberrypi_1049115.png

In the HCP I can see two things:

  1. The timestamp has been adjusted from UTC that I used to send it to the GMT Summer Time setting of my HCP.
  2. The IoT record got posted only one second after I had sent it from my Raspberry Pi.

Temperature.png

Also, the acknowledgement control message got posted in the same second still:

/wp-content/uploads/2016/10/acknowledge_1049117.png

Therefore, the total latency for this scenario including network latency, processing in the HCP and processing on my Raspberry PI was about 1 second.

If you were interested in the Java code, e.g. how to read the Raspberry PI’s I2C bus this is it:

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 MCP9801 {

     public static String GetStamp(I2CDevice device) throws Exception {

          byte[] bcd = new byte[1];

          String stamp = “20”;

          int bytes = device.read(6, bcd, 0, 1);

          stamp = stamp + String.format(“%02d”, BCDToDecimal(bcd[0])) + “-“;

          bytes = device.read(5, bcd, 0, 1);

          stamp = stamp + String.format(“%02d”, BCDToDecimal(bcd[0])) + “-“;

          bytes = device.read(4, bcd, 0, 1);

          stamp = stamp + String.format(“%02d”, BCDToDecimal(bcd[0])) + “T”;

          bytes = device.read(2, bcd, 0, 1);

          stamp = stamp + String.format(“%02d”, BCDToDecimal(bcd[0])) + “:”;

          bytes = device.read(1, bcd, 0, 1);

          stamp = stamp + String.format(“%02d”, BCDToDecimal(bcd[0])) + “:”;

          bytes = device.read(0, bcd, 0, 1);

          stamp = stamp + String.format(“%02d”, BCDToDecimal(bcd[0])) + “Z”;

          return stamp;

     }

     public static long BCDToDecimal(byte bcd) {

          return Long.valueOf(BCDtoString(bcd));

     }

     public static String BCDtoString(byte bcd) {

          StringBuffer sb = new StringBuffer();

          byte high = (byte) (bcd & 0xf0);

          high >>>= (byte) 4;

          high = (byte) (high & 0x0f);

          byte low = (byte) (bcd & 0x0f);

          sb.append(high);

          sb.append(low);

          return sb.toString();

     }

     public static void main(String[] args) {

          try {

               I2CBus i2cBus = I2CFactory.getInstance(I2CBus.BUS_1);

               I2CDevice mcp9801 = i2cBus.getDevice(0x4f);

               I2CDevice ds1338 = i2cBus.getDevice(0x68);

               byte[] buffer = new byte[2];

               HttpURLConnection con = null;

               URL url = new URL(“https://iotmmssYourHCPIDtrial.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);

               String stamp = GetStamp(ds1338);

               System.out.println(stamp);

               wr.writeBytes(“{\”mode\”:\”sync\”,\”messageType\”:\”YourMessageType\”,\”messages\”:[{\”temperature\”:\”” + ((double) buffer[0] – (double) buffer[1] / 256) + “\”,\”timestamp\”:\”” + stamp + “\”}]}”);

               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);

               con = null;

               url = new URL(“https://iotmmssYourHCPIDtrial.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);

               wr = new DataOutputStream (con.getOutputStream());

               String ackn = GetStamp(ds1338);

               System.out.println(ackn);

               wr.writeBytes(“{\”mode\”:\”sync\”,\”messageType\”:\”YourMessageType\”,\”messages\”:[{\”timestamp\”:\”” + stamp + “\”,\”acknowledge\”:\”” + ackn + “\”}]}”);

               wr.close();

               is = con.getInputStream();

               rd = new BufferedReader(new InputStreamReader(is));

               response = new StringBuilder();

               while((line = rd.readLine()) != null) {

                    response.append(line);

                    response.append(‘\r’);

               }

               rd.close();

               System.out.println(response);

          } catch (Exception e) {

               e.printStackTrace();

          }

     }

}

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