Java + Firmata interfacing with Arduino

Agib, you are right. That is what I want to do. I have a firmware on arduino that does what I want it to do. However it changes into different behaviors depending on the value of some variables as pre-settings. One of them is the "Time" variable. There are about 30 different variables. Most of them use lesser than three bytes but some use 10 as "Time" does. All this, I can update and upload into and by Arduino IDE, to the Arduino board. It works fine, but I am willing to make a Java API that does this task in a more effective or friendly way, so anybody can use it. It is not a Java API to upload firmwares to Arduino but just send some values through the USB to Arduino and then I use it as a stand alone device loaded with these values. In Arduino I am trying to make a function that works at the beginning of the code, sending a string or char to the host computer while waiting for connection for a limited time (10 seconds?). If connected to the USB and the java AI receives this string, then acknowledge the connection and sends back the values for those 30 variables. Then I can disconnect Arduino from USB and it is up and running with the new values. That is the general idea, but in Arduino if I use Serial.read() it receives one byte per byte and do not know how to concatenate them. I was thinking into some character like "|" to separate the values between the variables but again, this does not solve the problem of how to connect the bytes together in order to get the full value of each variable instead of its value on separate bytes. I do not know how to combine those separate bytes, that is why I was thinking about trying the Firmata path. I still have 18000 bytes of space for coding into Arduino. thanks!

Ok, the arduino initially needs to send something (just a char will do) to let the computer know it’s alive. That we can already do using the Serial.print() on the arduino, and the existing read() in the java app.

As for sending the data, you don’t need to send it in its ASCII form of 10 digits, if it is a 4 byte number then you just send those 4 bytes.

On the java side you’d have something like:

//Important to note, a "Java long" is not the same as a arduino long
//Because javas primitive types are all signed you need to use a type
//larger than you want on the arduino (you could use a int, but it would
//behave oddly when working with it in java)
long timeValue = 1234567890;

//This splits the value into individual bytes
byte b1 = (timeValue>>32)&0xFF;
byte b2 = (timeValue>>16)&0xFF;
byte b3 = (timeValue>>8)&0xFF;
byte b4 = timeValue&0xFF;

serial.write(b1);
serial.write(b2);
serial.write(b3);
serial.write(b4);

And on the arduino:

unsigned long timeValue = 0;

//Reassemble
timeValue |= Serial.read()<<32;
timeValue |= Serial.read()<<16;
timeValue |= Serial.read()<<8;
timeValue |= Serial.read();

That’s the general idea anyway.

Hi Agib, that looks great. I will try it today.
About Firmata I saw there is a forum on the its website. Maybe to post there your steps so more people can contribute in getting the clean up?
I did not know the << tool. Very interesting.
thanks a lot!

Hi Agib, I tried your method but as well as with the method rpesented on the DateTime library example, to set actually 53 variables I have to use many other as headers and footers, etc. Ending with Arduino collapsing... I was looking for another way to receive data from Serial Port, and as an alternative to Firmata, see this solution: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1247005964 regards, gps