Fork me on GitHub

Raspberry Coffee Raspberry PI Java Scala Groovy Kotlin Clojure   

The best way to predict the future is to invent it. (Alan Kay)

Several Raspberry PI projects in Java, Scala, Groovy, Kotlin, and whatever knows about a JVM...

Important: This document goes along with the code at

A vast majority of the code availale to reach various components from the Raspberry PI is often written in Python, and next in C (using Wiring PI).
We will not argue here about which language is the best for your needs (this is close to a religious matter!), but in case you want to use Java or a JVM-aware language, this project is here to help. It gathers the code I have been using to reach various boards and devices. They all rely on PI4J.

The setup of the Raspberry PI is not Java related, but it is to be done anyway.

Now the Raspberry PI is good to go.
The projects below are mostly written in Java, using PI4J.
Some of the projects below come with their C equivalent, for those interested in compairing...
Interestingly, PI4J is based on Wiring PI (a C library), through javah.

Cool links
Fritzing, Autodesk Circuits.

Summary of the code snippets and libraries

Java Scala Groovy Kotlin Clojure

Real world projects
(Using the above)

Some possibly useful hints...

Different breakout boards - specially the I2C ones - have different endianness (little endian, big endian). This is usually clearly taken care of in the code. For example:

  public final static int LITTLE_ENDIAN = 0;
  public final static int BIG_ENDIAN    = 1;
  private final static int TCS34725_ENDIANNESS = BIG_ENDIAN;
and the methods used to read some several-byte words are written this way:

  private int readU16(int register) throws Exception {
    int lo = this.readU8(register);
    int hi = this.readU8(register + 1);
    int result = (TCS34725_ENDIANNESS == BIG_ENDIAN) ? (hi << 8) + lo : (lo << 8) + hi;
    // ...
    return result;
Notice above that the device this snippet of code is about (TCS34725) delivers two 8-bit words from two different registers (register and register + 1). Then those two 8-bit words (aka bytes) are arranged according to their endianness.
Some other devices (like the MCP9808) deliver 16-bit words (2 bytes) from one register. Then the two bytes are arranged according to their endianness.

  public int readU16BE(int register) throws Exception {
    final int TWO = 2;
    byte[] bb = new byte[TWO];
    int nbr =, bb, 0, TWO);
    if (nbr != TWO)
      throw new Exception("Cannot read 2 bytes from register 0x" + lpad(Integer.toHexString(register), "0", 2));
    if (verbose)
      System.out.println("I2C: 0x" + lpad(Integer.toHexString(bb[0]), "0", 2) + lpad(Integer.toHexString(bb[1]), "0", 2));
    return ((bb[0] & 0xFF) << 8) + (bb[1] & 0xFF);
In the i2c-tool package, there are several utilities proven very helpful: The i2cget happens to be quite helpful in this area. See the following command:

 Prompt> sudo i2cget -y 1 0x18 0x05 w
Without having to write any code, it will read on the bus 1, the device at the address 0x18, from register 0x05 a word (16 bits). Based on the value you expect, you would know whether to read one or two registers, and one or two bytes. Then you would know how to write your code.

Some useful commands...

 Prompt> route
 Prompt> sudo netstat -tunap
 Prompt> sudo iwlist wlan0 scan
 Prompt> pcmanfm --desktop-pref
 Prompt> apt-get remove motion
 Prompt> sudo dpkg-reconfigure console-setup
 Prompt> sudo iwlist --help
 Prompt> wpa_cli
 > scan
 > scan_results
 > add_network
 > set_network 4 ssid "RPiNet"
 > set_network 4 psk "Password"
 > quit
 Prompt> sudo apt-get install arp-scan
 Prompt> sudo arp-scan --localnet --interface=wlan0


Not strictly JVM related, but there is this project, targeting the same features as this one, but with node.js. You write your code in JavaScript. Very cool too.
Oliv did it