Wednesday, November 6, 2013

JavaOne: Raspberry Pi Embedded Challenge

After a couple late night hours at JavaOne Java Embedded Challenge, Bruno and I had written a field sobriety tester on a Raspberry Pi using JavaFX and Java Embedded Suite. It's an example of how easy it is to build embedded solutions when all the hard work is already done for you :) Here's what we put together...

A few weeks ago I was fortunate enough to attend the JavaOne conference. I was there for purposes other than blog fodder but by happy coincidence, I stumbled across the Raspberry Pi Embedded Challenge in one of the first presentations of the event.

In the evenings, on personal time, a handful of us committed to hacking up some code on Raspberry Pi using Java Embedded Suite, Arduino, a big board of sensors, and a slick RPi-Arduino interface API (things-api) written by Vinicius Senger.

Bruno P. and I teamed to create a Field Sobriety Tester that incorporates an alcohol sensor and a "drunk swaying" sensor, that is, a Ping))) distance sensor. The distance sensor is graphed and value displayed, the alcohol monitor value is also displayed.

That's me talking about the JavaFX display
Our source code is posted on github.


The basic architecture begins with a JavaFX client running on our laptops. The client could just as easily run on the Raspberry Pi. In fact, JavaFX can directly write to the RPi's frame buffer hardware, bypassing X.

The Raspberry Pi is running Oracle Java Embedded Suite, basically like a lightweight, embedded J2EE.

The Embedded Suite is running things-api code, exposing Arduino sensors as REST web services. The RPi's things-api web services talk to Arduino's things-api code over serial. The Arduino in turn interfaces with sensors in the standard fashion.

Things-API Arduino

On the Arduino side, defining sensors is beautifully simple. Declar a Device object, then add things to it, defining the pin type and pin number, e.g., Analog pin 2. Then just loop calling loop functions.

Things-API Raspberry Pi

On the RPi side of things, well, you'll have to wait for an update to this post because I haven't had a chance to look into how things are set up. I can tell you that when it's all set up, there are REST services on the RPi corresponding to the devices defined on the Arduino. You connect to, for example, and it returns the value currently read by the Arduino.

JavaFX Front End

I'd not played with JavaFX before, only Swing. What struck me as nice about JavaFX is that you can achieve a higher degree of separation between code and UI using FXML, generated by Scene Builder, a GUI development tool. The FXML drives the creation of the UI in the start() method of your app's Application subclass. Like this:

The controller can then automagically update elements on the UI. We called the bind() method, for example, on the textProperty of a Label. We wrote a monitor class that periodically runs code to call the REST service, and updates its property which is declared as a StringProperty.

This property is made available through a  property()method, declared as ReadOnlyStringProperty used by bind(). The StringProperty class implements the Observable interface which appears to be at the heart of the magic that listens for changes to the property, and automatically updates the display. I've got more to learn.

Anyway, that's the gist of our code. We wrote it in a couple of hours by modifying example code. Not bad given that neither of us had played with JavaFX before. Bruno however is a pro Java coder, thankfully. Actually, most of our time was spent properly setting up our IDE environment.

Video Demo

If you want to see the video -- and if you are a minor, please note that I really am only joking about drinking heavily in pursuit of science. All things in moderation, my friends. Trivia: I actually didn't drink at all until I was out of college at aged 23. I've seen what abusing the drink can do and it's not pretty. Make intelligent choices, young ones.

Here's that video.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.