<< PiJava overview: Java 11 and JavaFX 11 on Raspberry PI
PiJava - Part 5: Running the minimal JavaFX 11 application on Raspberry PI >>

PiJava - Part 6: JavaFX 11 application on Raspberry PI with TilesFX and GPIO

2019-04-15 10:46:41
TL;DR; Combining the previous 5 posts, adding TilesFX and GPIO access into one "dashboard application" for the PI.

What we will do

Based on the previous blog posts
So there is one "small" step remaining: build something which actually does something on the PI, talking to the GPIO's and show what's happening.
Spoiler alert: this is what's is going to look like:


As this is just a proof-of-concept, I'm not doing anything fancy at all, just a LED we can turn on and off, and a button to read out the state of it. So a simple setup is used:
  • GPIO22 (WiringPi n° 3) > resistor > LED > ground
  • 3.3V > push button > GPIO24 (WiringPi n° 5)

Java to GPIO

At first I wanted to use PI4J: "a friendly object-oriented I/O API and implementation libraries for Java Programmers to access the full I/O capabilities of the Raspberry Pi platform". But a Java 11 version is not ready yet, so that will be for a next blog series... ;-)

For this quick demo, I just used basic gpio commands which you can also use in terminal to toggle the led on GPIO22 = WiringPi number 3 (1 on, 0 off):
$ gpio mode 3 out 
$ gpio write 3 1
$ gpio write 3 0
And read the button state on GPIO24 = WiringPi number 5 (1 pressed, 0 not pressed):
$ gpio mode 5 in 
$ gpio read 5

Install TilesFX module on the PI

The application as a single JAR file (which is only 9.7kb) will need Java 11 and JavaFX 11 to be available on the board, this is what has been done already in the previous blogs (part 2 and 5).

But we will also need the TilesFX module, a great library made by Gerrit Grunwald. We can easily download this from Maven and put it side-by-side with Java and JavaFX in the opt-directory. This library doesn't contain any platform specific code, so again the JAR can be used on both PC and Linux/PI. A script file is available here.
# Make sure we are in the home directory
cd /home/pi

# Download the TilesFX module from Maven
wget -O tilesfx-11.1.jar http://central.maven.org/maven2/eu/hansolo/tilesfx/11.1/tilesfx-11.1.jar

# Make a directory in opt
sudo mkdir /opt/tilesfx11

# Move the downloaded file to opt
sudo mv tilesfx-11.1.jar /opt/tilesfx11

The code

Based on the minimal JavaFX application from blog part 4, two additional classes were added. You can find the full source on GitHub, so a short overview here:


This class generates the view with multiple tiles from the TilesFX-library:
  • two as demo only (TEXT and CLOCK)
  • close button to exit the application (CUSTOM)
  • a toggle button to put the led on and off (SWITCH)
  • and two line charts (SMOOTHED_CHART), one with dummy temperature measurements, one with value 0 or 100 depending on the button state

Check the source in GitHub for the full code. As an example, this is the limited code we need to build the toggle tile and change the GPIO state of the led:
var ledSwitchTile = TileBuilder.create()
   .prefSize(250, 200)
   .title("Gpio " + PIN_LED)
ledSwitchTile.setOnSwitchReleased(e -> Gpio.setPinState(PIN_LED, ledSwitchTile.isActive()));


The Gpio class has an execute function, which uses Process to send the commands identical to how we can use them in the terminal.
private static String execute(String cmd) {
   Process p = Runtime.getRuntime().exec(cmd);
E.g. this is the function to toggle a pin:
public static void setPinState(final int pin, final boolean on) {
    execute("gpio write " + pin + (on ? " 1" : " 0"));

Run it!

We will run the application with Java 11.0.2 so don't need to add a reference to the JavaFX module as it is integrated. But we do need to link to the TilesFX module we downloaded before. A script file is available here.
sudo /opt/jdk-11.0.2/bin/java \
	--module-path=/opt/tilesfx11 \
	--add-modules=eu.hansolo.tilesfx \
	-jar /home/pi/git/PiJava/out/pijava-0.1-SNAPSHOT.jar \
	-Dprism.verbose=true \
	-Dembedded=monocle \
And here it is, in full action:


I wanted to be able to run Java, JavaFX, TilesFX on a PI and talk to the GPIO's, so mission accomplished! Many possible paths from here. WebView from JavaFX doesn't work on the PI and PI4J can use some help to bring it to Java 11 and Java 12 is already available...... So to be continued...

Related posts

ENPiJava overview: Java 11 and JavaFX 11 on Raspberry PI
As my daily work mainly is Java and back-end stuff on "real servers", I set myself for 2019 as a personal goal to experi...

ENPiJava - Part 6: JavaFX 11 application on Raspberry PI with TilesFX and GPIO
What we will do Based on the previous blog posts we can install and run Java 11 we have a minimal JavaFX 11 applica...
ENPiJava - Part 5: Running the minimal JavaFX 11 application on Raspberry PI
In part 2 of this blog series Java 11 was successfully installed on a PI. And in part 4 a running Java FX jar was gen...
ENPiJava - Part 4: Building a minimal JavaFX 11 application with Maven
I prefer a Java app above a web app, because starting a new "modern" web development requires you to pull a bunch of dep...
ENPiJava - Part 3: What is Java FX?
What is JavaFX? From the Oracle site: "JavaFX is a set of graphics and media packages that enables developers to design...
ENPiJava - Part 2: Installing and running Java 11 on a Raspberry PI 3 Model B+
After my first (and failed) attempt to get Java 11 running on an old Raspberry PI, I bought a brand new 3 Model B+ versi...
ENPiJava - Part 1: Trying to run Java 11 on an old Raspberry PI
One of my goals for 2019 is experimenting with the latest Java and JavaFX versions on a Raspberry PI. After my experimen...