Java + Firmata interfacing with Arduino

Hi, I would control an Arduino Board with a Java software.... I found this library, but it works with .NET http://www.acraigie.com/programming/firmatavb/ and i would do a Java project.

Another interesting project is this--> http://www.arduino.cc/playground/uploads/Interfacing/Arduino.java.txt but it doesn't work with Java, but with Processing.

Somebody know some library for Java that can control with Firmata all Arduino pin? (analog\digital read\write)

Thank you

nothing??

Being impatient doesn’t encourage people to try and help you.

Andrew

you are right, but I thought it was one of those questions for which it has been the answer... basically if the project for Java exist somebody should only give me a link ;D

What do you actually want to achieve? A) Program the Arduino in Java language? B) Control an Arduino from a computer, that runs a Java VM?

The first problem is outside my knowledge, but the second should be easy to achieve by using RxTx-Comm library and interacting with the Arduino via a serial port (or USB serial).

I known that programming Arduino in Java is impossible, in fact, I would control Arduino form a computer that send the command with the serial. I know the library RxTx, but I see that is very difficult to use. I would some librery that with RxTx library and an implementing of Firmata(or similar) protocol can send the action to Arduino.

for example

Class ArduinoControl{ public boolean analogWrite(int pin, int pwm){...} public boolean analogRead(int pin, int pwm){...} }

IC. This would actually suit me too.

Haven't heard of Firmata up until now. I should conduct a search, I am eager to do such a Java library myself, provided I have sufficient info. In my mind a pair of applications takes shape: a host on the Arduino, written using the IDE, and implementing listen-over-serial-do-work-and-respond-back-through-serial. And a Java library, that utilizes the same protocol to call the Arduino over a serial port using RxTx-Comm. RxTx-Comm is a library I've used LOTS of times (including communication with a digital scales with custom protocols).

How is It possible that you don't know Firmata.... It's a very famous project... it is also include in Arduino IDE

http://www.arduino.cc/playground/Interfacing/Firmata

If you have some little application to send me that can help me to understand RXTX library I wil be very happy :D

Can you open a project with me on sourceforge or similar to make a library to control Arduino in easy mode? (I'm not experience with open source project, but understand Java). I can write the part of the protocol with Firmata and you the speaking with rxtx library

Hi France,

rxtx is not that complex. when you use the right version.
I struggle long time looking for connecting java and arduino board until to find that the repositories of my linux distro were not the right version.
See this may help you:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1244869368/0#0

regards,
gps

Hi,

I've written a little Serial class in Java that abstracts all the RXTX nonsense away from using the serial port.

You can download the eclipse project folder from here. There's a example class which shows you how to use it. The project contains RXTX so you should just be able to import it into eclipse and go. (Assuming you're on windows that is)

One thing you should be aware of is the serial port is only read when you call available(), so you should call available() before calling read(). It would be possible to start a thread to do the reading, but the way it is at the moment is simpler, and makes it clearer to see what is going on.

Let me know if you need any help.

Hi Agib, Your code looks great. A lot more simple. However, since I am under linux, I just did small changes as the comm port to "/dev/ttyUSB0", its speed and the RXTX libraries as previously posted. Your code finds the ports and connects with Arduino. However the received data in Eclipse's console is in "garbage" style. Comparing your code with some other that receive from Arduino properly, I think the issue comes up on the settings of the port. Maybe by adding settings for the bits (8), stops(1) and no Parity can "clean" the readings from Arduino. I am trying to add these SerialPort Parameters to the port on your code to test it, but it seems I need some help. :-[ Can you help me? just if you have some time. I am writing a java API to send data to the arduino's firmware (with Firmata), in order to regulate arduino's behavior. I seems to be similar to what France is doing. thanks! gps

Hi Gian,

I've spotted a problem with my code. The port initialisation part wasn't setting up the port correctly. I assumed that since it worked on my system then it didn't need it, but I guess other systems don't default to settings that work with the arduino!

I've uploaded a fixed version, but all you really need to change is:

Replace

port = (SerialPort)id.open(this.getClass().getName(), speed);

(in the Serial constructor) with

port = (SerialPort)id.open(this.getClass().getName(), INPUT_BUFFER_SIZE);
port.setSerialPortParams(
      speed,
      SerialPort.DATABITS_8,
      SerialPort.STOPBITS_1,
      SerialPort.PARITY_NONE);

and put

private static final int INPUT_BUFFER_SIZE = 2000;

at the top of Serial.

I'm happy to help anyway I can, I'm familiar with Java and using RXTX, I'm not familiar with the firmata protocol though.

Hi Agib and France, The changes to the code cleaned the readings from Arduino but the speed is so slow that I receive only parts of it. Maybe the size buffer declaration? Here is another code I have been using and it gets perfect readings at least in Eclipse (java) and NetBeans consoles.

http://www.csc.kth.se/utbildning/kth/kurser/DH2400/interak08/SerialWork.java

I just changed the port to "/dev/USB0" (for windows users it would be some COM#) and the speed according to the firmware uploaded into Arduino [in my case Serial.begin(19200);]. I need to enable Arduino to collect strings from USB, and not just in separate bytes [as Serial.Read() seems to be limited to - http://arduino.cc/en/Serial/Read ]. Then Firmata seems to be the option. My understanding is that France also wants to use Firmata instead of the Serial library for the firmware uploaded to Arduino. :-? Firmata allows different speeds and clearer or lesser verbose communications with the host computer's API, and it is proposed to become a standard for communication. The problem is that there is no Firmata version for Java. See here: http://firmata.sourceforge.net/wiki/Download France, have did tried with the "Processing" version? maybe by finding alternatives to some of its "#include" since it is almost a java version... you may be very close to solve it. The alternative is to move into C++ or other language. I am also looking for using Firmata with java (using LGPL libraries). Regards, gps

Hi,

I wasn’t intending for my Serial class to be used in place of Firmata. It was more for providing an easier method to implement Firmata in Java, removing some of the need to deal with RXTX.

Looking at the code you linked to, it uses a serial event listener (I didn’t even know RXTX had this, I’ll definately be using it in the future!). This event driven method will be faster than the loop and check method I’ve used, as means you won’t be sat spinning waiting for data.

I've been looking at the Processing example that was posted, I've hacked it about a bit so it doesn't depend on the processing serial stuff, and uses my (slightly modified) Serial class. I've uploaded the code here.

I havn't modified any of the message handling code, just the stuff that dealt with the serial port. I uploaded the SimpleDigitalFirmata sketch to my Arduino, and I was able to blink the LED on pin 13 directly from the Java app.

Hi Agib, your java class is great. And if we can use it to send strings to Arduino, why not? I have no problem with that. However, the issue now is that Arduino has to be able to read the information coming from serial (sent by our java API). It does well with the Serial.read() but byte per byte (this is my understanding). As an example, I want a variable declared in the firmware uploaded into Arduino as:

unsigned long Time = 0123456789;

Then "Time" would used as a time and date variable (in unix_t or EPOCH time format - see http://www.epochconverter.com ) by receiving its value from USB / and a host computer's java API. As you see, my variable has 10 bytes... so with Serial.read() in Arduino's firmware it seems to be too complex to get these and the other 30 variables loaded with their initial values from serial sent by a java API if in Arduino I use the "default" Serial library.

Our java classes and rxtx for the java API solved the connection problem. Now the communication is the next step. Using "Serial" library (which includes Serial.read) seems to be very limited. However, with Firmata it would be great. Your class is not competing with Firmata, I never had in my mind it attempted to replace Firmata since the problem now resides in Arduino's serial reading capability. Let me explain it; to use Firmata as a standard for communication we have to use it in both sides, Arduino board and the java API on the host computer. That is what France is asking too get, but building the host computer's API in Java. We can upload Firmata to Arduino but we do not have yet a Firmata version on the Java API developing side. :'( If moving our API form Java to C++, there are Firmata versions for the host computer (GPL for linux and LGPL for Windows). But we want to stand with Java. A solution is using "Processing": in one side uploading Firmata to Arduino and on the host computer including the "Firmata version for Processing" in Processing ( http://processing.org ). But Processing is not like using Eclipse or Netbeans to develop a Java API... We need a Firmata Java version or to improve the Serial.read function of Serial library for Arduino board... as in my case it would be enough something like Serial.readString() function. Then what we have in our java classes would be enough. Thanks for sharing your thoughts! Maybe France found a workaround for that library... ;) kind regards, gps

Agib, please excuse me. I sent my last post just when receiving yours.

what are you using on the Arduino board side to read the information from the serial port? Firmata or Serial.read()?

You may well have got it!

The Firmata Arduino library has an example called SimpleDigitalFirmata which implements the callbacks to listen for commands from the computer, then sets the digital pins correspondingly.

It also appears to send the input state of the digital pins back to the computer, but I've tried fiddling with this and it doesn't behave as I'd expect, so this will need more looking into.

I think we certainly have the beginnings of what could become a Firmata API in Java. The existing code could do with a clean up though! :D

It's worth noting, the Firmata library doesnt provide any methods for communicating values or strings, so these would have to be implemented using the sysex commands. If all you need is the ability to pass strings back and forth, then Firmata might be overkill.

Hi Agib, just as you said... your version of arduino.java is a beginning of a Firmata version for Java APIs. I did the same test you did, under linux and it behaved just as you mentioned. I just changed the comm port in Example.java and deleted the lib folder. For RXTX, now I am using the plug-in for Eclipse, proposed on http://rxtx.qbang.org/wiki/index.php/Wrapping_RXTX_in_an_Eclipse_Plugin It was tested for Mac and Windows, but I had no problems to implement it for Linux. It helps to adapt your java APIs to several platforms or environments - not just the one of your computer.

I am too much a newbie on coding to be able to help on the clean up... Maybe David A. Mellis can be contacted and can help since he did the Firmata for Processing. He is one of the Administrators of the forum and maybe he can help in finding what is still needed.

Overkilling way to solve my problem? I feel the same... but I do not know how to build a string from Serial.read(), maybe there is something for concatenating that I have not find, and I should put more effort into that path rather than Firmata for Java. But France's question is one that I guess many are also sharing and I did not mind to try to help on it. Maybe she can help too on the cleaning up process. ;)

Great work!

Sending strings backwards and forwards isn't that difficult, but re-reading your earlier post it sounds like you want to sent the unix_t value from the computer to the arduino. You could send this as a string, but that would be 10 bytes, or you could send it as 4 bytes, then combine them back to the unsigned long on the arduino.

Perhaps you could clarify what it is you're trying to achieve?

I'll take a look at that link when I get a chance tomorrow, it looks interesting.