Raspberry Pi with Java: Programming the Internet of Things (IoT)
Book Review - Raspberry Pi with Java: Programming the Internet of Things (IoT)
by Stephen Chin and James L. Weaver
< Buy On Amazon > (~ $21.64 USD)
Overview
I had the opportunity to read this book cover to cover and can confidently say that it provides an excellent primer for any hobbyist or professional developer getting started with the Raspberry Pi with a focus on Java development. Fundamentally, this book is a compilation of projects that demonstrate various integrations where Java can be used with the Raspberry Pi to produce some functional "product". Along the way each project gets increasingly more complex thus exposing the reader to more sophisticated integration capabilities and techniques. While some projects seem daunting, the authors meticulously walk the reader through all the necessary environment configuration settings, development environment configuration, and build steps.
To clarify what this book is not ... This book is not a programmer's reference guide. It is not intended to provide a comprehensive reference for available APIs and libraries. It is not an exhaustive textbook covering every development pattern and/or capability of Java on the Raspberry Pi platform. This book does not teach you Java programming. If you are new to Java, I would suggest getting some other books or online introductory courses to learn the basics of Java programming and then use this book to to get started with Java on the Raspberry Pi.
The book takes a fun and sometimes whimsical approach while sneaking in some educational instruction along the way. Personally, I prefer this type of example-based learning approach. I find that engaging in sample projects with concrete goals helps me better understand the subject at hand and seeing a completed project provides a more comprehensive end-to-end understanding.
Chapter 1: Baking Pi
DIFFICULTY: Beginner
If you are new to the Raspberry Pi platform, then this is the chapter for you. This chapter covers all the prerequisites required to power the Raspberry Pi, build a bootable SD (or microSD) card, installing the Linux-based Raspbian operating system, connecting the Raspberry Pi to the network, configuring default system settings and running your first "Hello World" Java application on the Pi. If you are a Raspberry Pi veteran, then you can probably skip this chapter; however, if you have not run a Java program on your Raspberry Pi before, then at least start on Page 24 with the section "Creating A Simple Raspberry Pi Application".
Chapter 2: Your First Java Project
DIFFICULTY: Beginner
This chapter focuses on a Java project that helps to brew the prefect cup of coffee. This project integrates with a USB scale attached to the Raspberry Pi. The Java program prompts the user through weighing just the right amount of coffee beans, adding a specific volume of water, and providing a countdown timer for the coffee extraction period. Who knew that making a good cup of "joe" requires such precise measurement, timing, and formulas!
The project demonstrates how to leverage the usb4java libraries to communicate with an external hardware scale and take measurements of weight in a Java program.
Chapter 3: Binary Timer
DIFFICULTY: Easy
This chapter is probably my favorite chapter. This chapter teaches the reader how to construct simple/basic circuits and interface to the GPIO (general purpose input/output) pins on the Raspberry Pi. Interfacing with the GPIO pins is probably one of the most powerful and rewarding experiences for a new Java programmer entering the world of embedded devices. Harnessing the GPIO pins allows a Java program to interact with the real/physical world. Buttons, LEDs, Sensors, Switches, Relays, etc can all be controlled or sensed via circuits that connect to the GPIO pins. Of course, since I started the Pi4J project it probably comes as no surprise that this is my favorite chapter :-)
The project in this chapter focuses on building a timer that displays its current countdown time in a binary pattern using LED lights, but the underlying objective is to teach the reader about GPIO inputs and outputs and some of the various nuances such as button debouncing. This chapter introduces both the open source Pi4J project and the OpenJDK DIO project as libraries to instrument and communicate from Java with the hardware GPIO interface. The authors do a nice job of explaining each of the libraries and the pros and cons of each. The book also provides instructions and example code using both libraries so you can familiarize yourself with each.
Please see the section titled "Recent Updates to Pi4J" at the end of this article to provide a bit of updated information on the Pi4J project since the time of this books writing.
Chapter 4: IoT Hat
DIFFICULTY: Easy
This chapter's project ostensibly recreates a magician's card trick of mind reading to determine the audience member's selected card from a stack of playing cards. This project uses an RFID reader connected to the Raspberry Pi hidden inside a magician's top hat and RFID tagged playing cards to accomplish the illusion. This chapter is really teaching you how to interface from the Raspberry Pi to a more sophisticated hardware device such as a RFID reader using each of the data communication busses on the Pi: Serial (UART), SPI (Serial Peripheral Interface), and I2C (Inter-Integrated Circuit). This chapter is important because communicating over each of these data busses significantly expands the world of possibilities for your project to interface with more complex chips and devices. While this chapter does not cover Java programming directly using these busses (it uses a NFC library project instead), it does at least familiarize you with the physical connections and the operating system level configurations required for each of the data busses. Java programming to communicate on these data busses is illustrated in later chapters.
This chapter suggests using the Raspberry Pi Model A+; however, the recent introduction of the Raspberry Pi Zero model may provide a smaller form factor and may be easier to disguise and affix to the hat.
Chapter 5: Line Runner
DIFFICULTY: Moderate
In this chapter we encounter our first foray into robotics! Here we build a robotic vehicle to autonomously navigate a line marked path using an array of sensors to determine if we are on or off course and sensors to determine distance to an object ahead to avoid a collision. This chapter teaches you how to use PWM (pulse-width-modulation) capabilities to control the drive motors of the robotic vehicle, GPIO pins to sense inputs from the line follower and distance sensors, and serial (UART) data communication to accept input from an infrared remote control.
Chapter 6: Tea Station
DIFFICULTY: Moderate
This chapter takes the coffee brewing project from the first chapter to the next level for brewing tea. This tea brewing project demonstrates using a serial connected scale with greater precision and adds a temperature sensor for measuring the temperature using a 1-Wire interface. The project also introduces our first JavaFX project where we can create a much improved graphical based user interface.
Chapter 7: Autonomous Drone
DIFFICULTY: Advanced
This chapter demonstrates using a Raspberry Pi and Java to add some degree of pre-programmed autonomous flight to a drone. The reader is introduced to the Parrot AR drone library and the autonomous4j library in this chapter.
Chapter 8: Retro Video Game Emulator
DIFFICULTY: Advanced
This chapter walks the reader through creating a complete portable gaming emulator platform using the Raspberry Pi. This chapter will really start to test your maker skills when it comes to soldering and wiring.
Chapter 9: NightHacking RetroPi
DIFFICULTY: Expert
The final chapter builds on the gaming emulator project created in Chapter 8 to create a 3D printed plastic case to mount all of the electronics and switches to finalize your Raspberry Pi based portable gaming system.
Conclusion
I think this is a great book for any Java programmer just getting started on the Raspberry Pi platform. The book does a good job of providing detailed and comprehensive instructions, wiring diagrams, and illustrations for each project. The only technical area that I wish the book took a deeper dive would be more detailed instruction and demonstration on using the SPI and I2C data bus communications from a Java program. This is a slightly more advanced topic but it does add a lot more extensibility for the would-be maker.
Follow Up: Recent Updates to Pi4J
The Pi4J project, like any open source project, continues to march forward with new features and improvements. Since the time of this book’s writing there are a few noteworthy updates to Pi4J to point out:
- GPIO Performance - We have significantly optimized the GPIO performance when using the standard Pi4J GPIO interfaces. It’s still slower than using the WiringPi wrapper API directly because there is still a fair bit of software validation and error handling logic behind the scenes in the Pi4J implementation but it is much improved over the 0.751 kHz benchmark using Pi4J and the 3.048 kHz benchmark using Device I/O (DIO) discussed on page 96. Using the same test code as the author with the same platform, a Raspberry Pi B+, we now achieve a benchmark of ~500 kHz using the normal Pi4J interfaces. That's a whopping 665% performance gain! Note, I did have to up the number of iterations in the test program to lengthen the sample time. I also independently validated the result using an Oscilloscope to measure the frequency generated on the GPIO pin. If you upgrade to the newer Raspberry Pi 2B you can get further improved benchmark of ~1000 kHz (1 MHz). The sample code I used for GPIO frequency testing can be found here: https://github.com/RaspberryPiWithJava/BinaryTimer/blob/master/BinaryTimerPi4J/src/com/nighthacking/binarytimer/TimerPi4J.java
- Pin Numbering Schemes - While the Pi4J/WiringPi numbering scheme is the default and most commonly used numbering scheme used in conjunction with Pi4J, the library also supports the Broadcom pin numbering scheme. An example program is included in the Pi4J Examples project that demonstrates how to use the Broadcom pin numbering scheme: https://github.com/Pi4J/pi4j/blob/develop/pi4j-example/src/main/java/BroadcomPinNumberingGpioExample.java#L38
- Platform Support - Chapter 3 mentions that Pi4J is exclusive to the Raspberry Pi and while that was true at the time of writing, we recently added support for additional platforms including the BananaPi and BananaPro platforms. We are also actively working to support the Odroid platform as well as the OrangePi platforms. Pi4J does depend on a WiringPi port for each of these platforms, so the platform support is still specialized for each platform and not a universal or generic platform implementation. A sample program demonstrating usage on the BananaPi platform can be viewed here: https://github.com/Pi4J/pi4j/blob/develop/pi4j-example/src/main/java/bananapi/GpioListenAllExample.java
- Serial (UART) Support - Pi4J now includes the full complement of serial configuration options for baud rate, parity, stop bits, data bits, etc. Please note that there unfortunately is a breaking API change in the new serial implementation starting with version 1.1-SNAPSHOT. The original serial API was just far too over-simplified for some of the real world needs of serial communications. We have updated the entire serial interface and underlying serial implementation to provide a much improved and more sophisticated serial communications support. An example program using the new Serial API can be viewed here: https://github.com/Pi4J/pi4j/blob/develop/pi4j-example/src/main/java/SerialExample.java
Title: Raspberry Pi with Java: Programming the Internet of Things (IoT)
Authors: Stephen Chin and James L. Weaver
Print Length: 336 pages
Publisher: McGraw-Hill Education; 1 edition (October 23, 2015)
Publication Date: October 23, 2015
Language: English
ISBN-13: 978-0071842013
ISBN-10: 0071842012
Disclaimer:
Thank You for your support.
- This book was provided to me at no cost for peer review purposes.
- This book includes topics on the Pi4J Project which is an open-source project that I founded and an actively engaged.
- Using the links on this page to purchase this item provides a small contribution to this site at no cost to you.