SAP HANA IoT Internet of Things: Raspberry, Arduino Uno, XSJS & SAPUI5

Now we are going to perform:

Step 3 – Using Java in Raspberry Pi in order to read serial data of Arduino

This part is the most challenging part for someone who is new to Raspberry Pi, so we have decided to create the entire setup in the utmost simplistic way in 5 sub-steps:

Step 1: Install java in the Raspberry Pi.

Usually Raspbian comes with Java installed but you can still check it via the following command:

java -version

When you update your Raspbian, it will also update the Java library, if required.

sudo apt-get update

Step 2: The communication is in serial mode, between Arduino and Raspberry Pi, so we need a way to make our Java program to understand it. So Download the below files :

  • libjawt.so from here
  • librxtxSerial.so from here
  • RXTXcomm.jar from here

and go to the download folder of the files and type below commands:


sudo cp libjawt.so /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/lib/arm
sudo cp RXTXcomm.jar /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/lib
sudo cp librxtxSerial.so /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/lib/arm
sudo cp librxtxSerial.so /usr/lib/jni/              



Now, we are assuming here that you have your jdk-8-oracle-arm-vfp-hflt inside your /usr/lib/jvm. If you don’t, then your Java won’t be updated to jdk-8 and you have to update the JDK and JRE .sudo apt-get install oracle-java8-installerStep 3: Now you can copy below Java code.


  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.OutputStream;
  4. import gnu.io.CommPortIdentifier;
  5. import gnu.io.SerialPort;
  6. import gnu.io.SerialPortEvent;
  7. import gnu.io.SerialPortEventListener;
  8. import java.util.Enumeration;
  9. import java.net.HttpURLConnection;
  10. import java.net.URL;
  11. public class SerialTestGET implements SerialPortEventListener {
  12. SerialPort serialPort;
  13. static int sensorValue = 0;
  14. /** The port we’re normally going to use. */
  15. private static final String PORT_NAMES[] = {
  16. “/dev/tty.usbserial-A9007UX1”, // Mac OS X
  17. “/dev/ttyACM0”, // Raspberry Pi
  18. “/dev/ttyUSB0”, // Linux
  19. “COM3”, // Windows
  20. };
  21. /**
  22. * A BufferedReader which will be fed by a InputStreamReader
  23. * converting the bytes into characters
  24. * making the displayed results codepage independent
  25. */
  26. private BufferedReader input;
  27. /** The output stream to the port */
  28. private OutputStream output;
  29. /** Milliseconds to block while waiting for port open */
  30. private static final int TIME_OUT = 2000;
  31. /** Default bits per second for COM port. */
  32. private static final int DATA_RATE = 9600;
  33. public void initialize() {
  34. // the next line is for Raspberry Pi and
  35. // gets us into the while loop and was suggested here was suggested http://www.raspberrypi.org/phpBB3/viewtopic.php?f=81&t=32186
  36. System.setProperty(“gnu.io.rxtx.SerialPorts”, “/dev/ttyACM0”);
  37. CommPortIdentifier portId = null;
  38. Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
  39. //First, Find an instance of serial port as set in PORT_NAMES.
  40. while (portEnum.hasMoreElements()) {
  41. CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
  42. for (String portName : PORT_NAMES) {
  43. if (currPortId.getName().equals(portName)) {
  44. portId = currPortId;
  45. break;
  46. }
  47. }
  48. }
  49. if (portId == null) {
  50. System.out.println(“Could not find COM port.”);
  51. return;
  52. }
  53. try {
  54. // open serial port, and use class name for the appName.
  55. serialPort = (SerialPort) portId.open(this.getClass().getName(),
  56. TIME_OUT);
  57. // set port parameters
  58. serialPort.setSerialPortParams(DATA_RATE,
  59. SerialPort.DATABITS_8,
  60. SerialPort.STOPBITS_1,
  61. SerialPort.PARITY_NONE);
  62. // open the streams
  63. input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
  64. output = serialPort.getOutputStream();
  65. // add event listeners
  66. serialPort.addEventListener(this);
  67. serialPort.notifyOnDataAvailable(true);
  68. } catch (Exception e) {
  69. System.err.println(e.toString());
  70. }
  71. }
  72. /**
  73. * This should be called when you stop using the port.
  74. * This will prevent port locking on platforms like Linux.
  75. */
  76. public synchronized void close() {
  77. if (serialPort != null) {
  78. serialPort.removeEventListener();
  79. serialPort.close();
  80. }
  81. }
  82. /**
  83. * Handle an event on the serial port. Read the data and print it.
  84. */
  85. public synchronized void serialEvent(SerialPortEvent oEvent) {
  86. if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
  87. try {
  88. String inputLine=input.readLine();
  89. //System.out.println(inputLine);
  90. sendGet(inputLine);
  91. } catch (Exception e) {
  92. System.err.println(e.toString());
  93. serialPort.removeEventListener();
  94. serialPort.close();
  95. }
  96. }
  97. // Ignore all the other eventTypes, but you should consider the other ones.
  98. }
  99. // HTTP GET request
  100. public void sendGet(String inputLine) throws Exception {
  101. try{
  102. //if difference is more than 3 then send data to SAP HANA
  103. if(inputLine != null && inputLine .length() > 0 && Math.abs(sensorValue – Integer.parseInt(inputLine)) > 3 ){
  104. sensorValue = Integer.parseInt(inputLine);
  105. //Considering that A001 sensor is connection with this raspberry pie for now
  106. //we can even pass this with command line but for simplicityhardcoding it
  107. //Replace with your HANA server URL and port number
  108. String url = http:///demoApp/demo01/app01/services/putSensorReading.xsjs?id=A001&value=;
  109. url = url + inputLine;
  110. URL obj = new URL(url);
  111. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  112. // optional default is GET
  113. con.setRequestMethod(“GET”);
  114. //add request header
  115. //con.setRequestProperty(“User-Agent”, USER_AGENT);
  116. int responseCode = con.getResponseCode();
  117. if(responseCode == 200){
  118. System.out.println(“OK value:”+inputLine);
  119. }else{
  120. System.out.println(“Error: Response code “+responseCode);
  121. }
  122. }
  123. System.out.println(“OK value: Less than 3”);
  124. }catch (Exception e) {
  125. System.err.println(e.toString());
  126. serialPort.removeEventListener();
  127. serialPort.close();
  128. }
  129. }
  130. public static void main(String[] args) throws Exception {
  131. SerialTestGET main = new SerialTestGET();
  132. main.initialize();
  133. Thread t=new Thread() {
  134. public void run() {
  135. //the following line will keep this app alive for 1000 seconds,
  136. //waiting for events to occur and responding to them (printing incoming messages to console).
  137. try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
  138. }
  139. };
  140. t.start();
  141. System.out.println(“Started”);
  142. }
  143. }

Step 4: Compile it using below command:


javac -source 1.6 -target 1.6 -cp /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/lib/RXTXcomm.jar SerialTestGET.java

Step 5: If there is no error (warnings may come), then your setup is right and you can now read the Arduino data in your serial port of Raspberry Pi, with the step 4 Java compiled code.

You can change the Java code of Step 3 and recompile it, in order to fit your requirements as well.

In the part 5, we will see how to configure the SAP HANA system and SAPUI5 application.

To report this post you need to login first.

7 Comments

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

  1. Benjamin Pfahl

    Hi, i tried to follow this step, but something iam missing… i compiled the source code u gave, and i get this message:

    pi@raspberrypi ~/Desktop/JAVA $ javac -source 1.6 -target 1.6 -cp /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/lib/RXTXcomm.jar SerialTestGET.java

    warning: [options] bootstrap class path not set in conjunction with -source 1.6

    1 warning

    The real problem is that i didn´t get a compiled file: there are now 3 files in my folder:

    SerialTestGET$1.class

    SerialTestGET.class

    SerialTestGET.java

    i use java version “1.8.0”

    pi@raspberrypi ~/Desktop/JAVA $ java -version

    java version “1.8.0”

    Java(TM) SE Runtime Environment (build 1.8.0-b132)

    Java HotSpot(TM) Client VM (build 25.0-b70, mixed mode)

    i copied the files u mentioned…

    what can bbe  the problem?

    i´d really like to get this to work

    (0) 
      1. Benjamin Pfahl

        Hi,

        i followed ur blog and stored all in /home/pi/Downloads…

        i compiled it as u mentioned… and there are now 6 files in the folder:

        SerialTestGET$1.class

        SerialTestGET.class

        SerialTestGET.java

        libjawt.so

        librxtxSerial.so

        RXTXcomm.jar

        if i want to run the programm

        java SerialTestGET

        i return an error

        Error: Could not find or load main class SerialTestGet

        i also tried with java -cp .  SerialTestGET     with the same error

        (0) 
  2. Abhay Agnihotri

    Hello Ajay,

       I am getting the following warning when i try to compile SerialTestGET.java file.


    “bootstrap class path not set in conjunction with -source 1.6”

    Because of this warning i am unable to run the code.

    (0) 
    1. Ajay Nayak

      Hello Abhay,

      Check your JAVA_HOME class path and see what version is that also verify if the compilation is happening with two different java compiler version.

      -Br

      Ajay

      (0) 
      1. Abhay Agnihotri

        Hello Ajay,

              I used 1.8 instead of 1.6 while compiling, now there is no warning but getting the below error.

        could not find or load main class SerialTestGET

        (0) 
        1. Abhay Agnihotri

          Hello Ajay,

               I solved the issue. I used the following command.

          javac -cp /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/lib/RXTXcomm.jar:. SerialTestGET.java

          java -Djava.library.path=/usr/lib/jni -cp /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/lib/RXTXcomm.jar:. SerialTestGET

          (0) 

Leave a Reply