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 Raspbian

To install Raspbian, we need to get the Raspberry Pi ready for use. As the Raspberry Pi has no power on and off button, the powering of the Raspberry Pi will be done as the last step:

  1. At the bottom of the Raspberry Pi on the side you will see a slot to insert your MicroSD card. Insert the SD card with the connectors pointing to the board.
  2. Next, connect the HDMI or the composite connector and your keyboard and mouse. You won't need a network cable as we will be using the wireless functionality built into the Raspberry Pi.
  3. We will now connect the Raspberry Pi with the micro USB power supply.
  4. When the Raspberry Pi boots up, you will be presented with the OSes available to be installed. Depending on the download of NOOBS you have done, you will be able to see if the Raspbian OS is already available on the SD card or if it will be installed by downloading it. This is visualized by showing an SD card image or a network image behind the OS name. In the following screenshot, you see the NOOBS installer with the Raspbian image available on the SD card.
  5. At the bottom of the installation screen you will find the Language and Keyboard drop-down menus. Make sure you select the appropriate language and keyboard selection; otherwise, it will be difficult to enter the right characters on the command line and in other tools requiring text input.
  1. Select the Raspbian [RECOMMENDED] option and click the Install (i) button to start installing the OS:
  1. You will be prompted with a popup confirming the installation as it will overwrite any existing installed OSes. As we are using a clean SD card, we will not be overwriting any.
It is safe to press Yes to start the installation. This installation will take a couple of minutes, so it is a good time to go for a second cup of coffee.
  1. When the installation is done you can press Ok in the popup that appears and the Raspberry Pi will reboot. Because Raspbian is a Linux OS, you will see text scrolling by that relates to services that are being started by the OS.
  2. When all services are started, the Raspberry Pi will start the default graphical environment, called LXDE, which is one of the Linux window managers.

Configuring Raspbian

Now that we have installed Raspbian and have it booting into the graphical environment we can start configuring the Raspberry Pi for our purposes. To be able to configure the Raspberry Pi, the graphical environment has a utility tool installed, which eases up the configuration called Raspberry Pi Configuration:

  1. To open this tool, use the mouse and click on the Menu button on the top left, navigate to Preferences, and press the Raspberry Pi Configuration menu option, as shown in the following screenshot:
  1. When you click on the Raspberry Pi Configuration tool Menu option a popup will appear with the graphical version of the raspi-config command-line tool. In the graphical popup we see four tabs explaining different parts of possible configuration options. We first focus on the System tab, which allows us to:
    • Change the Password
    • Change the Hostname, which helps to identify the Raspberry Pi in the network
    • Change the Boot method to To Desktop or the CLI
    • Set the Network at Boot option
  1. With the system newly installed, the default username is pi and the password is set to raspberry. Because these are the default settings, it is recommended that we change the password.
  2. Press the Change Password button and enter a newly chosen password twice, Once to set the password and the second time to make sure we have entered the new password correctly.
  3. Press Ok when the password has been entered twice.
Try to come up with a password that contains capital letters, numbers, and some strange characters, as this will make it more difficult to guess.
  1. Now after we have set a new password, we are going to change the hostname of the Raspberry Pi. With the hostname we are able to identify the device on the network. I have changed the hostname into RASPI3JAVA, which helps me to identify the Raspberry Pi to be used for the book.
The hostname is used on the CLI, so you will immediately identify this Raspberry Pi when you log in.
  1. By default, the Raspberry Pi boots into the graphical user interface, which you are looking at right now. Because a future project will require us to make use of a display with our application, we will be choosing to boot into CLI mode.
  2. Click on the radio button that says To CLI. The next time we reboot, we will be shown the CLI.
  3. Because we will be using the integrated Wi-Fi connection on this Raspberry Pi, we are going to change the Network at Boot option to have it waiting for the network. Later on in the book we will be creating network-based services and we will be logging in from remote locations. Having the network ready before any service requiring a network connection is a good practice.
  4. Tick the box that says Wait for network.

We have set some default settings, which sets some primary options to help us identify the Raspberry Pi and change the boot mode. We will now be changing some advanced settings that enable us to make use of the hardware provided by the Raspberry Pi:

  1. Click on the Interface tab, which will give us a list of the available hardware provided. This list is as follows:
    • Camera: The Official Raspberry Pi camera interface
    • SSH: To be able to log in from remote locations
    • SPI: Serial peripheral interface bus for communicating with hardware
    • I2C: Serial communication bus mostly used between chips
    • Serial: The serial communication interface
    • 1-Wire: Low data power supplying bus interface conceptual based on I2C
    • Remote GPIO
  1. A future project that we will be working on will require some kind of camera interface. This project will be able to use both the local attached official Raspberry Pi Camera module as well as a USB connected webcam.
  2. If you have the camera module, tick the Enabled radio box behind Camera. We will be deploying our applications immediately from the editor. This means we need to enable the SSH option. By default this already is, so we leave the setting as it is. If the SSH option is not enabled, tick the Enabled radio button behind the SSH option. For now, you can leave the other interfaces disabled as we will only enable them when we need them.
  3. As we now have enabled the default interfaces that we will need, we are going to do some performance tweaking. Click on the Performance tab to open the performance options.
  4. We will not need to overclock the Raspberry Pi, so you can leave this option as it is. Later on in the book we will be interfacing with the Raspberry Pi's display and doing neat tricks with it. For this, we need some memory for the Graphical Processor Unit (GPU). By default this is set to 64 MB. We will ask for the largest amount of memory possible to be assigned to the GPU, which is 512 MB. Put 512 behind the GPU Memory option; there is no need to enter the text MB. The memory on the Raspberry Pi is shared between the system and the GPU. Having this option set to 512 MB results in 512 MB available for the system. I can assure you that this is more than sufficient.
  1. Now that we are done with the system configuration, we are making sure we can work with the Raspberry Pi. Click on the Localisation tab to show the options applicable to the location the Raspberry Pi resides in. We have the following options:
    • Set Locale: Where you set your locale settings
    • Timezone: The time zone you are currently in
    • Keyboard: The layout of the keyboard
    • WiFi Country: The country you will be making the Wi-Fi connection in
  1. This book uses the US-English language with a broad character set. Unless you prefer to continue with your own personal preferences, change the following by pressing the Set Locale button:
    • Language to en (English)
    • Country to US (USA)
    • Character Set to UTF-8
  1. Press the OK button to continue.

As this needs to build up the Locale settings this can take up to about 20 seconds to set up. You will be notified with a small window when this process is finished.

  1. The next step is to set the timezone. This is needed as we want to have time and dates shown correctly.
  2. Click on the Set Timezone button and select your appropriate Area and Location from the drop-down menus. When done, press the OK button.
  3. To make sure that the text we enter in any input field is correct, we are going to set the layout of our keyboard. There are a lot of layouts available, so you need to check yours. The Raspberry Pi is quite helpful and provides many keyboard options.
  4. Press the Set Keyboard button to open up a popup showing the keyboard options. Here, you are able to select your country and the keyboard layout available for this country. In my case, I have to select United States as the Country and the Variant as English (US, with euro on 5).
  5. After you have made the selection, you can test your keyboard setup in the input field below the Country and Variant selection lists. Press the OK button when you are satisfied with your selection.
  1. Unless you are connecting your Raspberry Pi to a wired network connection, we are going to set up the country we are going to make the Wi-Fi connection in so that we are able to connect remotely to the Raspberry Pi. Press the Set WiFi Country button to have the Wifi Country Code shown, which provides us the list of available countries for the Wi-Fi connection. Press the OK button after you have made the selection. We are now done with the minimal Raspberry Pi system configuration.
  2. Press OK in the settings window to have all our settings stored and press No in the popup that follows that says a reboot is needed to have the settings applied as, we are not completely done yet.
  3. Our final step is to set up the local Wi-Fi chip on the Raspberry Pi.

We will now set up the Wi-Fi on the Raspberry Pi. If you want your Raspberry Pi connected with a network cable, you can skip this section and head over to the Set fixed IP section:

  1. To set up the Wi-Fi, click on the Network icon, which is at the top of the screen between the Bluetooth and Speaker Volume icons.
  2. When you click this button, the Raspberry Pi will start to scan for available wireless networks. Give it a couple of seconds if your network does not appear immediately. When you see your network appear, click on it, and if your network is secured you will be asked to supply the credentials required to be able to connect to your wireless network.
If you have any trouble connecting to your wireless network without any messages, log in to your router and change the Wi-Fi channel to a channel lower than channel 11.
  1. When you have entered your credentials and pressed the OK button you will see the icon changing from the two network computers to the wireless icon, as it it trying to connect to the wireless network.
  2. Now that the Raspberry Pi has rebooted, we have configured the wireless network to make sure the wireless network will keep its connection. As the Raspberry Pi is an embedded device targeting low power consumption, the Wi-Fi connection is possible set to sleep mode if after a specific time there is no network usage.
  3. To make sure the Wi-Fi does not go into power sleep mode we will be changing a setting through the command line that will make sure that this won't happen. To open a CLI we need to open a terminal. A terminal is a window that will show the command prompt where we are able to provide commands.
  1. When you look at the graphical interface you will notice a small computer screen icon at the top in the menu bar. When we hover over this icon it shows the text terminal. Press this icon to open up a terminal.
  2. A popup will open with a large black screen showing the command prompt, as shown in the following screenshot:
Do you notice the hostname we set earlier? This is the same prompt as we will see when we log in remotely.
  1. Now that we have a command line open we need to enter a command to make sure the wireless network will not go to sleep after a period of no network activity. Enter the following in the terminal:
           sudo iw dev wlan0 set power_save off
  1. Press Enter. This command sets the power save mode to off so the wlan0 (wireless device) won't enter power save mode and will stay connected to the network.

We are almost done with setting up the Raspberry Pi. To be able to connect to the Raspberry Pi from a remote location we need to know the IP address of the Raspberry Pi. This final configuration step involves setting a fixed IP address into the Raspberry Pi settings:

  1. To open the settings for a fixed IP configuration, we are going to open up the settings by pressing the wireless network icon with the right mouse button and press the WiFi Networks (dhcpcdui) Settings option.
  2. A popup will appear with the settings we can change. As we will only change the settings of the Wi-Fi connection, we select Interface next to the Configure option. When interface is selected, we select the wlan0 option in the drop-down menu next to the interface selection.
If you have chosen to use a wired connection instead of the wireless connection you can select the eth0 option next to the interface option.
  1. We now have a couple of options available to enter IP address-related information. Please refer to the documentation of your router to find out which IP address is available to you which you can use. My advice is to only enter the IP address in the available fields, which leaves the other options automatically configured, like in the following screenshot. Notice that the entered IP address is the correct one; this only applies to my configuration, which could differ from yours:
  1. After you have entered the IP address you can click Apply and Close.
  2. It is now time to restart our Raspberry Pi and have it boot to the Command Line Interface (CLI).
  3. While it is rebooting, you will see a lot of text scrolling, which shows the services starting and at the end, instead of starting, the graphical interface, we are now shown the text-based CLI, as shown in the following screenshot:
  1. If you want to return to the graphical interface just type in the following command:
      startx
  1. Press Enter and wait a couple of seconds for the graphical user interface to appear again. We are now ready to install the Oracle JDK, which we will be using to run our Java applications.