One of the "cool" features of Java ME Embedded 8 is the Device I/O (DIO) API. With DIO, you can write Java code to take advantage of the GPIO header on the Raspberry Pi, and connect LED's, switches, I2C and SPI devices, and serial devices that use a UART. This opens up the world of the Internet of Things to Java developers, and for less than $40 US!

Java SE, both 7 and 8 are also available for the Raspberry Pi (ARM 6 architecture), and if you want to explore Java SE Embedded, and play with profiles, you can do that too. But what about Java EE and Web Services? How do I access devices using servlets and web services?

The answer is by combining two technologies. Recently, the Device I/O API has been under development as an Open JDK project - creating a standalone way to access the GPIO header using exactly the same API that makes Java ME Embedded so special. This allows developers to write DIO code outside of a Java ME Embedded VM.

OpenJDK Device I/O is a third-party library which leverages standard Java ME Device I/O APIs to Java SE.

To learn more:

1 Install JDK 1.8

Ensure JDK 1.8 is installed

Copy
$ java -version
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) Client VM (build 25.65-b01, mixed mode)

If not installed you can install it by doing:

Copy
$ sudo apt-get update
$ sudo apt-get install oracle-java8-jdk

2 Install JDK IO

Next, download the JDK DIO developer code. This library has device providers for JDK Device I/O v1.0 (in Mercurial master repository) and v1.1 Mercurial (in dev repository).

  • Install mercurial
    Copy
    $ sudo apt-get -y install mercurial
  • Install zip
    Copy
    $ sudo apt-get install zip unzip
  • Create directory deviceio
    Copy
    $ mkdir deviceio
    $ cd deviceio
  • Clone JDK dio 1.0 (master repository) or 1.1 (dev repository)
    Copy
    $ hg clone http://hg.openjdk.java.net/dio/master/
    destination directory: master
    requesting all changes
    adding changesets
    adding manifests
    adding file changes
    added 276 changesets with 1579 changes to 314 files
    updating to branch default
    291 files updated, 0 files merged, 0 files removed, 0 files unresolved
  • Change to deviceio cloned directory master
    Copy
    $ cd master
    $ ls -l
    -rw-r--r-- 1 pi pi  2516 Oct 23 17:51 Javadoc.gmk
    -rw-r--r-- 1 pi pi 19264 Oct 23 17:51 LICENSE
    -rw-r--r-- 1 pi pi 11187 Oct 23 17:51 Makefile
    -rw-r--r-- 1 pi pi   940 Oct 23 17:51 README
    -rw-r--r-- 1 pi pi  1294 Oct 23 17:51 README_Tests
    drwxr-xr-x 2 pi pi  4096 Oct 23 17:51 config
    drwxr-xr-x 2 pi pi  4096 Oct 23 17:51 jrecreate
    drwxr-xr-x 4 pi pi  4096 Oct 23 17:51 samples
    drwxr-xr-x 5 pi pi  4096 Oct 23 17:51 src
    drwxr-xr-x 5 pi pi  4096 Oct 23 17:51 test
  • Determine where Java is installed
    Copy
    $ ls /usr/lib/jvm/
    jdk-8-oracle-arm32-vfp-hflt
  • Setup JAVA_HOME to point the installed java
    Copy
    $ export JAVA_HOME=/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt
    $ export PI_TOOLS=/usr
    $ make osgi
    Creating output directories
    Compiling Java source files to /home/pi/dio/master/build/jar/dio.jar
    Note: Some input files use or override a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    Note: Some input files use unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    Compiling src/se/native/com/oracle/dio/dio_common.cpp
    Compiling src/se/native/com/oracle/dio/dio_exceptions.cpp
    Compiling src/se/native/com/oracle/dio/uart/impl/jni_signal_dispatcher.cpp
    Compiling src/se/native/com/oracle/dio/uart/impl/jni_modem.cpp
    Compiling src/se/native/com/oracle/dio/uart/impl/jni_uart.cpp
    Compiling src/se/native/com/oracle/dio/i2cbus/impl/jni_i2c.cpp
    Compiling src/se/native/com/oracle/dio/gpio/impl/jni_gpio.cpp
    Compiling src/se/native/com/oracle/dio/dio_nio.cpp
    ...
  • Now copy the libraries in Raspberry PI
    Copy
    $ cp build/deviceio/lib/ext/dio.jar dio-1.0.jar
    $ sudo cp build/deviceio/lib/ext/dio.jar $JAVA_HOME/jre/lib/ext/.
    $ sudo cp build/deviceio/lib/arm/libdio.so $JAVA_HOME/jre/lib/arm/.
  • Trasnfer dio library to a workstation if you are using cross development. In the example we transfer from a pi located at address 192.168.1.47 to our local workstation:
    Copy
    $ scp pi@192.168.1.47:/home/pi/dio/master/dio-1.0.jar .