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! See this tutorial series on Java ME Embedded 8 for more info on using the Raspberry Pi as development platform for the Internet of Things.
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.
The second technology is Java Embedded Suite 7.0. JES 7.0 is designed to run on ARM 6 and 7 environments and comes with a small footprint application server (Glassfish for Embedded Suite Application Server), Jersey RESTful Web Services Framework and Java DB Embedded database. GlassFish for Embedded Suite supports servlets 3.0, web services and bean validation.
Putting the two together, you can write a servlet or web service that controls and communicates with devices. In this post, I'll show you how to set up the JES 7.0 with DIO to turn an LED on and off using a servlet running on Oracle Java Embedded Suite on a Raspberry Pi.
Part 1: Install JDK 8 (if not already installed)
Start by installing JDK 8. If you are working with Raspbian Debian Wheezy (September 2014), then you already have JDK 8 installed. Otherwise, you can download the JDK 8 for ARM as a tar gzip file, and extract into /opt. Then install the compiler and JVM with these two commands:
Alternatively, you can install the JDK using these commands:
sudo apt-get updatesudo apt-get install oracle-java8-jdk
sudo chmod +s /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/bin/java
Make sure that JDK 8 is your default version by typing:
java -version
If you see that the default version is still JDK 7, you can change that with these two commands:
sudo update-alternatives --config javac
sudo update-alternatives --config java
Part 2: Install JDK DIO
Next, download the JDK DIO developer code. While the JDK DIO team recommends cross-compiling the code on a Linux 86 platform, I have found it easier just to compile it directly on the Raspberry Pi. Download the current dev build as a zip file and unzip it on the Pi. Then cd to the directory (on the RPi) where you unzipped and set PI_TOOLS to /usr and run the Makefile:
export PI_TOOLS=/usr
make
The important artifacts are the native libraries (build/so/libido.so) and the jar file (build/jar/dio.jar).
Copy the dio.jar file to your development machine (I used psftp to ftp it over).
Part 3: Install JES 7.0 and Test
Next step is to download Oracle Java Embedded Suite 7.0 ARMv6/7 Linux zip file to your development machine. Unzip the zip file and by default it will create a jes7.0 directory. You will need the glassfish-jes.jar file from the glassfish/lib directory when you write code. The zip file contains a jre directory. You can delete this, since you are going to use your JDK instead. Then zip up the remaining files and directories and download the zip file to your Raspberry Pi (again, I used psftp, but you can use another ftp solution, such as vsftpd.)
Unzip the JES file on your Raspberry Pi and cd to the samples/dist/run directory.
Edit the config.sh file and modify JAVA_COMMAND:
JAVA_COMMAND="java -Xmx64m"
Then try running one of the samples, using the following line:
sudo ./gfhost.sh ../helloservlet.war
After a few seconds, your should see these lines:
Deploying ../helloservlet.war ...
Press <Enter> to exit server.
In a browser, you should be able to enter the IP address of your Pi and helloservlet and see a response. Now it is time to write our own servlet that communicates with DIO.
Part 4: Write a servlet in NetBeans (or your favorite IDE)
The samples provided with JES 7.0 are built as NetBeans projects, so the easiest thing is to make a copy of the helloservlet project from the samples directory on your development machine and modify that.
First, add the dio.jar file to the Libraries. Add a class called GPIOLED to the com.oracle.jes.samples.helloservlet package. The code for this class is here.
In the HelloServlet class, modify the doGet method:
try (GPIOLED led = new GPIOLED(23)) {
led.blink(10);
}
Build the project and ftp the helloservlet.war file to the jes7.0/samples/dist directory on your Raspberry Pi.
Step 5: Create a DIO java.policy file in the DIO dev directory
The java.policy file is used by DIO to determine what device permissions are required by the application. For this simple application, we just need to grant two permissions:
grant {
permission jdk.dio.gpio.GPIOPinPermission "*:*";
permission jdk.dio.DeviceMgmtPermission "*:*", "open";
};
Step 6: Modify the JES run command:
Modify the config.sh script (the one from step 3) and add the following lines:
DIO_JAR=/home/pi/<dio-dev_dir>/build/jar/dio.jar
DIO_LIB=/home/pi/<dio-dev_dir>/build/so
DIO_POLICY=/home/pi/<dio-dev_dir>/java.policy
DIO_REG=/home/pi/<dio-dev_dir>/config/dio.properties-raspberryp
Be sure to replace <dio-dev_dir> with your DIO dev build directory.
Modify the gfhost.sh script (in the same directory as config.sh):
JES_CLASSPATH="$JES_CLASSPATH:../webhost.jar:$DIO_JAR"
$JAVA_COMMAND -DAS_DERBY_INSTALL=$JES_HOME/javadb -Djava.security.policy=$DIO_POLICY
-Djava.library.path=$DIO_LIB -Djkd.dio.registry=$DIO_REG -classpath $JES_CLASSPATH com.oracle.jes.util.webhost.GlassFishHost $*
Step 7: the circuit:
The final step is to wire the circuit. If you have never wired an LED with a breadboard, I highly recommend looking at this tutorial for recommended parts and instructions first.
This circuit is a single LED connected to GPIO #23 (pin 16) through a 560 ohm resistor connected to the LED's anode (long lead). The cathode (short lead) of the LED is connected to ground. You can test that the LED is oriented properly by temporarily removing the wire from GPIO #23 and connecting it to 3.3V. If the LED lights, then you have the LED oriented properly. Reconnect the wire to GPIO #23.
Step 8: Ready to test!
On the RPi, execute the modified gfhost.sh script as root:
sudo ./gfhost.sh ../helloservlet.war
After you see these lines:
Deploying ../helloservlet.war ...
Press <Enter> to exit server.
In a browser, enter the IP address of your Pi and helloservlet and if everything is in place, then the LED should blink 10 times.
Although this is a simple example, hopefully you can see the possibilities with Java Embedded Suite and DIO - web services to provide fine grained control and access to a variety of sensors, and JavaDB to store sensor data and configuration information. Java Embedded Suite and DIO make a great combination!