Go Down

Topic: Documentation and example code on using firmata client libraries in Java (Read 53 times) previous topic - next topic

I am trying for some time to get documentation on using Java and a firmata client library for the communication between a PC and an arduino. Please note I want to use Java only, i.e., not processing.

Up to no I used the excellent haskell library [hArduino for firmata communication with arduino boards.

Now I am teaching a java course at the university as a Phd student. Therefore I need to use java. However, compared to haskell there is no centralized libraries, pacakges, documentation as with haskell.

So I used google to find more information on using firmata client libraries for java. Unfortunately, the search results are polluted with irrelevant results about processing and javascript. Threfore I ask here.

According to the firmata client library page, there are two java client libraries. However, only 4ntoine seems to be up to date, while the Javarduino
 project seems abandoned.

So I guess the standard way is to use the 4ntoine java client library. Or are there other options?

(I am not interested in Processing or Javascript, i.e., what most google results revile.)

In short, where do I find up to date documentation and example code on using firmata client libraries for Java?


This question is soved.

The mentioned libraries are somewhat outdated, but might be still good as they less rely on native libs and are more pure java implementations.

However, firmata4j (on github) is the new way to do things

Example Code:
package app;

import java.io.IOException;

import org.firmata4j.IODevice;
import org.firmata4j.Pin.Mode;
import org.firmata4j.firmata.FirmataDevice;

public class Application {

   public static void main(String[] args) {
      
      
      IODevice device = new FirmataDevice("/dev/ttyUSB0");
      
      try {
         device.start();
      } catch (IOException e) {
         System.out.println("!! ERROR: device start failed.");
         e.printStackTrace();
      }
      
      while ( !device.isReady() ) {
         
      }
      System.out.println("device is now ready!");
      
      
      try {
         device.getPin(4).setMode(Mode.OUTPUT);
      } catch (IllegalArgumentException | IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      System.out.println("Pin 4 set to OUTPUT mode.");
      
      
      try {
         for(int i = 0; i < 1000; i++) {
            device.getPin(4).setValue(1);
            Thread.sleep(500);
            device.getPin(4).setValue(0);
            Thread.sleep(500);
         }
      } catch (IllegalStateException | IOException | InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      System.out.println("Pin 4 set to logic HIGH.");
      
      
      
      
   }

}
 

Go Up