A comprehensive Java library for interfacing with Arduino boards through serial, Bluetooth, and Ethernet connections
npx @tessl/cli install tessl/maven-org-sintef-jarduino--org-sintef-jarduino@0.1.0A comprehensive Java library for interfacing with Arduino boards through serial, Bluetooth, and Ethernet connections. JArduino provides complete control over Arduino hardware including digital/analog I/O, PWM, interrupts, EEPROM operations, and tone generation, with both programmatic APIs and interactive GUI components.
pom.xml:<dependency>
<groupId>org.sintef.jarduino</groupId>
<artifactId>org.sintef.jarduino.core</artifactId>
<version>0.1.7-SNAPSHOT</version>
</dependency>
<!-- For serial communication -->
<dependency>
<groupId>org.sintef.jarduino</groupId>
<artifactId>org.sintef.jarduino.serial</artifactId>
<version>0.1.7-SNAPSHOT</version>
</dependency>
<!-- For GUI components -->
<dependency>
<groupId>org.sintef.jarduino</groupId>
<artifactId>org.sintef.jarduino.gui</artifactId>
<version>0.1.7-SNAPSHOT</version>
</dependency>
<!-- For Bluetooth on Android -->
<dependency>
<groupId>org.sintef.jarduino</groupId>
<artifactId>org.sintef.jarduino.bluetooth</artifactId>
<version>0.1.7-SNAPSHOT</version>
</dependency>import org.sintef.jarduino.*;
import org.sintef.jarduino.JArduinoProtocol;
import org.sintef.jarduino.comm.Serial4JArduino;
import org.sintef.jarduino.observer.JArduinoObserver;import org.sintef.jarduino.*;
// Create Arduino program by extending JArduino
public class BlinkExample extends JArduino {
// Constructor with serial port
public BlinkExample(String port) {
super(port);
}
// Setup method called once when program starts
@Override
protected void setup() {
// Initialize pin 13 as output for LED
pinMode(DigitalPin.PIN_13, PinMode.OUTPUT);
}
// Loop method called repeatedly while program runs
@Override
protected void loop() {
// Turn LED on
digitalWrite(DigitalPin.PIN_13, DigitalState.HIGH);
delay(1000); // Wait 1 second
// Turn LED off
digitalWrite(DigitalPin.PIN_13, DigitalState.LOW);
delay(1000); // Wait 1 second
}
public static void main(String[] args) {
// Create and start Arduino program
JArduino arduino = new BlinkExample("COM3");
arduino.runArduinoProcess();
}
}JArduino uses a layered architecture with clear separation of concerns:
JArduinoProtocol provides a factory for creating Arduino command messages as FixedSizePacket objectsSerial4JArduino, AndroidBluetooth4JArduino, Udp4JArduino) handle Arduino communicationJArduinoObserver/JArduinoSubject interfaces enable asynchronous message handlingThe protocol uses binary messaging with start/stop bytes and escape sequences for reliable communication across all transport types.
Core JArduino programming interface providing Arduino-like methods for pin control, analog I/O, timing, and advanced features. This is the primary API users interact with by extending the JArduino class.
// Main JArduino class - extend this for Arduino programs
abstract class JArduino extends AbstractJArduino {
// Constructors
JArduino(); // Default serial
JArduino(String id); // Serial with ID
JArduino(String id, JArduinoCom com); // Custom communication
JArduino(JArduinoCom com); // Communication only
JArduino(String id, JArduinoCom com, ProtocolConfiguration prot);
// Abstract methods to implement
protected abstract void setup(); // Called once at start
protected abstract void loop(); // Called repeatedly
protected void interrupt0(); // Override for interrupt handling
protected void interrupt1(); // Override for interrupt handling
// Process control
void runArduinoProcess(); // Start the Arduino program
void stopArduinoProcess(); // Stop the Arduino program
// Timing utilities
void delay(long millis); // Sleep for milliseconds
int map(int value, int l, int h, int nl, int nh); // Map value between ranges
}
// Inherited methods from AbstractJArduino available in JArduino
abstract class AbstractJArduino {
// Digital pin operations
void pinMode(DigitalPin pin, PinMode mode);
void digitalWrite(DigitalPin pin, DigitalState value);
DigitalState digitalRead(DigitalPin pin); // Synchronous read with timeout
// Analog operations
void analogReference(AnalogReference type);
short analogRead(AnalogPin pin); // Synchronous read with timeout
void analogWrite(PWMPin pin, byte value); // PWM output 0-255
// Advanced digital features
void tone(DigitalPin pin, short frequency, short duration);
void noTone(DigitalPin pin);
int pulseIn(DigitalPin pin, DigitalState state, long timeout);
// Interrupt handling
void attachInterrupt(InterruptPin interrupt, InterruptTrigger mode);
void detachInterrupt(InterruptPin interrupt);
// EEPROM operations
byte eeprom_read(short address); // Synchronous read with timeout
void eeprom_write(short address, byte value); // Asynchronous write
boolean eeprom_sync_write(short address, byte value); // Synchronous write with ack
// Communication testing
boolean ping(); // Test Arduino connection with timeout
}
// Communication types
enum JArduinoCom { Serial, Ethernet, AndroidBluetooth }Foundation protocol factory and Arduino hardware abstraction including pin definitions, states, and command creation. Essential for all Arduino communication.
// Arduino hardware enums - complete definitions
enum DigitalPin {
PIN_0, PIN_1, PIN_2, PIN_3, PIN_4, PIN_5, PIN_6, PIN_7,
PIN_8, PIN_9, PIN_10, PIN_11, PIN_12, PIN_13,
A_0, A_1, A_2, A_3, A_4, A_5;
byte getValue();
static DigitalPin fromValue(byte b);
}
enum AnalogPin {
A_0, A_1, A_2, A_3, A_4, A_5;
byte getValue();
static AnalogPin fromValue(byte b);
}
enum PWMPin {
PWM_PIN_3, PWM_PIN_5, PWM_PIN_6, PWM_PIN_9, PWM_PIN_10, PWM_PIN_11;
byte getValue();
static PWMPin fromValue(byte b);
}
enum PinMode {
INPUT, OUTPUT;
byte getValue();
static PinMode fromValue(byte b);
}
enum DigitalState {
LOW, HIGH;
byte getValue();
static DigitalState fromValue(byte b);
}
enum AnalogReference {
DEFAULT, INTERNAL, EXTERNAL;
byte getValue();
static AnalogReference fromValue(byte b);
}
enum InterruptPin {
PIN_2_INT0, PIN_3_INT1;
byte getValue();
static InterruptPin fromValue(byte b);
}
enum InterruptTrigger {
CHANGE, RISING, FALLING, LOW;
byte getValue();
static InterruptTrigger fromValue(byte b);
}
// Main protocol factory
class JArduinoProtocol {
static FixedSizePacket createPinMode(DigitalPin pin, PinMode mode);
static FixedSizePacket createDigitalWrite(DigitalPin pin, DigitalState value);
static FixedSizePacket createDigitalRead(DigitalPin pin);
static FixedSizePacket createAnalogRead(AnalogPin pin);
static FixedSizePacket createAnalogWrite(PWMPin pin, byte value);
}Serial port communication for connecting to Arduino boards over USB or RS-232. Provides automatic port discovery and cross-platform native library support.
class Serial4JArduino implements JArduinoClientObserver, JArduinoSubject {
Serial4JArduino(String port);
void receiveMsg(byte[] msg);
void register(JArduinoObserver observer);
void close();
static HashSet<CommPortIdentifier> getAvailableSerialPorts();
static String selectSerialPort();
}Android Bluetooth communication enabling Arduino control from mobile applications. Supports automatic device pairing and connection management.
class AndroidBluetooth4JArduino implements JArduinoClientObserver, JArduinoSubject, Runnable {
AndroidBluetooth4JArduino(AndroidBluetoothConfiguration config);
void setAndroidBluetoothSocket(BluetoothSocket socket);
void receiveMsg(byte[] msg);
void register(JArduinoObserver observer);
void run();
void close();
}Swing-based graphical components for interactive Arduino control with visual pin layouts, control panels, and real-time monitoring capabilities.
class InteractiveJArduinoDataControllerClientAdvanced implements JArduinoObserver, JArduinoClientSubject {
void sendpinMode(PinMode mode, DigitalPin pin);
void senddigitalWrite(DigitalPin pin, DigitalState value);
void senddigitalRead(DigitalPin pin);
void sendanalogRead(AnalogPin pin);
void sendanalogWrite(PWMPin pin, byte value);
void sendping();
}Observer pattern implementation for asynchronous Arduino message handling with strongly-typed message classes and automatic parsing.
interface JArduinoObserver {
void receiveMsg(byte[] msg);
}
interface IJArduinoMessageHandler {
void handleDigitalReadResult(DigitalReadResultMsg msg);
void handleAnalogReadResult(AnalogReadResultMsg msg);
void handlePong(PongMsg msg);
void handleInterruptNotification(InterruptNotificationMsg msg);
}Specialized Arduino functionality including interrupts, tone generation, pulse measurement, and EEPROM operations for advanced hardware control.
// Advanced protocol operations - typically used through JArduino methods
static FixedSizePacket createAttachInterrupt(InterruptPin interrupt, InterruptTrigger mode);
static FixedSizePacket createDetachInterrupt(InterruptPin interrupt);
static FixedSizePacket createTone(DigitalPin pin, short frequency, short duration);
static FixedSizePacket createNoTone(DigitalPin pin);
static FixedSizePacket createPulseIn(DigitalPin pin, DigitalState state);
static FixedSizePacket createEeprom_read(short address);
static FixedSizePacket createEeprom_write(short address, byte value);
static FixedSizePacket createEeprom_sync_write(short address, byte value);
static FixedSizePacket createPing();// Core protocol packet for Arduino communication
abstract class FixedSizePacket {
byte[] getPacket(); // Get binary packet data
String toString(); // String representation
void setSourceAddress(byte address); // Set source address
byte getSourceAddress(); // Get source address
void setTargetAddress(byte address); // Set target address
byte getTargetAddress(); // Get target address
byte[] getRawData(); // Get raw packet data
}
// Configuration classes
abstract class ProtocolConfiguration {
// Base configuration class
}
class AndroidBluetoothConfiguration extends ProtocolConfiguration {
AndroidBluetoothConfiguration(BluetoothSocket socket);
BluetoothSocket getmSocket();
}
// Observer pattern interfaces for message handling
interface JArduinoObserver {
void receiveMsg(byte[] msg);
}
interface JArduinoSubject {
void register(JArduinoObserver observer);
void unregister(JArduinoObserver observer);
}
interface JArduinoClientObserver extends JArduinoObserver {
// Client-specific observer interface
}