sábado, 29 de abril de 2017

Control Arduino from a JavaFX application

That's exciting. I just finished a post about JArduino and Java and I was supposed to sleep, but I wanted to give it a try with JavaFX, so I did and in less than a minute I could make a first experiment!

Show a light sensor information in a label

In Control Arduino from Java using JArduino post we printed on console the current light intensity coming from a LDR connected to an arduino. In less than a minute I could add it to a label in a JavaFX application, see:

 I basically reused the same class from the other post and read the output in a JavaFX thread (see Platform.runLater on the code below).

My plan was to also turn on a LED from the JavaFX application. So let's continue on the original plan.

Controlling a LED and reading a LDR

In the second version I used a chart to display the LDR sensor data in real time and also used a button to control a LED, so when we turn the LED on we can see the values changing.

See our circuit now and notice a LED on digital pin D1:

Now the code on LightSensorApp (which extends JArduino) was modified to include also a LED command. The JavaFX application still look simple, we have a button a chart.

The following video shows it in action:

The code is below and we didn't modify the maven pom.xml, so please check the only two Java files we have in our project.

The code used in these projects are on my github.

What's next?

You tell me. This is so exciting and my head explode about the possibilities. Think about using API with the great hansolo libraries, such as TilesFX and Medusa. Or about integrating your business process with "things" using the JArduino ethernet integration and jBPM.... The list goes on and on.
NOTE: I know and understand that we have many other modern boards with modern APIs, but arduino is, easy, cheap and popular which makes it really exciting!

Control Arduino from Java using JArduino

In general when we want to integrate Java with Arduino we can use the serial port for interaction, where arduino will receive commands and also write sensors information. This is greatly explained on JavaFX 8: Introduction by Example book on Arduino chapter. The problem with this approach is that you will end having to maintain two distinct codes in two different languages.

Another approach is use JArduino, which allows you to write Java code to directly interact with arduino! What you should do is follow the procedure from README file in JArduino page to install the firmware in your arduino board, then build a maven project that has jarduino as a maven dependency to start building Java applications that uses arduino. Since it is Java we can use JavaFX, JavaEE and all the Java APIs!


I always start writing before having something working (that's the reason why I had more than 50 draft posts on this blog). So I faced some unwanted issue and I had to figure out a few things to get this working (in fact, it is still not working, but if someone is reading this, it worked).

The error that took me a few minutes was:

java.lang.UnsatisfiedLinkError: no rxtxSerial64 in java.library.path thrown while loading gnu.io.RXTXCommDriver

This comes from the missing native rxtx library. Just download the one required for your system here and place in your JDK lib directory, in my case it is: /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64

That was the major issue, a simple one to be honest.

Reading a light sensor (LDR) from Java

The Hello World jArduino tutorial is the classic blink app. I wanted something a little more interactive, so I started creating a simple Java application that reads an analog input connected to a LDR, just like described in this arduino tutorial, but with Java. Things to note about the code:

  • The JArduino class mimics an Arduino sketch. You just have to extend JArduino and implement your stuff on loop and setup methods, just like I meant to this on Quick visual effects apps development with JavaFX but with Processing;
  • I am using Maven and I had to add some extra repositories to my pom.xml because some dependencies are not available in maven central;
  • The only thing I did to prepare my arduino to be used with jArduino was upload the jArduino sketch to it. But remember to provide the USB port where arduino is connected when creating the jArduino application
  •  The physical circuit is the same  from the Using an LDR Sensor with Arduino: A Tutorial for Beginners tutorial, the only difference is that I am using a brazilian arduino called "garagino", which I bought in a promotion years ago! See:

And here's the code:

The result is simple the light intensity on console. If I cover the sensor, the number decrease, see:

Now let' s add something visual to it, let's use JavaFX! Next post I will share a version with JavaFX.

sexta-feira, 14 de abril de 2017

Using tensorflow from a Java application

At this point you probably have heard about TensorFlow, the famous Google library for deep learning.
Although the TensorFlow core is C/C++, the most famous API is Python (as you can see in Siraj or sintdex tutorials), but recently TensorFlow released a Java version for the API.

TensorFlow has a small tutorial about the Java API and a single more complex example to classify image. On this small tutorial the link to the tensorflow JAR is broken. I found a working JAR in this URL. The upstream tutorial was already corrected as you can see in github.

There's also the labeled image example, which is very simple as well, but a few additional steps must be taken:

  • Download the content from this URL and unzip it. It is the labeled dataset
  • Download the LabelImage class from github. When saving it locally remember to remove the package declaration.
  • Having the tensorflow JAR you downloaded previously and the native library, you can compile and run the class passing the directory where you unzipped the labeled dataset and a test image.

In the example above you can see it found that the image contained a "Zebra". There are 1001 labeled images on the downloaded dataset and it usually labels the image correctly.


A few days ago the tensorflow artifacts was added to maven  central repository. To use it you just need the dependency org.tensorflow:tensorflow:1.1.0-rc1. Seriously, no other configuration is required, you just need to add this dependency and everything should work. A test sample maven application can be found at my github

You will find a simple JavaFX Application that allows you to select a file that will be analyzed by the same code used in the LabelImage official example. See some screenshot:

To run this application locally you must have maven and Java. Then download the dataset and the labels and modify the PATH_TO_DATA_DIR in App class to the path were you extracted it. Finally, just run:

mvn -q compile exec:java

And it should open the application.


TensorFlow Java API is very easy to "install" and use. I would say that it is easier than python installation! All my tests were made on