Raspberry Pi 3 Projects for Java Programmers - Pradeeka Seneviratne - E-Book

Raspberry Pi 3 Projects for Java Programmers E-Book

Pradeeka Seneviratne

0,0
31,19 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 216

Veröffentlichungsjahr: 2017

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Title Page

Raspberry Pi 3 Projects for Java Programmers
Get the most out of your Raspberry Pi 3 with Java
Pradeeka Seneviratne
John Sirach

BIRMINGHAM - MUMBAI

Copyright

Raspberry Pi 3 Projects for Java Programmers

Copyright © 2017 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: May 2017

Production reference: 1300517

Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.

ISBN 978-1-78646-212-1

www.packtpub.com

Credits

Authors

Pradeeka Seneviratne

John Sirach

Copy Editor

Safis Editing

Reviewer

Rajdeep Chandra

Project Coordinator

Kinjal Bari

Commissioning Editor

Pratik Shah

Proofreader

Safis Editing

Acquisition Editor

Prachi Bisht

Indexer

Rekha Nair

Content Development Editor

Trusha Shriyan

Graphics

Kirk D'Penha

Technical Editor

Varsha Shivhare

Production Coordinator

Shantanu Zagade

Foreword

About the Authors

Pradeeka Seneviratne is a software engineer with over 10 years of experience in computer programming and designing systems. He is an expert in the development of Arduino and Raspberry Pi-based embedded systems.

Pradeeka is currently a full-time embedded software engineer who works with embedded systems and highly scalable technologies. Previously, he worked as a software engineer for several IT infrastructure and technology servicing companies.

He collaborated on the Outernet (free data from space, forever) project as a volunteer hardware and software tester for Lighthouse, and Raspberry Pi-based DIY Outernet receivers based on Ku band satellite frequencies.

He is also the author of three books:

Internet of Things with Arduino Blueprints

by Packt Publishing

IoT: Building Arduino-Based Projects

by Packt Publishing

Building Arduino PLCs

by Apress

John Sirach works as a product owner at Greenhouse Innovation. He has more than 10 years of experience in Internet-related disciplines from connectivity to hosting, and Internet of Things. Currently, he is involved in the open source PiDome home automation platform project as a passionate Java and JavaFX software developer and project maintainer.

In the past ten years, he has gained experience with large-scale web applications committed to online services with most experience gained in frontend web development and application middleware.

www.PacktPub.com

For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.comand as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www.packtpub.com/mapt

Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

Why subscribe?

Fully searchable across every book published by Packt

Copy and paste, print, and bookmark content

On demand and accessible via a web browser

Customer Feedback

Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1786462125.

If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!

Table of Contents

Credits

About the Authors

Preface

What this book covers

What you need for this book

Who this book is for

Conventions

Reader feedback

Customer support

Downloading the example code

Downloading the color images of this book

Errata

Piracy

Questions

Setting up Your Raspberry Pi

Getting started with the Raspberry Pi

Getting a compatible SD card

Preparing and formatting the SD card

Installing Raspbian

Configuring Raspbian

Installing Java

Installing and preparing the NetBeans Java editor

Our first remote Java application

Running our application on the Raspberry Pi

Summary

Automatic Light Switch Using Presence Detection

Introduction to and installing Fritzing

Billing of materials

How to emulate reading analog values on digital pins

Starting our project and installing the necessary libraries

The Pi4J libraries

Adding the HD44780-compatible 16x2 character display

Showing data on the HD44780-compatible display

Adding the light-dependent resistor to the setup

Reading and displaying the values from the LDR

Using digital out to switch and display a relay status

Automatic switch based on environment lighting

Using the Bluetooth chip on the Raspberry Pi

Bluetooth device discovery

Putting it all together, our first automation project

Summary

A Social and Personal Digital Photo Frame

Bill of materials

Waveshare HDMI display

Assembling with Raspberry Pi

Selecting video source

Correcting display resolution

Mounting on desktop

Connecting with Flickr

Obtaining a Flickr API key

Creating an album

Finding Flickr photoset_id

REST request format

Invoking flickr.test.echo

Invoking flickr.photosets.getPhotos

Constructing photo source URL

Writing Java program

Accessing Flickr image URL

Installing feh on Raspberry Pi

Scheduling your application

Writing shell script for Java application

Testing the digital_photo_frame.sh with the terminal

Scheduling digital_photo_frame.sh with crontab

Testing digital_photo_frame.sh with crontab

Writing shell script for slideshow

Starting digital photo frame on Raspberry Pi boot

Photo frame in action

Summary

Integrating a Real-Time IoT Dashboard

Adafruit IO

Bill of materials

Sign in with Adafruit IO

Finding your AIO key

Creating news feed

Understanding topics

Creating a dashboard

Creating a block on a dashboard

Raspberry Pi and I2C pins

Connecting an I2C-compatible sensor to the Raspberry Pi

Serial bus addresses

Configuring the Raspberry Pi to use I2C

Searching I2C devices attached to the Raspberry Pi

Accessing I2C with Pi4J

Eclipse Paho Java client

Writing Java program to publish data to a feed

Publishing temperature sensor data

Publishing system information

Subscribing to a feed

Creating a toggle button on Adafruit dashboard

Subscribe to the button feed

Controlling an LED from button feed

Summary

Wireless Controlled Robot

Prerequisites

The Zumo chassis kit

Assembling Zumo chassis

Preparing motors to reducing the effects of electrical noise

Attaching Raspberry Pi to Zumo chassis

Building the circuit

Wiring them together

Moving and turning

Moving

Turning

Swing turn

Writing your Java program

Running and testing your Java program

Summary

Building a Multipurpose IoT Controller

Prerequisites

Preparing your Raspberry Pi board

Installing and configuring Jetty servelet engine

Writing your first Java web application

Creating a Maven project from Archetype

Creating a servlet

Copying iot.war file to the Raspberry Pi

Summary

Security Camera with Face Recognition

Raspberry Pi camera module

Connecting the camera module to the Raspberry Pi

OpenCV

Downloading and installing OpenCV on Windows

Creating the Java project

Adding the OpenCV library to your Java project

Downloading and building OpenCV on Raspberry Pi

Working with video

Facial recognition

Build and run

Summary

Preface

As Java becomes widely used on different hardware platforms from computers to embedded devices, Raspberry Pi has no limitations to work with it to gain full power of Java. This book presents some basic to advanced projects that can be used to build Raspberry Pi 3 projects with Java as the core development platform.

What this book covers

Chapter 1, Setting up Your Raspberry Pi, teaches you about the hardware available with Raspberry Pi and how to prepare to utilize it from the installed Java Virtual Machine. Setting up the NetBeans editor to be able to write Java applications, which can be deployed from the editor, including the in-editor, available console to be able to interact with Java applications on Raspberry Pi.

Chapter 2, Automatic Light Switch Using Presence Detection, explains how to perform analogue readings using digital pins, as the Raspberry Pi has no analog reading capabilities. By adding a 16x2 character display, you will visualize these readings. By adding logic to the code, you will be able to determine when someone is present, and in combination with detecting light levels, you will be able to set a relay state that turns a light on or off.

Chapter 3, A Social and Personal Digital Photo Frame, offers comprehensive guidance on building a social and personal digital photo frame with the Raspberry Pi as the heart. Flickr will be used the social media platform to grab a set of images that will be shown, on the display. FEH will be used an X11 image viewer to display images as a slide show with customizable configurations. Some advanced configurations for the Raspberry Pi will be needed to automate the photo frame to connect with Flickr, download images to local storage, display them on screen, and frequently sync local storage with Flickr.

Chapter 4, Integrating a Real-Time IoT Dashboard, presents a real-time IoT dashboard to display sensor data and Raspberry Pi’s system information on it, and control actuators from it. The dashboard will be built with Adafruit IO, in conjunction with the Eclipse Paho Java MQTT library and Pi4J. The IC2 communication protocol will be used to read data from the sensors by enabling the 12C interface on Raspberry Pi.

Chapter 5, Wireless Controlled Robot, introduces the Zumo chassis kit and how to build a Raspberry Pi brain on it with Java and Pi4J. The robot uses two gear motors to rotate the drive wheels, and the Pi4J library allows us to build various moving and turning mechanisms to control the robot. The built-in Wi-Fi module of the Raspberry Pi 3 allows you to connect the robot wirelessly to the computer, in order to execute the commands through SSH with a keyboard.

Chapter 6, Building a Multipurpose IoT Controller, teaches you how to build a simple web-based IoT controller by installing and configuring a Jetty servlet engine on the Raspberry Pi. Pi4J is used to control an LED or any other actuator from the web interface through a local network or from the Internet, by configuring port forwarding on the router.

Chapter 7, Security Camera with Face Recognition, explains how to build a security camera with face recognition using the OpenCV library for Java and the Raspberry Pi camera module. It uses built-in cascade classifiers to detect human faces and highlights them with a square in real-time video.

What you need for this book

The following software and hardware components are essential in order to build all the projects that presented in this book:

Raspberry Pi 3

Micro SD card

HDMI screen or HDMI enabled TV

HDMI to HDMI cable

5V power supply

Breadboard and wires

Raspberry Pi 3 T-Cobbler or male to female jumper wires

Kohm resistors

Hitachi HD44780 16X2 LCD

A device capable of being detected by Bluetooth, such as a mobile phone

Phillips head screw driver

One Zumo Chassis kit (no motors)

Two 100:1 micro metal Gearmotor HP 6V

One H-bridge motor driver - SN754410 breakout board

Aluminum Standoff

Machine screws

Two 0.1µF ceramic capacitors

Four rechargeable NiMH AA batteries

One USB battery pack for Raspberry Pi - 10000 mAh - 2 x 5V outputs

3mm LED

Raspberry Pi camera module with mount

Hookup wires

Who this book is for

This book is suitable for those who have experience in Java application development and are interested in developing applications on the Raspberry Pi development environment. A basic level of Java-based development skills is essential to develop the projects that will be discussed in this book.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

Log in or register to our website using your e-mail address and password.

Hover the mouse pointer on the

SUPPORT

tab at the top.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box.

Select the book for which you're looking to download the code files.

Choose from the drop-down menu where you purchased this book from.

Click on

Code Download

.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for Windows

Zipeg / iZip / UnRarX for Mac

7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Raspberry-Pi-3-Projects-for-Java-Programmers. We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/RaspberryPi3ProjectsforJavaProgrammers_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.

Setting up Your Raspberry Pi

In the first part of this chapter, we will be setting up a Raspberry Pi 3 with the installation of the official, by the Raspberry Pi foundation, released Raspbian Linux distribution with the help of the NOOBS installer. This will be followed up with any preparations needed to be able to use the onboard hardware and the installation of Java. The second part will cover preparing NetBeans as the editor of choice to write, compile, and deploy our Java applications with a simple Hello World application at the end of the chapter to confirm that our setup works.

Getting started with the Raspberry Pi

With the release of the Raspberry Pi 3, the Raspberry Pi foundation has made a very big step in the history of the Raspberry Pi. The current hardware architecture is now based on a 1.2 GHz 64 bit ARMv7. This latest release of the Raspberry Pi also includes support for wireless networking and has an onboard Bluetooth 4.1 chip available.

Even though the Raspberry Pi 3 has 64-bit ARMv8, the operating system of the Raspberry Pi which is Raspbian confusingly report it is an 32-bit ARMv7. This is because of the Raspbian OS is currently only available in 32-bit.

To get started with the Raspberry Pi you will need the following components:

Keyboard and mouse:

Having both a keyboard and mouse present will greatly help with the installation of the Raspbian distribution. Almost any keyboard or mouse will work.

Display:

You can attach any compatible HDMI display, which can be a computer display or a television. The Raspberry Pi also has composite output shared with the audio connector. You will need an A/V cable if you want to use this output.

Power adapter:

Because of all the enhancements made, the Raspberry Pi foundation recommends a 5V adapter capable of delivering 2.5 A. You would be able to use a lower-rated one, but I strongly advise against this if you are planning to use all the available USB ports. The connector for powering the device uses a micro USB cable.

MicroSD

card

: The Raspberry Pi 3 uses a MicroSD card. I would advise using at least an 8-GB class 10 version. This will allow us to use the additional space to install applications, and as our projects will log data, you won't be running out of space soon.

The Raspberry Pi 3:

Last but not least, a Raspberry Pi 3. Some of our projects will be using the on-board Bluetooth chip, and this version will also be focused on in this book.

Our first step will be preparing a SD card for use with the Raspberry Pi. You will need a MicroSD card as the Raspberry Pi 3 only supports this format. The preparation of the SD card is being done on a normal PC, so it is wise to purchase one with an adapter fitting a full-size SD card slot. There are webshops selling preformatted SD cards with the NOOBS installer already present on the card. If you have bought one of these preformatted cards you can skip to the Installing Raspbian section.

Getting a compatible SD card

There is a large number of SD cards available. The Raspberry Pi foundation advises an 8-GB card, which leaves space to install different kinds of application and supplies enough space for us to write log data. When you buy an SD card, it is wise to keep your eyes open for the quality of these cards. Well-known and established manufacturers often supply better quality than the counterfeit ones. SD cards are sold with different class definitions. These classes explain the minimal combined read and write speeds. Class 6 should provide at least 6 MB/s, and class 10 cards should provide at least 10 MB/s. There is a good online resource available that provides tested results of using SD cards with the Raspberry Pi. If you need a resource to check for compatible SD cards, I advise you to go to the embedded Linux page at http://elinux.org/RPi_SD_cards.

Preparing and formatting the SD card

To be able to use the SD card, it first needs to be formatted. Most cards are already formatted with the FAT32 filesystem, which the Raspberry Pi NOOBS installer requires, unless you have bought a large SD card it is possible it is formatted with the exFAT filesystem. These then should also be formatted as FAT32. To format the SD card, we will be using the SD association's SDFormatter utility, which you can download from http://elinux.org/RPi_SD_cards, as default OS supplied formatters do not always provide optimal results.

In the following screenshot, the SDFormatter for the Mac is shown. This utility is also available for Windows and has the same options. If you are using Linux, you can use GParted. Make sure when using GParted you use FAT32 as the formatting option. As shown in the screenshot, select the Overwrite format option and give the SD card a label. The example shows RPI3JAVA, but this can be the label of your choice so you can quickly recognize the card when it's inserted:

Press the Format button to start formatting the SD card. Depending on the size of the SD card, this could take some time, enabling you to get a cup of coffee. The utility will show a done message in the form of Card Format complete when the formatting is done. You will now have a usable SD card.

To be able to use the NOOBS installer, you will need to follow the following steps:

Download the NOOBS installer from

https://www.raspberrypi.org/downloads/

.

Unzip the file with your favorite unzip utility. Most OSes already have one installed.

Copy the contents of the unzipped file into the SD card's

root

directory so that the copy result is as shown in the following screenshot:

When selecting the NOOBS for download, only select the lite version if you do not mind installing Raspbian using the Raspberry Pi's network connection.

After we have copied the required files into the SD card, we can start installing the Raspbian OS.

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:

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.

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.

We will now connect the Raspberry Pi with the micro USB power supply.

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.

At the bottom of the installation screen you will find the

Language

and

Keyboard