Java code and wiring for the Raspberry PI, featuring reusable libraries and snippets
It uses the PI4J library.
This project contains Java code, mostly translated from Python, dedicated to usually one board (like BMP180, LSM303, etc). More consistent samples can be found in the RasPISamples project, where several components have been assembled together. Do take a look, it also comes with a readme file.
Java code is compiled into
class files, that run on a Java Virtual Machine (
JVM). Java is not the only language that runs a
JVM, this project also contains some small samples of
other JVM-aware languages, invoking and using the features of this project.
Those samples include Scala, Groovy, Kotlin..., and the list is not closed!
See in the OthetJVM.languages directory.
This project uses
Gradle will be installed automatically if it is not present on your system,
it uses the gradle wrapper (
Git is usually installed on Linux and Mac, but not on Windows. On Windows, you need to install the
git bash shell, and run in it the commands mentioned in this document.
To build it, clone this project (this repo), make sure the script named
gradlew is executable, and execute
Prompt> git clone https://github.com/OlivierLD/raspberry-pi4j-samples.git Prompt> cd raspberry-pi4j-samples Prompt> chmod +x gradlew Prompt> ./gradlew [--daemon] build
You are expecting an end like that one:
BUILD SUCCESSFUL in 55s 97 actionable tasks: 17 executed, 80 up-to-date Prompt>
gradle web site for info about Gradle.
We will also be using the
shadowJar gradle plugin is several projects.
This plugin is aggregating the required classes and all their dependencies into a single archive, called a
fat Jar. This simplifies the syntax of the
Typically, this operation will be run like this:
Prompt> cd RESTNavServer Prompt> ../gradlew shadowJar
The expected archive will be produced in the local
Important : If
JAVA_HOMEis not set at the system level, you can set it in
set.gradle.envand execute it before running
Prompt> . ./set.gradle.env
Note: If you are behind a firewall, you need a proxy. Mention it in all the files named
gradle.propetries, and in all the
build.gradle scripts, uncomment the following two lines:
// ant.setproxy(proxyhost: "$proxyHost", proxyport: "$proxyPort") //, proxyuser="user", proxypassword="password") // compileJava.dependsOn(tellMeProxy)
Developing on the Raspberry PI, or Developing for the Raspberry PI ?
To write code, the simplest editor is enough. I have used
vi for ages, mostly because this was the only one available, but also because it is indeed good enough.
vi is available on the Raspberry PI,
nano too, graphical editors like
geany are even easier to use, on a grahical desktop.
All the code provided here can be built from Gradle (all gradle scripts are provided), on the Raspberry PI itself. The Raspberry PI is self sufficient, if this is all you have, nothing is preventing you from accessing all the features presented here.
But let us be honest, Integrated Development Environments (IDE) are quite cool. In my opinion, IntelliJ leads the pack, and Eclipse, JDeveloper, NetBeans follow. Cloud9 provides amazing features, on line. Smaller ones like GreenFoot, BlueJ are also options to consider.
Those two last ones might be able to run on a Raspberry PI, but forget about the others..., they use way too much RAM. The features they provide definitely increase productivity, and when you use them, you learn as you code. Code-insight, auto-completion and similar features are here to help. And I'm not even talking about the remote debugging features they provide as well.
So, as the Raspberry PI is not the only machine on my desk, I develop on a laptop using IntelliJ (with several GigaBytes of RAM, like 8, 16, ...), and I use
scp to transfer the code to (and possibly from) the Raspberry PI.
Worst case scenario, I do a
git push from the development machine, and a
git pull from the Raspberry PI.
I found it actually faster and more efficient than developing directly on the Raspberry PI.
Something to keep in mind
The Java Virtual Machine (JVM) implements the Java Platform Debugging Architecture (JPDA). This allows remote debugging. In other words, you run the code on the Raspberry PI, but you debug it (set breakpoints, introspect variable values, etc) on another machine (the one where the IDE runs). This is specially useful when the code interacts with sensors and other devices that are not supported from the laptop. This will make your life considerably easier than if you used another language missing it (like Python, C, and many others). It uses TCP between the debugger and the debuggee.
Raspberry PI, a possible thing of the Internet of things...
- The Raspberry PI is a fully featured Linux computer, which can - as such - connect to the Internet.
- The Raspberry PI has a General Purpose Input Output (GPIO) interface that allows it to drive all kind of electronic components, from a simple LED to a complex robot, and including all kind of sensors (GPS, light resistors, pressure sensors, temperature sensors, all kinds!). None of the above is new. Connecting to the Internet does not impress anyone anymore. Driving a robot, modern kitchens are full of robots, cars are loaded with electronic components... But what if we put those two together, with the Raspberry PI sitting in between. Then, we can drive a robot over the Internet. And this is not that usual (yet).
The snippets provided in this project are here to help in this kind of context. Some will use the network aspect of the story, some others will interact with electronic components. The two aspects should be easy to bridge, that is the goal. If that was not the case, please let me know (email address of the left side).
Several projects are featured here:
- Basic GPIO interaction
- Two Leds
- Use the Raspberry PI to turn LEDs on and off, through email (with doc)
- Read Serial Port (with doc)
- Read and parse NMEA Data from a GPS (with doc)
- Read analog data with an Analog Digital Converter (ADC). (with doc, with node.js and WebSocket)
- Drive servos using the PCA9685. (with doc).
- Drive servos using the PCA9685, over the Internet, with an Android client option. (with doc).
- Use the LSM303. (I2C compass & accelerometer, with doc).
- Use the BMP180. (I2C temperature and pressure sensor, with doc).
- Use the BMP183. (SPI temperature and pressure sensor, with doc).
- Use a relay, through email. (with doc).
- Use a relay, through HTTP. (with doc).
- Use a seven-segment display. (with doc).
- Use the VCNL4000 (I2C proximity sensor).
- Use the TCS34725 (I2C color sensor, demo).
- Use the TSL2561 (I2C light sensor).
- Use the L3GD20 (I2C gyroscope, demo).
- Use the MCP4725 (I2C Digital to Analog Converter, demo).
- ... and more.
All the doc - with more details than here - can be reached from this page.