RPi and Java Embedded GPIO: Big Data and Java Technology

Posted by hinkmond on Oracle Blogs See other posts from Oracle Blogs or by hinkmond
Published on Thu, 13 Dec 2012 03:01:27 +0000 Indexed on 2012/12/13 5:11 UTC
Read the original article Hit count: 309

Filed under:
Java Embedded and Big Data go hand-in-hand, especially as demonstrated by prototyping on a Raspberry Pi to show how well the Java Embedded platform can perform on a small embedded device which then becomes the proof-of-concept for industrial controllers, medical equipment, networking gear or any type of sensor-connected device generating large amounts of data.

The key is a fast and reliable way to access that data using Java technology. In the previous blog posts you've seen the integration of a static electricity sensor and the Raspberry Pi through the GPIO port, then accessing that data through Java Embedded code. It's important to point out how this works and why it works well with Java code.

First, the version of Linux (Debian Wheezy/Raspian) that is found on the RPi has a very convenient way to access the GPIO ports through the use of Linux OS managed file handles. This is key in avoiding terrible and complex coding using register manipulation in C code, or having to program in a less elegant and clumsy procedural scripting language such as python. Instead, using Java Embedded, allows a fast way to access those GPIO ports through those same Linux file handles.

Java already has a very easy to program way to access file handles with a high degree of performance that matches direct access of those file handles with the Linux OS. Using the Java API java.io.FileWriter lets us open the same file handles that the Linux OS has for accessing the GPIO ports. Then, by first resetting the ports using the unexport and export file handles, we can initialize them for easy use in a Java app.

            // Open file handles to GPIO port unexport and export controls
            FileWriter unexportFile = 
                    new FileWriter("/sys/class/gpio/unexport");
            FileWriter exportFile = 
                    new FileWriter("/sys/class/gpio/export");
...
            // Reset the port
            unexportFile.write(gpioChannel);
            unexportFile.flush();
            
            // Set the port for use
            exportFile.write(gpioChannel);   
            exportFile.flush();

Then, another set of file handles can be used by the Java app to control the direction of the GPIO port by writing either "in" or "out" to the direction file handle.

                // Open file handle to input/output direction control of port
                FileWriter directionFile =
                    new FileWriter("/sys/class/gpio/gpio" + gpioChannel + 
                        "/direction");
            
                // Set port for input
                directionFile.write("in");  // Or, use "out" for output
                directionFile.flush();

And, finally, a RandomAccessFile handle can be used with a high degree of performance on par with native C code (only milliseconds to read in data and write out data) with low overhead (unlike python) to manipulate the data going in and out on the GPIO port, while the object-oriented nature of Java programming allows for an easy way to construct complex analytic software around that data access functionality to the external world.

               
      RandomAccessFile[] raf = new RandomAccessFile[GpioChannels.length];
...
      // Reset file seek pointer to read latest value of GPIO port
      raf[channum].seek(0);
      raf[channum].read(inBytes);
      inLine = new String(inBytes);

It's Big Data from sensors and industrial/medical/networking equipment meeting complex analytical software on a small constraint device (like a Linux/ARM RPi) where Java Embedded allows you to shine as an Embedded Device Software Designer.

© Oracle Blogs or respective owner

Related posts about /General Java