Introdution
We have prepared earlier Arduino with an installed library Firmata (post how to prepare Arudino with firmata - link). Now we connect from the Raspberry to the Arduino with Firmata. We have the possibility to connect from most programming languages. In my opinion, best choice is Java.
We need
- Raspberry Pi with installed ArchLinux
- Arduino(Mega) with installed Firmata library (Firmata - Arduino)
The ArduinoMega connected to the Raspberry Pi
Installation Java Development Kit
refresh of all package list:
# pacman -Syyinstall open JDK:
# pacman -S jdk8-openjdktest jvm:
# java -versiontest java compiler:
# javac -versionyou should see something similar to:
Java firmata library
There are many implementations firmata on java. My choice fell on firmata4j.
Writing Arduino equivalent "Hello world" application
We have two basic software way to build java application:
- connect Arduino to your PC computer (not Raspberry), write appliction, compile, tests and move copiled binary to Raspberry
- connect Arduino to Raspberry, write application, compile, test and run on Raspberry
I'm going show the example by first scenario. I use on my workstation PC:
- JDK - Java Development Kit, Oracle or Open Source distirbution, it does not matter.
- gradle - open source build automation system
- Eclipse - IDE for Java development
Now we write simple application that controls LED13 on Arduino.
Localization LED13
Full Java code of this app is below:
import java.io.IOException;
import org.firmata4j.IODevice;
import org.firmata4j.Pin;
import org.firmata4j.Pin.Mode;
import org.firmata4j.firmata.FirmataDevice;
public class Main {
private static final int PIN_NUMBER = 13;
private static final long DELAY_MILISECONDS = 1000;
private static final long LOOP = 10;
public static void main(String[] args) throws IOException, InterruptedException {
IODevice device = new FirmataDevice("COM3"); // construct the Firmata device instance using the name of a port
device.start(); // initiate communication to the device
device.ensureInitializationIsDone(); // wait for initialization is done
Pin pin = device.getPin(PIN_NUMBER);
pin.setMode(Mode.PWM);
for (int i = 0; i < LOOP; i++) {
Thread.sleep(DELAY_MILISECONDS);
pin.setValue(1); // on led
Thread.sleep(DELAY_MILISECONDS);
pin.setValue(0); // off led
}
device.stop(); // stop communication to the device
}
}
Full code with gradle configuration file can you download from link.
clone project (or download from page):import java.io.IOException;
import org.firmata4j.IODevice;
import org.firmata4j.Pin;
import org.firmata4j.Pin.Mode;
import org.firmata4j.firmata.FirmataDevice;
public class Main {
private static final int PIN_NUMBER = 13;
private static final long DELAY_MILISECONDS = 1000;
private static final long LOOP = 10;
public static void main(String[] args) throws IOException, InterruptedException {
IODevice device = new FirmataDevice("COM3"); // construct the Firmata device instance using the name of a port
device.start(); // initiate communication to the device
device.ensureInitializationIsDone(); // wait for initialization is done
Pin pin = device.getPin(PIN_NUMBER);
pin.setMode(Mode.PWM);
for (int i = 0; i < LOOP; i++) {
Thread.sleep(DELAY_MILISECONDS);
pin.setValue(1); // on led
Thread.sleep(DELAY_MILISECONDS);
pin.setValue(0); // off led
}
device.stop(); // stop communication to the device
}
}
Full code with gradle configuration file can you download from link.
# git clone https://github.com/SulacoSoft/robotics-examples-firmata-blink-led13.gitcreate Eclipse configuration files:
# gradle eclipserun Eclipse and import project, click right button on Project Explorer, select Import > Import... > Existing Projects into Workspace.
Change port on line:
IODevice device = new FirmataDevice("COM3");If you run on windows, set valid port definied as: COM1, COM2, COM3, ...
If you run app on linux, you must definied port have style: /dev/ttyUSB0, /dev/ttyUSB1, /dev/ttyUSB2, ...
Remember, port on your workstation is different than a port on Raspberry. When you move binary from workstation to Rasberry, you must change port number. Best practices is to store configuration in a external file.
links: