Pular para o conteúdo principal

Control Arduino from Java using JArduino

In general when we want to integrate Java with Arduino we can use the serial port for interaction, where arduino will receive commands and also write sensors information. This is greatly explained on JavaFX 8: Introduction by Example book on Arduino chapter. The problem with this approach is that you will end having to maintain two distinct codes in two different languages.

Another approach is use JArduino, which allows you to write Java code to directly interact with arduino! What you should do is follow the procedure from README file in JArduino page to install the firmware in your arduino board, then build a maven project that has jarduino as a maven dependency to start building Java applications that uses arduino. Since it is Java we can use JavaFX, JavaEE and all the Java APIs!

Problems


I always start writing before having something working (that's the reason why I had more than 50 draft posts on this blog). So I faced some unwanted issue and I had to figure out a few things to get this working (in fact, it is still not working, but if someone is reading this, it worked).

The error that took me a few minutes was:

java.lang.UnsatisfiedLinkError: no rxtxSerial64 in java.library.path thrown while loading gnu.io.RXTXCommDriver

This comes from the missing native rxtx library. Just download the one required for your system here and place in your JDK lib directory, in my case it is: /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64

That was the major issue, a simple one to be honest.

Reading a light sensor (LDR) from Java

The Hello World jArduino tutorial is the classic blink app. I wanted something a little more interactive, so I started creating a simple Java application that reads an analog input connected to a LDR, just like described in this arduino tutorial, but with Java. Things to note about the code:

  • The JArduino class mimics an Arduino sketch. You just have to extend JArduino and implement your stuff on loop and setup methods, just like I meant to this on Quick visual effects apps development with JavaFX but with Processing;
  • I am using Maven and I had to add some extra repositories to my pom.xml because some dependencies are not available in maven central;
  • The only thing I did to prepare my arduino to be used with jArduino was upload the jArduino sketch to it. But remember to provide the USB port where arduino is connected when creating the jArduino application
  •  The physical circuit is the same  from the Using an LDR Sensor with Arduino: A Tutorial for Beginners tutorial, the only difference is that I am using a brazilian arduino called "garagino", which I bought in a promotion years ago! See:

And here's the code:

package org.fxapps.arduino;
public class App {
public static void main(String[] args) {
// remember to use the correct port in your project
new LightSensorApp("/dev/ttyUSB0" , System.out::println)
.runArduinoProcess();
}
}
view raw App.java hosted with ❤ by GitHub
package org.fxapps.arduino;
import java.util.function.Consumer;
import org.sintef.jarduino.AnalogPin;
import org.sintef.jarduino.InvalidPinTypeException;
import org.sintef.jarduino.JArduino;
public class LightSensorApp extends JArduino {
private Consumer<Short> receiveLight;
public LightSensorApp(String port, Consumer<Short> receiveLight) {
super(port);
this.receiveLight = receiveLight;
}
@Override
protected void setup() throws InvalidPinTypeException {
}
@Override
protected void loop() throws InvalidPinTypeException {
short light = analogRead(AnalogPin.A_0);
receiveLight.accept(light);
delay(100);
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.fxapps.arduino</groupId>
<artifactId>arduino-light-chart</artifactId>
<version>1.0</version>
<repositories>
<repository>
<id>thingml-snapshot</id>
<name>thingml-snapshot</name>
<url>http://maven.thingml.org/thingml-snapshot/</url>
</repository>
<repository>
<id>thingml-release</id>
<name>thingml-release</name>
<url>http://maven.thingml.org/thingml-release/</url>
</repository>
<repository>
<id>thirdparty</id>
<name>thingml-snapshot</name>
<url>http://maven.thingml.org/thirdparty/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.kevoree.extra</groupId>
<artifactId>org.kevoree.extra.osgi.rxtx</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.sintef.jarduino</groupId>
<artifactId>org.sintef.jarduino.core</artifactId>
<version>0.1.7-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.sintef.jarduino</groupId>
<artifactId>org.sintef.jarduino.serial</artifactId>
<version>0.1.7-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
view raw pom.xml hosted with ❤ by GitHub
The result is simple the light intensity on console. If I cover the sensor, the number decrease, see:




Now let' s add something visual to it, let's use JavaFX! Next post I will share a version with JavaFX.

Comentários

Postagens mais visitadas deste blog

Dancing lights with Arduino - The idea

I have been having fun with Arduino these days! In this article I am going to show how did I use an electret mic with Arduino to create a Dancing Lights circuit. Dancing Lights   I used to be an eletronician before starting the IT college. I had my own electronics maintenance office to fix television, radios, etc. In my free time I used to create electronic projects to sell and I made a few "reais" selling a version of Dancing lights, but it was too limited: it simply animated lamps using a relay in the output of a 4017 CMOS IC. The circuit was a decimal counter  controlled by a 555. 4017 decimal counter. Source in the image When I met Arduino a few years ago, I was skeptical because I said: I can do this with IC, why should I use a microcontroller. I thought that Arduino was for kids. But now my pride is gone and I am having a lot of fun with Arduino :-) The implementation of Dancing Lights with Arduino uses an electret mic to capture the sound and light leds...

Simplest JavaFX ComboBox autocomplete

Based on this Brazilian community post , I've created a sample Combobox auto complete. What it basically does is: When user type with the combobox selected, it will work on a temporary string to store the typed text; Each key typed leads to the combobox to be showed and updated If backspace is type, we update the filter Each key typed shows the combo box items, when the combobox is hidden, the filter is cleaned and the tooltip is hidden:   The class code and a sample application is below. I also added the source to my personal github , sent me PR to improve it and there are a lot of things to improve, like space and accents support.

Genetic algorithms with Java

One of the most fascinating topics in computer science world is Artificial Intelligence . A subset of Artificial intelligence are the algorithms that were created inspired in the nature. In this group, we have Genetic Algorithms  (GA). Genetic Algorithms  To find out more about this topic I recommend the following MIT lecture and the Nature of Code book and videos created by Daniel Shiffman. Genetic Algorithms using Java After I remembered the basics about it, I wanted to practice, so I tried my own implementation, but I would have to write a lot of code to do what certainly others already did. So I started looking for Genetic Algorithm libraries and found Jenetics , which is a modern library that uses Java 8 concepts and APIs, and there's also JGAP . I decided to use Jenetics because the User Guide was so clear and it has no other dependency, but Java 8. The only thing I missed for Jenetics are more small examples like the ones I will show i...