Arduino is an open source, low-powered, low-cost microcontroller. It’s often used in Internet of Things projects to take input from sensors in the environment and to respond appropriately. As a developer, you write a ‘sketch’ which then runs continuously on the Arduino to tell it how to behave. With this sample Arduino feature for Liberty profile, you can write Java applications that interact with one or many Arduinos.

We recently published the source of the Arduino feature in Github. Here’s a bit more info about it.

Here is an example of how easy it is to get your Java apps talking to Arduino using the Liberty Arduino user feature.

Writing the code that will run on the Arduino

You write this in the Arduino IDE in C and then transfer it to the Arduino itself. You will need to have added the Liberty Library to the Arduino IDE before it can reference the correct content. See the readme on Github for information on how to set that up.

Here’s the code you need to use the Liberty library in an Arduino sketch:

#include <EEPROM.h>
#include <Liberty.h>

Liberty liberty;

void setup() {
   liberty.begin();
}
void loop() {
   liberty.update();
}

Configuring the Liberty server

Next, you need to configure the Liberty server to load the Arduino server-side functionality. You also need to configure where the application code can find your Arduino. Give your Arduino a name so that you can refer to the correct device if you have multiple Arduinos accessed from the same application.

Put this in the server.xml of the Liberty server:

<server description="new server">

    <featureManager>
        <feature>jsp-2.2</feature>
        <feature>usr:arduino-1.0</feature>
    </featureManager>

    <httpEndpoint id="defaultHttpEndpoint" httpPort="9080" httpsPort="9443"/>

    <usr_arduino id="myArduinoName" ports="COM10"/>

</server>

Communicating with the Arduino from your application

Finally you need to communicate with the Arduino from within your application code. In the example below, we are setting the LED on and off directly from our code. We could also remotely invoke methods in our Arduino sketch or setup callbacks based on sensor input from our Arduino. See the Github wiki for full details on the API.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.ibm.wasdev.arduino.*" %>
<%@ page import="static com.ibm.wasdev.arduino.Arduino.Level.LOW" %>
<%@ page import="static com.ibm.wasdev.arduino.Arduino.Level.HIGH" %>
<%@ page import="static com.ibm.wasdev.arduino.Arduino.Mode.OUTPUT" %>
<html>
  <body>
    <h2>Liberty Arduino Demo - Helloworld</h2>
     Refresh the page to switch on and off the LED on pin 13
    <p><b>
    <%
        int led = 13;
        Arduino arduino = ArduinoService.get("myArduinoName");
 arduino.pinMode(led, OUTPUT);
 if (arduino.digitalRead(led) == LOW) {
 arduino.digitalWrite(led, HIGH);
 >Light on!<%
 } else {
 arduino.digitalWrite(led, LOW);
 %>Light off!<%
 }
 %>
 </b></p>
 </body>
</html>

And…ta-da! One Java application talking to your Arduino!

8 comments on"Getting Java apps talking to Arduino"

  1. How can I get “com.ibm.wasdev.arduino.*” files? I cant found it anywhere.

  2. Hello, How can we communicate java eclipse program with arduino via bluetooth? I have a desktop application using java language and need to received and send data to my arduino program.

    • Hi ally, there are two parts to doing that, your Bluetooth device needs to support the Serial Port Profile (SPP), and then you need some software to enable Java to interact with the Serial Port. A lot of Bluetooth devices do support SPP so that likely may well just work. Next you need software for Java applications to interact with the Serial Port. There used to be a standard Java Communications API for this but that is no longer part of standard Java, however there are some third party packages that provide this now. This article you’re commenting on is about one of those – the Liberty Arduino Feature – which enables Java programs running in WebSphere Liberty Application Server to interact with an Arduino. The Liberty Arduino Feature does also run outside of Liberty though, and you could use it to have you Java Eclipse program communicate with an Arduino. You can see examples of doing this in the feature’s testcases, for example:

      https://github.com/WASdev/sample.arduino/blob/master/Liberty/org.ibm.ws.arduino.test/src/com/ibm/wasdev/arduino/test/InvokerTestcase.java

      Happy to help further if you need more help on how to set that up.

  3. Is it possible to make this work via WiFi (to an Arduino that has a WiFi shield and can communicate via TCP/IP by using that shield) rather than via a COM port? I’m thinking that the more interesting applications probably require an untethered Arduino.

    • Hi Phil, yes it is, and there is already some support for doing that.

      In the server.xml instead using the name of a serial port an Arduino is using you can give it the ip/port of a remote Arduino on the network. So far this has only been tested with an Arduino using and RN-XV WiFly module, and as there are no standards for Arduino WiFi shields and they often work slightly differently there are likely minor updates required for other types of modules. If you have a particular one to use in mind I’m happy to go have a look.

      I agree with your comment about the more interesting applications probably require an untethered Arduino. We’ve already been using this with several other types of wireless technologies commonly used with Arduinos, and some of these are more appropriate than WiFi which can be expensive compared to the price of the Arduino and uses a lot of power so isn’t so good for battery powered remote devices.

      The Arduino feature works well over Bluetooth using things like HC-05 and HC-06 Bluetooth cards, one of the demos at the IBM Impact conference showed using this with an Arduino connected with an HC-06. The Ciseco SRF transceivers are easy to use and work with this too, we had a demo using one of those at the Devox conference last week, which you happen to be able to see in this photo – https://twitter.com/WASdevnet/status/477094169786449921 – its the thing with the green LED hanging over the hackathon sign.

      I’ve also been using nRF24L01s which are really cheap and flexible. The code for that hasn’t been included in this first release yet but i hope to get that out soon. And also support for Bluetooth LE with the Arduino feature providing central role support and so able to support all the API features when connecting to Bluetooth LE Arduino peripheral nodes.

      • Thanks for the detailed reply!

        I have a CC3000 WiFi shield from Adafruit, and as you say it does eat up a lot of power. It’s nice that I can communicate with it via normal TCP sockets, but I’ve been meaning to look into some of the more lightweight wireless technologies too — this gives me another good reason to do that!

        • CC3000’s use SPI for communication not UART so wont run with the current Arduino feature release out-of-the box. It will need some wrapper code around the CC3000 library to provide a stream style interface that the Arduino feature can use. The code i have for nRF24L01s does that so i’ll try to get this committed in the github repo soon with an example of how to use it.

Join The Discussion

Your email address will not be published. Required fields are marked *