Just under thirty years ago, in 1982, the BBC launched a television series called, “The Computer Programme”. Since this was before the launch of the IBM PC it was intended to introduce people to computers and teach them how to program in BASIC. As part of this, a company you may not have heard of called Acorn developed a personal computer called the BBC Micro. You will, however, probably know the company that Acorn became: it’s now called ARM. Although ARM doesn’t actually manufacture processors, its designs are used in literally billions of phones, tablets and other devices around the world.
A few years ago a number of academics in the UK started a campaign to improve the way computing and computer science was taught to children in British schools. As part of the National Curriculum all children study ICT (information and communication technology). This has mostly been learning how to use office automation software like a word processor and some exposure to web page authoring. The academics wanted children to understand more about how computers work and how to program them rather than just how to use them. They wanted to bring back the ideas and the excitement of the BBC micro, but for the Internet age.
Out of this came a project to create a cheap computer that would allow children (and adults) to write programs and learn about interfacing to things like LEDs, switches and motors. This became the Raspberry Pi Foundation and the end result was the single board computer called the Raspberry Pi (Fig. 1) that uses an ARM-based processor. After several years of development the board was launched on February 29th 2012 with an initial run of 10,000 boards. Demand was so high that the websites of the two distributors in the UK, Farnell and RS, were both rendered inaccessible, effectively through a denial-of-service attack by eager customers! Since then over a million boards have been sold at the incredibly low price of $35 each.
The Raspberry Pi board, as shown above provides processing, memory and input/output facilities. What is particularly clever about the design of the Pi is how it keeps the cost of the additional components very low. Let’s look at these:
Cooling: Take the side off a modern PC clocked at more than 3GHz and you’ll find several fans and a sizeable heat sink attached to the processor. ARM processors are extremely good at using very little electrical power and therefore not generating lots of heat. Even under heavy load you can put your finger on the main chip of the Pi and you’ll find it warm, but certainly not burning hot. So, no heat sink and no fans required.
Power: Laptops and PCs need power supplies that can deliver hundreds of Watts and have both 5 V and 12 V connections. The low-power ARM architecture means that the Raspberry Pi can run off a single 5 V supply, which can be obtained directly from a USB connection. Although the current required (about 800 mA) is above what the USB specification defines many laptops, PCs and powered hubs will happily provide enough current. The power supply cost can be just that of a USB cable.
Storage: PCs and laptops use hard drives or solid-state drives; the Raspberry Pi uses an SD card to boot from and provide a file system. If you don’t have a spare 4 Gbyte card (the recommended minimum size) lying around, you can get one for about $6. Read and write speeds are both adequate for most situations.
Display: The Raspberry Pi provides HDMI and composite video output, so any modern TV will make a great monitor. Add a cheap USB keyboard and you’re all set to go.
Technically, the Raspberry Pi has about the same processing power as a typical desktop PC did in the mid-1990s; the processor is clocked by default at 750 MHz. That might not sound like much but remember, back then we were still running spreadsheet programs, graphical design apps and many other complex things. They just ran a bit slower and didn’t have quite so many features as the ones we use today. The Raspberry Pi has 512 Mbytes of memory, so more like a desktop machine from about ten years ago. Since it’s intended for school children this is more than adequate for most applications they are likely to develop. In addition, buried in the single-on-chip (SoC) package is a really quite impressive GPU; many people use the Raspberry Pi as a very low cost media server.
What Is Java?
The most important thing to understand when answering this question is that Java is not one thing, but many different aspects of a comprehensive software platform. Primarily there is what’s called the Java Platform, Standard Edition (Java SE) which consists of the Java language syntax, a rich set of class libraries to simplify application development and the Java Virtual Machine (JVM).
The majority of computer languages are compiled into instructions that are specific to the machine the application is destined to run on: Windows, Mac OSX or Linux for example. This means that in order to run an application on more than one type of machine you have to compile the application (potentially also making changes) for each platform to be supported. A Java application is compiled to bytecodes that represent the instruction set of a virtual, rather than real machine architecture. The JVM takes these bytecodes and converts them as the application is running to the necessary native instructions and system calls. People often think that this extra processing makes Java slower than native applications. That was certainly true when Java was first launched, but with advanced techniques like adaptive compilation and aggressive method inlining it’s actually possible to run certain types of applications faster than natively compiled ones. This is what is described in marketing terms as, “Write once, run anywhere”.
Java was first released in 1995, which means it was originally designed to work on machines that had the same processing power and considerably less memory than the Raspberry Pi. Over the years, Java has also been adapted to different application types and deployment platforms so, in addition to Java SE there is Java Card (a very specialised form of Java designed to run on smart cards with only kilobytes of memory), Java Enterprise Edition (Java EE) for server side applications and Java Mobile Edition (Java ME) for small and embedded applications.
In order to convert bytecodes to the right native instructions the JVM must be ported to each combination of operating system and processor architecture. Partially because of the popularity and cost of the Raspberry Pi, Oracle decided to make it a reference platform for Linux running on ARM processors. In addition to porting the JVM changes were made for specific hardware components like the GPU and the fact that the Raspberry Pi has hardware floating point acceleration (which is not mandated by the ARM v5 architecture used in the Raspberry Pi). The result is an implementation of the Java Development Kit 7 (JDK 7) that is tailored to the Raspberry Pi. Oracle also provides a port of Java ME Embedded (version 3.3) for the Raspberry Pi. This is a version of the Java environment that can run in less than a megabyte of memory, but has a reduced set of class libraries.
To develop graphical or user interface applications you can use the JavaFX library. This is an extension to Java SE that provides a rich set of libraries for these types of applications. The runtime has been extended to take advantage of graphics hardware acceleration (through direct use of the GPU) and this has also been tuned for the Raspberry Pi. This is available as early access until the release of JDK8.
Why Is Java Good For Embedded Applications and the Raspberry Pi
There are numerous reasons that make Java an attractive platform for developing embedded type applications using the Raspberry Pi:
The very comprehensive set of included class libraries. Java SE 7 has almost 4000 class libraries covering everything from data structures to concurrency to networking. This saves a lot of time by not having to keep re-inventing the wheel.
Unlike languages like assembler, C and C++, which are typically used for embedded development, Java does not use explicit pointers. This means that you cannot manipulate the address in memory that you want to access, either accidentally or on purpose. This does two things: one, it reduces the number of bugs in code through incorrectly accessing memory, ad two, it prevents people from using common techniques like buffer overruns to bypass security checks and develop malicious code.
Java was also developed to help developers with what could be considered mundane activities like memory management. Again, unlike C and C++ Java does not require a programmer to remember to explicitly free memory when they no longer require it. This is how many memory leaks occur. Java uses a garbage collector that detects when memory can be reclaimed and does so automatically.
Almost all universities teach Java as part of under-graduate computer science courses so that students can understand object-oriented programming principles. Combined with the huge popularity of Java for all types of applications this means that there are literally millions of developers around the world who know how to program in Java.
Java has great tools for developing code. Aside from commercial products there are two free and open source integrated development environments (IDEs): NetBeans and Eclipse. Developing applications with these substantially reduces the amount of time and effort required using facilities like automatic code completion, syntax checking as you type and many other great features. It is simple to configure your environment so you can develop the code for your application on a desktop or laptop machine and quickly deploy it to the Raspberry Pi.
How Do I Get Started?
Oracle has worked with the Raspberry Pi foundation to make Oracle’s production-grade Java SE implementation available as part of the standard Raspberry Pi Linux images. This makes life simpler for people as they do not need to download extra software and configure things to get started.
Alternatively, if you want to experiment with what’s currently in development you can get Early Access versions of both JDK8 for Embedded or Java ME Embedded 8 including installation instructions for Java ME Embedded 8. JDK 8 also includes the JavaFX 8 library so you can develop graphical applications.
One of the core design features of the Raspberry Pi is the ability to connect it to external devices using standard hardware level interfaces like GPIO, I2C and SPI. It also provides a serial interface through a UART. In the case of Java ME Embedded an API is included called the Device Access API, which makes controlling these from Java very straightforward. Java SE Embedded does not include this API, but there is a separate library available called Pi4J. To use the serial port you can install another library, RXTX using the command: sudo apt-get install librxtx-java.
What Kind of Things Can You Do With Java and the Raspberry Pi?
Despite being small and cheap the Raspberry Pi is a formidable piece of hardware capable of controlling pretty much anything you can think of. Here are some examples of things people have done:
Deutsche Telekom connected a Raspberry Pi to a Nespresso coffee machine. They were then able to use additional Java APIs on the server to provide voice control from a mobile phone. Some of my colleagues at Oracle built a ‘Dukepad’. This is a portable device containing a Raspberry Pi, a touch sensitive screen and an accelerometer. The software uses Java and JavaFX to deliver a tablet interface.
Some of the same team also built a robot, controlled by a Raspberry Pi and programmed in Java to play chess (see JavaOne 2013 Chess Robot). Angela Caicedo (also from Oracle) used a touchscreen and an accelerometer to build a system that handles parallax for viewing three-dimensional displays as the screen is tilted (see Beyond Beauty: JavaFX, I2C, Parallax, Touch, Raspberry Pi, Gyroscopes and Much More. (Part I)).
I connected a cheap EEG monitor to a Raspberry Pi and used it to control a LEGO motor, which (in theory) moved a pointer to indicate how hard (or not) I was concentrating (see Mind Reading with the Raspberry Pi). My most recent project involved connecting a Raspberry Pi to my car and, in conjunction with a touch screen and accelerometer, display a variety of real-time data about the car’s performance (see The Raspberry Pi JavaFX In-Car System (Part 1)).
What Do I Do Now?
The next step is to let your imagination run wild in terms of ideas for applications. There are already a host of add-on boards for the Raspberry Pi that provide facilities like analog-to-digital input as well as motor and relay controls. All of these can be accessed quickly and easily from Java applications and you don’t even need to know a lot about electronics to start building sophisticated embedded applications.
The Raspberry Pi hardware platform with the Java software platform is clearly a winning combination.