Oracle Blog

speakjava

Thursday Aug 16, 2012

Power Control JavaFX Interface
Having completed the construction
of my power control system I've
finally found time to build the software interface using the Arduino
board I included in it.

First off I needed some code on the Arduino that would listen for
commands comming via the USB connection and then take the appropriate
action. Since all that is required is to set one of two pins
either high or low the protocol is trivial. The C code for the
Arduino is shown below:

All this does is initialise the serial port to work at 9600 baud and
configure pins 2 and 3 as outputs. Why use pins 2 and 3 and not 0
and 1 I hear you ask. The answer is that pins 0 and 1 are also
used for accessing the UART
of the Arduino. Once programmed and up and running this is no
problem, but if you have an application running that is using these
pins you can't then upload a program the Arduino though the USB
port. This caused me some problems to start with until I found a
blog reference to this elsewhere. To make life easier I switched
to using pins 2 and 3.

The loop function looks for bytes being sent via the USB serial
connection and takes the appropriate action in setting the pins high or
low. To keep things simple I used 'a' and 'A' for socket 1 and
'b' and 'B' for socket 2. Lower case sets the pins low (turning
the socket off) and upper case sets the pin high (turning the socket
on). To test this all you need to do is use the Serial Monitor in
the Arduino IDE and type the appropriate character.

Next we need some way of sending the appropriate bytes from the
controlloing PC. Java has long had the JavaComm
API which provides an API for all things serial and parallel. The
PC I'm using for the UI is running Ubuntu Linux, so I used the
available librxtx-java
package. This has a rather frustrating limitation, that I would
describe as a bug. Plugging the Arduino USB into my machine
automatically creates me a device to use to access this, which is what
we need. In this case the device is /dev/ttyACM0. The
problem is that librxtx-java will only
recognise serial ports of the form /dev/ttyS{number}. To get
round this I created a symbolic link from /dev/ttyACM0 to /dev/ttyS4
(since I actually have physical serial ports on my machine using ttyS0
to ttyS3). The big drawback to this is that when the machine is
rebooted the OS very thoughtfully removes my symbolic link. At
some point I need to try and figure out if there is a way through udev
to make this work properly.

The code below shows part of the class I created to handle
communication with the Arduino through the serial port:

Passing /dev/ttyS4 to this constructor provides the application with an
InputStream and OutputStream to communicate with the Arduino. To
simplify things further I subclassed my Arduino communications class to
make it specific to my power control adding some useful methods shown
below:

All that is the required now is a user interface to provide a way of
sending the appropriate character when the user wants to change the
power state. I borrowed some button graphics from Jaspers JavaOne
Kinect demo last year and a nice background I found here.
The result is shown below:

One of the things I've just started really using when developing JavaFX
is the Builder classes. These are great for making it easy to
create Nodes and setting numerous attributes without having to call
each method individually on the object.

I guess the next thing is to make this into a simple web service so I
can control my Raspberry Pi
and Beagle Board from a web
browser antwhere in the world.

About

A blog covering aspects of Java SE, JavaFX and embedded Java that I find fun and interesting and want to share with other developers. As part of the Developer Outreach team at Oracle I write a lot of demo code and I use this blog to highlight useful tips and techniques I learn along the way.