Friday Dec 21, 2012

Quickie Guide Getting Hard-Float Enabled Java Embedded Running on Raspberry Pi

Here's the Quickie Guide in getting the hard-float version of JDK 8 for ARM Early Access running on your Raspberry Pi, and how to compile on the device!

1. Download the Win32DiskImager if you are on Windows, or if your are on a Mac/Linux PC skip this download and use dd instead:
  http://downloads.sourceforge.net/project/win32diskimager/Archive/win32diskimager-v0.7-binary.zip

2. Download the RPi Hard-Float Debian Wheezy image from here:
  http://downloads.raspberrypi.org/images/raspbian/2012-12-16-wheezy-raspbian/2012-12-16-wheezy-raspbian.zip

3. Insert a blank SD Card (NOTE: Only use a SD Card from this list) into your Windows or Mac/Linux PC.

4. Use Win32DiskImager on a Windows PC (or on Mac/Linux use "sudo dd if=<path_to_raspian.img> of=/dev/[diskNN|sdd] bs=[1m|4m]" or similar command see: dd ref) to burn the unzipped image from #2 to the SD Card.
(NOTE: After burning the SD Card, it will say the size is ~55MB (or something small like that). That's supposed to be that way and is OK, since the SD Card was imaged to have a swap area on it)

5. Insert the SD Card into your RPi. Connect an Ethernet cable from your RPi Ethernet port to your network. Connect the RPi Power Adapter. (NOTE: Make sure to use your RPi Power Adapter and do not use the USB port of a computer, since only the RPi Power Adapter will have enough current to drive your RPi and the SD Card)

6. The RPi will boot onto your network. Find its IP address by referring to your network DHCP settings or by using Windows Wireshark or the Mac/Linux tcpdump command:
  sudo tcpdump -vv -i[en0|en1|eth0|eth1] port 67 and port 68

7. Download JDK 8 for ARM Early Access (with Hard-Float support):
  http://jdk8.java.net/download.html
  NOTE: First click the "Accept License Agreement" button, then choose:
    Linux ARMv6/7 VFP, HardFP ABI "tar.gz" under the "JDK" column

8. scp the bundle from step #7 to your RPi:
   scp <jdk8-zip-bundle> pi@<ip_addr_rpi>:/home/pi
   <Password: "raspberry">

9. ssh to your RPi:
  ssh <ip_addr_rpi> -l pi
  <Password: "raspberry">

10. Untar the bundle from step #8 and rename (move) the toplevel directory to /usr/local/java
   cd /home/pi
   tar zxvf jdk-8-ea-b36e-linux-arm-hflt-29_nov_2012.tar.gz
   sudo mv jdk1.8.0 /usr/local/java
   export PATH=$PATH:/usr/local/java/bin
   java -version
     ...
11. Compile on your device!
   cd /home/pi
   cat > HelloWorld.java
   public class HelloWorld {
     public static void main(String args[]) {
       java.lang.System.out.println("Hello World!");
     }
   }
   ^D
   javac HelloWorld.java
   java -cp . HelloWorld

Yay! You have the JDK 8 for ARM Early Access (with hardware floating point support!!) on your RPi and can compile Java apps on your device!

Wednesday Dec 19, 2012

RPi and Java Embedded: Hard-Float Support is Here!!!

You wanted Java Embedded with Hardware Floating Point support to install on a default Raspian environment for your Raspberry Pi? Well, you just got your wish. Merry Christmas!

See:

JDK 8 Early Access for ARM w/Hard-Float

Here's a quote:

 The JDK 8 Early Access Release 
 for ARM including JavaFX (JDK 8) on 
 Linux has been made available at 
 http://jdk8.java.net. The Early 
 Access is provided to the community 
 to get feedback on the ongoing progress 
 of the project. Developers can start 
 developing applications using this 
 build of JDK 8 on an ARM device, 
 such as the a Raspberry Pi.
It's a regular JDK (JDK 8 Early Access) for your Raspberry Pi, so you should note this means there is a javac (and the other typical JDK tools) available to compile your Java apps right there on the device! Woot!

I'll cover step-by-step instructions how to do that in a future blog post. Stay tuned...

Monday Dec 17, 2012

Forbes Article on Big Data and Java Embedded Technology

Whoa, cool! Forbes magazine has an online article about what I've been blogging about all this time: Big Data and Java Embedded Technology, tying it all together with a big bow, connecting small devices to the data center.

See:

Billions of Java Embedded Devices

Here's a quote:

 By the end of the decade we could see 
 tens of billions of new Internet-connected 
 devices... with billions of Internet-
 connected devices generating Big Data, 
 are the next big thing.
 ...
 That’s why Oracle has put together an 
 ecosystem of solutions for this new, Big 
 Data-oriented device-to-data center world: 
 secure, powerful, and adaptable embedded 
 Java for intelligent devices, integrated 
 middleware...
This is the next big thing. Java SE Embedded Technology is something to watch for in the new year. Start developing for it now to get a head-start...

Wednesday Dec 12, 2012

RPi and Java Embedded GPIO: Big Data and Java Technology

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
            File exportFileCheck = new File("/sys/class/gpio/gpio"+gpioChannel);
            if (exportFileCheck.exists()) {
                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 constrained device (like a Linux/ARM RPi) where Java Embedded allows you to shine as an Embedded Device Software Designer.

Thursday Dec 06, 2012

RPi and Java Embedded GPIO: Sensor Reading using Java Code

And, now to program the Java code for reading the fancy-schmancy static electricity sensor connected to your Raspberry Pi, here is the source code we'll use:




First, we need to initialize ourselves...

/*
 * Java Embedded Raspberry Pi GPIO Input app
 */
package jerpigpioinput;

import java.io.FileWriter;
import java.io.File;
import java.io.RandomAccessFile;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *
 * @author hinkmond
 */
public class JerpiGPIOInput {
    
    static final String GPIO_IN = "in";
    
    // Add which GPIO ports to read here
    static String[] GpioChannels = 
    { "25" };
       
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        
        try {
            
            /*** Init GPIO port(s) for input ***/
            
            // 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");

            for (String gpioChannel : GpioChannels) {
                System.out.println(gpioChannel);
    
                // Reset the port
                File exportFileCheck = new File("/sys/class/gpio/gpio"+
                    gpioChannel);
                if (exportFileCheck.exists()) {
                    unexportFile.write(gpioChannel);
                    unexportFile.flush();
                }
            
            
                // Set the port for use
                exportFile.write(gpioChannel);   
                exportFile.flush();

                // 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(GPIO_IN);
                directionFile.flush();
            }   

And, next we will open up a RandomAccessFile pointer to the GPIO port.

            /*** Read data from each GPIO port ***/
            RandomAccessFile[] raf = new RandomAccessFile[GpioChannels.length];
            
            int sleepPeriod = 10;
            final int MAXBUF = 256;
            
            byte[] inBytes = new byte[MAXBUF]; 
            String inLine;
            
            int zeroCounter = 0;
            
            // Get current timestamp with Calendar()
            Calendar cal;
            DateFormat dateFormat = 
                    new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
            String dateStr;
            
            // Open RandomAccessFile handle to each GPIO port
            for (int channum=0; channum < raf.length; channum++) {
                raf[channum] = new RandomAccessFile("/sys/class/gpio/gpio"
                        + GpioChannels[channum] + "/value", "r");
            }

Then, loop forever to read in the values to the console.

            // Loop forever
            while (true) {
                
                // Get current timestamp for latest event
                cal = Calendar.getInstance();
                dateStr = dateFormat.format(cal.getTime());
        
                // Use RandomAccessFile handle to read in GPIO port value
                for (int channum=0; channum < raf.length; channum++) {
                    
                    // Reset file seek pointer to read latest value of GPIO port
                    raf[channum].seek(0);
                    raf[channum].read(inBytes);
                    inLine = new String(inBytes);
                    
                    // Check if any value was read
                    if (inLine != null) {
                        
                        // Compress 0 values so we don't see too many 
                        //   unimportant lines
                        if (inLine.startsWith("0")) {
                            if (zeroCounter < 1000) {
                                zeroCounter++;
                            } else {
                                System.out.print(dateStr + ": " + inLine);
                                zeroCounter = 0;
                            }
                        } else {
                            // Else, specially mark value non-zero value
                            System.out.print("***" + dateStr + ": " + inLine);
                            zeroCounter = 0;
                        }
                    }

                    // Wait for a while
                    java.lang.Thread.sleep(sleepPeriod);
        
                }
            }

        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

Rinse, lather, and repeat...

Compile this Java code on your host PC or Mac with javac from the JDK. Copy over the JAR or class file to your Raspberry Pi, "sudo -i" to become root, then start up this Java app in a shell on your RPi.

That's it! You should see a "1" value get logged each time you bring a statically charged item (like a balloon you rub on the cat) near the antenna of the sensor. There you go. You've just seen how Java Embedded technology on the Raspberry Pi is an easy way to access sensors.

About

Hinkmond Wong's blog on making Machine to Machine (M2M) and the Incredible Internet of Things (IoT) smarter with Java Embedded Technologies

Search

Archives
« December 2012 »
SunMonTueWedThuFriSat
      
1
2
3
4
5
7
8
9
10
11
13
14
15
16
18
20
22
23
24
25
26
27
28
29
30
31
     
Today