Raspberry Pi Projects for Java Programmers Get the most out of your Raspberry Pi with Java Pradeeka Seneviratne John Sirach BIRMINGHAM - MUMBAI Raspberry Pi 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 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 service@packtpub.com 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 customerreviews@packtpub.com 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 Preface Chapter 1: 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 Chapter 2: 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 Chapter 3: A Social and Personal Digital Photo Frame Bill of materials Waveshare HDMI display Assembling with Raspberry Pi Selecting video source 7 11 13 21 27 32 36 38 39 40 42 42 43 44 45 48 55 57 63 67 71 72 77 82 83 84 84 85 87 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 Chapter 4: 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 [ ii ] 89 93 94 94 97 98 99 100 100 101 103 106 111 113 113 114 115 115 116 116 117 117 118 119 119 120 121 122 127 128 131 136 137 139 141 146 149 150 150 154 159 160 Creating a toggle button on Adafruit dashboard Subscribe to the button feed Controlling an LED from button feed Summary Chapter 5: 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 161 164 166 169 170 Writing your Java program Running and testing your Java program Summary 171 171 172 173 174 175 180 181 182 183 187 192 202 206 Chapter 6: Building a Multipurpose IoT Controller 207 Prerequisites Preparing your Raspberry Pi board Installing and configuring Jetty servelet engine Writing your first Java web application Creating a Maven project from Archetype 208 208 208 212 212 222 231 234 Moving Turning Swing turn Creating a servlet Copying iot.war file to the Raspberry Pi Summary Chapter 7: 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 [ iii ] 235 236 238 239 239 240 241 249 252 260 267 Security Camera with Face Recognition vboxCenter = new VBox(); vboxCenter.setAlignment(Pos.CENTER); vboxCenter.setPadding(new Insets(5, 5, 5, 5)); frame = new ImageView(); vboxCenter.getChildren().addAll(frame); root.setCenter(vboxCenter); hboxBottom = new HBox(); hboxBottom.setAlignment(Pos.CENTER); hboxBottom.setPadding(new Insets(5, 5, 5, 5)); videoButton = new Button("Start"); exitButton = new Button("Exit"); hboxBottom.getChildren().addAll(videoButton, exitButton); root.setBottom(hboxBottom); } public static void main(String[] args) { launch(args); } } Rebuild the SecurityCamera project with NetBeans and run it from the Raspberry Pi using the following command: sudo java -jar /home/pi/RASPI3JAVA/SecurityCamera/dist/SecurityCamera.jar [ 258 ] Security Camera with Face Recognition You will get a JavaFX-based window similar to the image shown in Figure 7-17: Figure 7-17: JavaFX window [ 259 ] Security Camera with Face Recognition Click the Start button to grab the video and the Stop button to stop the video The sequence of grabbed images (frames) at 30 frames per second makes a real-time video similar to what is shown in Figure 7-18: Figure 7-18: Video output Facial recognition Face recognition is another improvement that can be used to highlight the human face in a video output This can be further improved to identify the correct person by matching the human face with a pre-built database [ 260 ] Security Camera with Face Recognition In this chapter, we are going to use Haar-like features to encode the contrasts highlighted by the human face and its spatial relations with other objects present in the picture Usually, these features are extracted using a cascade classifier, which has to be trained in order to recognize different objects with precision A pre-trained classifier for face recognition can be found at https://github.com/opencv/opencv/tree/master/data/haarcascades in the OpenCV GitHub repository as listed following: haarcascade_frontalcatface.xml haarcascade_frontalcatface_extended.xml haarcascade_frontalface_alt.xml haarcascade_frontalface_alt2.xml haarcascade_frontalface_alt_tree.xml haarcascade_frontalface_default.xml The Haar classifier named haarcascade_frontalcatface.xml is great for detecting and tracking human faces in video output You can copy the downloaded haarcascade_frontalcatface.xml file to the /home/pi/RASPI3JAVA/SecurityCamera/ directory to use for face detection which is the front part of the human face Listing 7-4 presents the modified SecurityCamera.java program, which can be used to detect and track human faces in video output The application can also detect multiple faces and highlight each face with a rectangle: Listing 7-4: SecurityCamera.java package com.packt.B05688.chapter7; import import import import import import import import import import import import import import import java.io.ByteArrayInputStream; java.util.concurrent.Executors; java.util.concurrent.ScheduledExecutorService; java.util.concurrent.TimeUnit; javafx.application.Application; javafx.event.ActionEvent; javafx.geometry.Insets; javafx.geometry.Pos; javafx.scene.Scene; javafx.scene.control.Button; javafx.scene.image.Image; javafx.scene.image.ImageView; javafx.scene.layout.BorderPane; javafx.scene.layout.HBox; javafx.scene.layout.VBox; [ 261 ] Security Camera with Face Recognition import import import import import import import import javafx.stage.Stage; org.opencv.core.Core; org.opencv.core.Mat; org.opencv.core.MatOfByte; org.opencv.imgcodecs.Imgcodecs; org.opencv.imgproc.Imgproc; org.opencv.videoio.VideoCapture; org.opencv.videoio.Videoio; import org.opencv.core.MatOfRect; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.objdetect.CascadeClassifier; import org.opencv.objdetect.Objdetect; public class SecurityCamera extends Application { static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); } private boolean isStart = false; private VideoCapture capture; private ScheduledExecutorService timer; private BorderPane root; private VBox vboxCenter; private ImageView frame; private HBox hboxBottom; private Button videoButton, exitButton; CascadeClassifier faceDetector = new CascadeClassifier(); [ 262 ] Security Camera with Face Recognition @Override public void start(Stage primaryStage) { initGui(); capture = new VideoCapture(); exitButton.setOnAction((ActionEvent event) -> { System.exit(0); }); videoButton.setOnAction((ActionEvent event) -> { if (!isStart) { frame.setFitWidth(640); frame.setFitHeight(480); frame.setPreserveRatio(true); capture.open(0); capture.set(Videoio.CAP_PROP_FRAME_WIDTH, 640); capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 480); if (capture.isOpened()) { isStart = true; Runnable frameGrabber = new Runnable() { @Override public void run() { Image imageToShow = grabFrame(); frame.setImage(imageToShow); } }; timer = Executors.newSingleThreadScheduledExecutor(); timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS); videoButton.setText("Stop"); } else { System.err.println("Open camera error!"); } } else { isStart = false; videoButton.setText("Start"); try { timer.shutdown(); timer.awaitTermination(33, TimeUnit.MILLISECONDS); [ 263 ] Security Camera with Face Recognition } catch (InterruptedException e) { System.err.println(e); } capture.release(); frame.setImage(null); } }); Scene scene = new Scene(root, 800, 640); primaryStage.setScene(scene); primaryStage.show(); } private Image grabFrame() { faceDetector.load("haarcascade_frontalface_alt.xml"); Image result = null; Mat image = new Mat(); MatOfRect faceDetections = new MatOfRect(); if (capture.isOpened()) { capture.read(image); if (!image.empty()) { if (faceDetector.empty()) { System.out.println("error in face detector"); } faceDetector.detectMultiScale(image, faceDetections); System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); [ 264 ] Security Camera with Face Recognition for (Rect rect : faceDetections.toArray()) { Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0), 2); } result = mat2Image(".png", image); } } return result; } public static Image mat2Image(String ext, Mat image) { MatOfByte buffer = new MatOfByte(); Imgcodecs.imencode(ext, image, buffer); return new Image(new ByteArrayInputStream(buffer.toArray())); } private void initGui() { root = new BorderPane(); vboxCenter = new VBox(); vboxCenter.setAlignment(Pos.CENTER); vboxCenter.setPadding(new Insets(5, 5, 5, 5)); frame = new ImageView(); vboxCenter.getChildren().addAll(frame); root.setCenter(vboxCenter); hboxBottom = new HBox(); hboxBottom.setAlignment(Pos.CENTER); hboxBottom.setPadding(new Insets(5, 5, 5, 5)); videoButton = new Button("Start"); exitButton = new Button("Exit"); hboxBottom.getChildren().addAll(videoButton, exitButton); root.setBottom(hboxBottom); } public static void main(String[] args) { launch(args); [ 265 ] Security Camera with Face Recognition } } Let's look at the most important sections of the modified SecurityCamera.java program: Import the following additional classes to the program to use face detection and make graphics on them: import import import import import import org.opencv.core.MatOfRect; org.opencv.core.Point; org.opencv.core.Rect; org.opencv.core.Scalar; org.opencv.objdetect.CascadeClassifier; org.opencv.objdetect.Objdetect; Create a new object named faceDetector, of the CascadeClassifier class: CascadeClassifier faceDetector = new CascadeClassifier(); You can load the correct cascade classifier with the load method, by providing the relative path to the cascadeclassifier file We just use the filename, haarcascade_frontalface_alt.xml, because the file resides in the SecurityCamera directory If you'd like to place the cascade classifier files in a separate directory, for example a directory named resources, use the path resources/haarcascade_frontalface_alt.xml: faceDetector.load("haarcascade_frontalface_alt.xml"); The detectMultiscale method detects faces in every captured frame: faceDetector.detectMultiScale(image, faceDetections); System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); To draw a rectangle on the detected face, you can use the following code snippet: for (Rect rect : faceDetections.toArray()) { Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0), 2); } [ 266 ] Security Camera with Face Recognition Build and run First, rebuild the SecurityCamera project with NetBeans and run it from the Raspberry Pi using the following command: sudo java -jar /home/pi/RASPI3JAVA/SecurityCamera/dist/SecurityCamera.jar An application window will open; click on the Start button to start the video capture If the captured frame has a human face, the program will identify it and draw a rectangle around the face to highlight it The process continuously detects and highlights faces in every captured video frame Figure 7-19 shows a single face captured by the application: Figure 7-19: Single face capture [ 267 ] Security Camera with Face Recognition Figure 7-20 shows multiple faces captured by the application: Figure 7-20: Multiple faces captured Click the Stop button to stop the video capture Summary In this chapter, you learned how to use the camera module with a Raspberry Pi to capture video as a series of frames, and process them to detect human faces using a cascade classifier, which is haarcascade_frontalface_alt.xml You can improve this application by displaying the detected person's name, along with the highlighting rectangle, sending an email or SMS alert, or implementing a web stream to access the video through the network and display the video on a web page [ 268 ] Index A Adafruit IO about 119 dashboard, creating 128, 130, 131 feed, subscribing 160 I2C pins 136 I2C-compatible sensor, connecting to Raspberry Pi 137 key, finding 127 news feed, creating 122, 125 Raspberry Pi 136 sign in 120 tools 119 URL 119 album creating 97 automatic light switch required components 42 starting with 43 automatic switch, based on environment lighting using 67, 70 B Bluetooth chip using, on Raspberry Pi 71 Bluetooth device discovery feature using 72, 75 C Command Line Interface (CLI) 20 cron 115 D dashboard, Adafruit IO block, creating 131, 133, 135, 136 desktop mounting 93 digital out used, for displaying relay status 63, 67 used, for switching to relay status 63, 67 digital pins reading analog values, emulating 43 display resolution correcting 89, 92 E Eclipse Paho Java client reference link 150 F face recognition, video build and run 267, 268 face recognition reference link 261 feed LED, controlling 166, 168 subscribing 160, 164 toggle button, creating on Adafruit dashboard 161, 164 feh installing, on Raspberry Pi 111, 112 FileZilla download link 21 first automation project implementing 77, 80 Flickr API key obtaining 95, 97 Flickr image URL accessing 106, 109 Flickr photoset_id finding 98 Flickr REST endpoint reference link 99 Flickr API key, obtaining 94 connecting 94 Fritzing about 40, 41 installation link 40 installing 40 installing 21, 24, 26 JDK 8+ 209 Jetty servelet engine configuring 208, 210 installing 208, 210 reference link 208 K G keyboard and mouse display MicroSD power adapter Raspberry Pi General Inquiry Access Code (GIAC) 76 Graphical Processor Unit (GPU) 15 H HD44780 compatible 16x2 character display adding 45, 47 HD44780 compatible display data, displaying 48, 51, 54 L Light Dependent Resistor (LDR) about 40 adding, to setup 55, 57 values, displaying 57, 61, 63 values, reading 57, 61, 63 Limited Inquiry Access Code (LIAC) 76 I I2C class reference link 149 I2C-compatible sensor serial bus addresses 139, 141 I2C.0 136 I2C1 136 M J Java application digital_photo_frame.sh, scheduling with crontab 115 digital_photo_frame.sh, testing with crontab 115 digital_photo_frame.sh, testing with terminal 114 scheduling 113 shell script, writing 113 Java program executing 202, 203, 205 testing 202, 203, 205 writing 103, 104, 192, 193, 198, 200 Java web application Maven project, creating from Archetype 212, 215, 217, 219 writing 212 Java installation link 21 mainNetBeans Java editor application, running on Raspberry Pi 36 Maven project creating, from Archetype 212, 215, 217, 219 iot.war file, copying to Raspberry Pi 231, 232, 234 servlet, creating 222, 223, 225, 227, 229, 230 N NetBeans Java editor installing 27, 30 preparing 30 writing 33, 36 news feed, Adafruit IO topics 127 NOOBS installer download link 10 O OpenCV about 239 [ 270 ] building, on Raspberry Pi 249, 250, 252 downloading, on Raspberry Pi 249, 250, 252 downloading, on Windows 239 installing, on Windows 239 Java project, creating 240 library, adding to Java project 241, 242, 244, 246, 248 P personal digital photo frame building 84 Pi4J libraries pi4j-core.jar 44 pi4j-device 44 pi4j-gpio-extension.jar 44 Pi4j-service.jar 44 reference 44 point turn 184 Pololu Zumo chassis 171 pololu URL 170 Pulse Width Modulation (PWM) 170 R Raspberry Pi board Jetty servelet engine, configuring 208, 210 Jetty servelet engine, installing 208, 210 preparing 208 Raspberry Pi camera module about 236 connecting 238 Raspberry Pi components keyboard and mouse Raspberry Pi application, running 36 assembling 85 Bluetooth chip, using 71 compatible SD card, obtaining configuring, to I2C 141, 144, 146 Eclipse Paho Java client 150 I2C devices attached, searching 146, 148 I2C, accessing with Pi4J 149 I2C-compatible sensor, connecting 137, 139 Java program, writing to publish data to feed 150, 151, 152, 154 system information, publishing 159 temperature sensor data, publishing 154, 156 Raspbian configuring 13, 14, 17, 18, 21 installing 11 RC Circuit 42 reading analog values emulating, on digital pins 43 relay status displaying, digital out used 63, 67 switching to, digital out used 67 switching to, digital status used 63 remote Java application writing 32 required libraries installing 43 REST request format about 99 flickr.photosets.getPhotos, invoking 100 flickr.test.echo, invoking 100 photo source URL, constructing 101 S SD card download link formatting obtaining preparing reference shell script digital photo frame, starting on Raspberry Pi boot 116 photo frame, in action 117 writing, for slideshow 116 SparkFun Pi Wedge reference link 137 SystemInfo class reference link 159 T TMP102 digital temperature reference link 139 [ 271 ] V video facial recognition 260, 266 source, selecting 87, 89 working 253, 254, 259 W Waveshare HDMI display about 84 album, creating 97 desktop, mounting 93 display resolution, correcting 89, 92 feh, installing on Raspberry Pi 111, 112 Flickr API key, obtaining 94, 95, 97 Flickr image URL, accessing 106, 109 Flickr photoset_id, finding 98 Flickr, connecting 94 Java application, scheduling 113 Java program, writing 103, 104 Raspberry Pi, assembling 85 reference link 85 REST request format 99 shell script, writing for slideshow 116 video source, selecting 87, 89 Web Application ARchive (WAR) 208 Wide Super VGA (WSVGA) 84 wiringpi library 44 Z Zumo chassis kit about 171 assembling 172 circuit, building 175, 176, 178 instructions 180 motors, preparing to reduce effects of electrical noise 173, 174 moving operations 182, 183 prerequisites 171 Raspberry Pi, attaching 174 swing turn 187, 188, 190 turning operations 181, 183, 185, 186 .. .Raspberry Pi Projects for Java Programmers Get the most out of your Raspberry Pi with Java Pradeeka Seneviratne John Sirach BIRMINGHAM - MUMBAI Raspberry Pi Projects for Java Programmers... 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 at https://github.com/PacktPublishing /Raspberry- Pi-3-Projects -for- JavaProgrammers... handler.setHome(); /// Write to the first line handler.write(" RASPI 3JAVA "); /// Create a time format for output SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); /// Sets the cursor on the second