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

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:

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

Important, if you are using a Raspberry Pi 4, you will need to make sure you are on the latest version 2.52. Because the internal wiring of the processor of this type is different compared to the previous boards, an updated gpio is available. Check the version with “gpio -v” in the terminal and install the new version with the following commands:

$ gpio -v
gpio version: 2.50
$ cd /tmp
$ wget https://project-downloads.drogon.net/wiringpi-latest.deb
$ sudo dpkg -i wiringpi-latest.deb
$ gpio -v
gpio version: 2.52

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:

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…