Book Image

Raspberry Pi 3 Projects for Java Programmers

By : Rajdeep Chandra, John Sirach, Pradeeka Seneviratne
Book Image

Raspberry Pi 3 Projects for Java Programmers

By: Rajdeep Chandra, John Sirach, Pradeeka Seneviratne

Overview of this book

Raspberry Pi is a small, low cost and yet very powerful development platform. It is used to interact with attached electronics by the use of it's GPIO pins for multiple use cases, mainly Home Automation and Robotics. Our book is a project-based guide that will show you how to utilize the Raspberry Pi's GPIO with Java and how you can leverage this utilization with your knowledge of Java. You will start with installing and setting up the necessary hardware to create a seamless development platform. You will then straightaway start by building a project that will utilize light for presence detection. Next, you will program the application, capable of handling real time data using MQTT and utilize RPC to publish data to adafruit.io. Further, you will build a wireless robot on top of the zuma chassis with the Raspberry Pi as the main controller. Lastly, you will end the book with advanced projects that will help you to create a multi-purpose IoT controller along with building a security camera that will perform image capture and recognize faces with the help of notifications. By the end of the book, you will be able to build your own real world usable projects not limited to Home Automation, IoT and/or Robotics utilizing logic, user and web interfaces.
Table of Contents (8 chapters)
4
Integrating a Real-Time IoT Dashboard

Installing Java

In this section, we will be installing a file transfer application, installing Java, and preparing the NetBeans editor.

We will be using the Oracle JDK to run our Java applications. At the time of writing, the latest version is JDK 1.8, which all our projects will be using as a minimal version:

  1. To get the Oracle JDK, go to http://www.oracle.com/technetwork/java/javase/downloads/index.html and select the Download JDK button.
  2. On the following page you need to select the ARM version to be downloaded. Although the version you will be seeing now may differ from the version mentioned in the book, the naming scheme for the download is the same in all these versions. Even though the Raspberry Pi is now based on a 64-bit architecture the OS is still 32-bit at the time of writing. This also means the version you will need to download is: jdk-8u[buildnumber]-linux-arm32-vfp-hflt.tar.gz, which is the 32-bit version. The [buildnumber] indicates the release build of the JDK. All projects are targeted to work on JDK8, so any build number that is at least 91 or higher should be working. At the time of writing, the JDK version used is jdk-8u91-linux-arm32-vfp-hflt.tar.gz.
  3. To be able to use this Java version we will be using an application to upload the downloaded version to the Raspberry Pi. The application we will be using is called FileZilla and it is available for Windows, Linux, and Mac. To download this application go to https://filezilla-project.org/ and by clicking the Download FileZilla Client link, the application will be downloaded.
  1. To install FileZilla open the downloaded installer and follow the instructions on the screen. We will now upload this Java version to the Raspberry Pi. Open FileZilla, and you will be presented with the window shown in the following screenshot:
  1. To connect we need to enter the credentials that we have set during the installation of the Raspberry Pi. In the most upper part below the icons we will enter the credentials, which are as follows:
  • Only the IP address in the Host field
  • The username pi in the Username field
  • The password we have changed during preparation in the Password field
  • We enter port number 22 in the Port field, which is the secured connection
  1. Press the Quickconnect button, and when all the credentials are entered correctly we are logged in to the Raspberry Pi. On the left side in FileZilla we see the structure of our local computer, and on the right side we see the directory structure of the Raspberry Pi.
  2. Browse to the downloaded Java version on the left side of the application. When you see the Java version that we downloaded click and move this file from the left to the right side. The file will now be uploaded to the Raspberry Pi.
  3. In the most bottom part of FileZilla we see the upload progressing. When the upload is finished, the progress is removed from this list and the upload is done.

We can now close FileZilla and do the final steps required to install this Java version.

  1. For this, we will use the CLI on the Raspberry Pi. There are different ways to get to this command line. On Windows, we can use PuTTY. This is a small application that allows us to log in from the Windows PC onto the Raspberry Pi. On a Mac we can use the terminal application to log in. When we have our keyboard and display connected to the Raspberry Pi we are already logged in. As we will be using the Raspberry Pi as a remote device, we will be using PuTTY on Windows and the terminal on Mac. When you use Mac, you can skip the paragraph where we will be using the terminal.
  2. On Windows, we use PuTTY to connect to the Raspberry Pi. To use PuTTY we need to download it from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html.
  3. On the second half of this page, we see a list of files ready to be downloaded below the Binaries header.
  1. Click the putty.exe file to download. This file does not need to be installed, so we can save this file in any location that we can easily reach. When PuTTY is downloaded, double-click it to open it so that you see something similar to the following screenshot:
  1. With this window open we can enter the IP address of the Raspberry Pi in the Host Name field.
  2. We would like to save this IP address with a recognizable name, so we enter RASPI3JAVA without the quotes in the Saved Sessions field and press Save.
  3. We now have saved this session for future usage. When we now press Open we are prompted with Login as, where we enter the username pi and press Enter.
  4. The next step is to enter the password we have set during the preparation. When the credentials are entered correctly we are logged in.
  1. To log in on the Mac we use the terminal application, which is preinstalled on the Mac. We open this application by pressing command + space bar at the same time. This will open Spotlight Search, where we can type terminal without quotes and press return or Enter on the keyboard to open the terminal. Hold the option button while clicking on the green circle in the top-left corner to maximize the terminal window.
  2. We are now in the CLI on the Mac. To log in on the Raspberry Pi we need to enter the following command:
       ssh [email protected]

The connection we make is done with ssh, which is a small terminal application that creates a secure connection with the Raspberry Pi. We use pi as the username where the @ character literally means at because we log in as this user at the specified IP address. In the preceding command-line example, I have used the 192.168.1.20 IP address of my Raspberry Pi. You should replace this with the IP address you have set for yours. We are now asked to enter the password; enter the password that has been set for the user pi during the preparation.

From this point on it does not matter how we are logged in on the Raspberry Pi as the following steps are the same via Windows with PuTTY, Mac with terminal, or using the keyboard with a display attached to the Raspberry Pi. We are now presented with the CLI on the Raspberry Pi, which gives us the opportunity to unpack and install the Java package we have uploaded to the Raspberry Pi. We will place this installation in the /opt/ directory. To make this possible we first create the java directory inside the /opt/ directory by entering the following command:

    sudo mkdir /opt/java

We use the sudo command because this directory is owned by the root user, which is the administrator on the Raspberry Pi. The sudo command temporarily leverages our privileges to be an administrator for the duration of the command. With mkdir /opt/java we created the java directory inside the /opt/ directory. Now that we have the /opt/java directory we are going to unpack the downloaded Java archive file. We do this by entering the following command:

       sudo tar -C /opt/java -xf jdk-8u91-linux-
arm32-vfp-
hflt.tar.gz

Again we have used the sudo command to leverage our permissions. With the tar utility we unpack the Java archive into the /opt/java directory with the help of the -C parameter line switch. The command line parameters -xf tells the tar utility that we want to unpack the jdk-8u91-linux-arm32-vfp-hflt.tar.gz file. The filename in the preceding example is the version downloaded at the time of writing. Replace 91 with the version you have downloaded.

The Linux command line has a nice way to help you enter commands, directories, and filenames. When you have entered a part of the filename pressing the Tab key will auto-complete the filename.

We can now check if the files have been extracted correctly from the downloaded Java archive. By entering the following command we should see the directory extracted from the archive:

    ls -la /opt/java/

If you see the jdk1.8.0_91 directory, as shown the following screenshot, we have successfully unpacked Java. If you have a different version of the JDK, you will see a different number than 91 at the end: