Monday, May 5, 2014

Java 8 on Intel Galileo

Aloha,

Last week my Intel Galileo board arrived and last weekend I've started to play around with it. The target was to put Java on the Galileo (what else) and see what is possible. So the first thing I did was downloading and installing a Debian Wheezy Linux on a micro sd card. You can find different preconfigured distributions that have ssh server etc. already setup (I used this one). 
So after the Galileo booted in Debian I connected via ssh and set up the Linux in the same way I used to setup the Raspberry Pi. 
So usually I set up a new user, add him to the sudoers group, install mingetty to enable autologin and setup avahi daemon to be able to connect to the board from the Finder in OS X.
After that was done I've simply downloaded the appropriate JDK8 version for the Galileo platform (jdk-8u5-linux-i586.tar.gz).
The installation of JDK8 is the same as on the Pi, so simply copy the tar file to the Galileo and execute the following commands on the command line:

mkdir -p /opt
sudo tar zxvf jdk-8u5-linux-i586.tar.gz -C /opt
rm jdk-8u5-linux-i586.tar.gz

With this commands you install JDK8 in /opt/jdk1.8.0_05.
As next step I've setup JAVA_HOME as follows

sudo nano /etc/bash.bashrc

add the following two lines to the end of the file

export JAVA_HOME=/opt/jdk1.8.0_05
export PATH=$JAVA_HOME/bin:$PATH

Save the file with CTRL+O
The next time you login on the Galileo you should be able to type

java -version

on the command line and you should see something like this

java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05_b13)
Java HotSpot(TM) Client VM (build 25.5-b02, mixed mode)

Now that I had Java 8 on the Galileo the question was how to make use of the GPIO and Analog ports of the Galileo. Because there is no direct way in Java to access the IO-ports of the Galileo board (because there is nothing like Pi4J for the Galileo) you have to use a little workaround to address the IO-ports.
Fortunately there is a nice website that explains how to address the IO-ports of the Galileo board from the Linux console. The approach is to use the file system to address the IO-ports. Well addressing the file system is very easy to do in Java and so I wrote a little library that enables me to address the ports on the Intel Galileo from Java.
At the moment this is just something to play with and because it's based on the file system approach it's not really something you would like to use on the regular basis but he...it works :)
My idea was to use the IO-ports of the Galileo with the names of the Arduino shield connector. Means we have 6 analog ports (A0 - A5) and 13 digital ports (D0 - D13) that can be used. I just made two little tests with D13 and A0 and both worked but I can't guarantee that everything works as it should...so be careful.
At the moment you can set and read the digital pins D0..D13 and you can read the analog pins A0...A5. There is no support for pulse-width modulation (pwm) at the moment but I'll maybe add it in the near future.

Because I had a spare TMP36 analog temperature sensor lying around I've connected it to the 5V and GND pins of the Intel Galileo and the data pin I've connected to A0. With my GalileoIO library the following code is needed to read out the value at A0 and print it on the console.


// Read analog value from A0 with TMP36 connected
public class Main {
  public Main() {
    // Instantiate the GalileoIO library
    final GalileoIO galileoIO = new GalileoIO();            
        
    // Read out the voltage at A0 in millivolt
    double voltage = galileoIO.getAnalog(Analog.A0);
        
    // Calculate the temperature from the voltage of the TMP36
    double temp = (voltage - 500d) / 10d;        
        
    // Print the temperature on the console
    PrintStream out = new PrintStream(System.out, true, "UTF-8");
    out.println("Temperature: "
                temp + 
                "\u00B0C ("
                voltage + 
                " [mV])");                
    System.exit(0);
  }
   
  public static void main(String[] args) {
    new Main();
  }

}

Another example would be to set a digital pin to high and the code you need to do this is as follows

// Set D13 to high
public class Main {
  public Main() {
    // Instantiate the GalileoIO library
    final GalileoIO galileoIO = new GalileoIO();            
        
    // Set D13 to high
    galileoIO.setDigital(Digital.D13, Pin.Value.HIGH);
        
    // Wait for 3 seconds
    try {
      Thread.sleep(3000);
    } catch (InterruptedException exception) {}    
    
    // Set D13 to low
    galileoIO.setDigital(Digital.D13, Pin.Value.LOW);

    System.exit(0);
  }
   
  public static void main(String[] args) {
    new Main();
  }

}

To make sure the IO-ports will be unexported again I've added a shutdown hook to the GalileoIO library that will unexport all used ports when the JVM shuts down.

If you are interested in the code feel free to fork it on BitBucket.

That's it for today...so keep coding...

1 comment:

  1. I'm very glad to see the work you've done at putting Java to work on Intel Galileo, plus your library GalileoIO, amazing!
    As soon as possible, I'll publish an example of your work at Embarcados.com.br.
    Thanks, Andre Curvello.

    ReplyDelete