Raspberry Pi 3 Projects for Java Programmers
By John Sirach and Pradeeka Seneviratne
()
About this ebook
- Explore the small yet powerful mini computer in order to run java applications
- Leverage Java libraries to build exciting projects on home automation, IoT, and Robotics by leveraging Java libraries
- Get acquainted with connecting electronic sensors to your Raspberry Pi 3 using Java APIs.
The book is aimed at Java programmers who are eager to get their hands-on Raspberry Pi and build interesting projects using java. They have a very basic knowledge of Raspberry Pi.
Related to Raspberry Pi 3 Projects for Java Programmers
Related ebooks
Mastering BeagleBone Robotics Rating: 5 out of 5 stars5/5Learning iBeacon Rating: 0 out of 5 stars0 ratingsIntel Galileo Essentials Rating: 0 out of 5 stars0 ratingsRaspberry Pi Blueprints Rating: 0 out of 5 stars0 ratingsCollection of Raspberry Pi Projects Rating: 5 out of 5 stars5/5BeagleBone Essentials Rating: 0 out of 5 stars0 ratingsProgramming the BeagleBone Rating: 0 out of 5 stars0 ratingsAndroid for the BeagleBone Black Rating: 0 out of 5 stars0 ratingsMastering Java: A Comprehensive Guide to Programming Excellence Category Rating: 0 out of 5 stars0 ratingsAndroid Things Projects Rating: 0 out of 5 stars0 ratingsIntel Galileo Blueprints Rating: 0 out of 5 stars0 ratingsRaspberry Pi LED Blueprints: Design, build, and test LED-based projects using the Raspberry Pi Rating: 0 out of 5 stars0 ratingsRaspberry Pi Android Projects: Create exciting projects by connecting the Raspberry Pi to your Android phone Rating: 0 out of 5 stars0 ratingsProgramming the BeagleBone Black: Getting Started with JavaScript and BoneScript Rating: 0 out of 5 stars0 ratingsRaspberry Pi 2 Server Essentials Rating: 0 out of 5 stars0 ratingsLighttpd Rating: 4 out of 5 stars4/5Banana Pi Cookbook Rating: 0 out of 5 stars0 ratingsThe Essential Guide to Control Systems Rating: 0 out of 5 stars0 ratingsBeagleBone Robotic Projects Rating: 5 out of 5 stars5/5Uno Programming digitalWrite: Beginners Edition S O S Project Rating: 0 out of 5 stars0 ratingsThe Encrypted Web: Building Secure and Invisible Networks: Networking, #1 Rating: 0 out of 5 stars0 ratingsApple Watch For Seniors For Dummies Rating: 0 out of 5 stars0 ratingsMicrosoft System Center Orchestrator 2012 R2 Essentials Rating: 0 out of 5 stars0 ratingsAndroid High Performance Programming Rating: 0 out of 5 stars0 ratingsCacti 0.8 Network Monitoring Rating: 0 out of 5 stars0 ratingsRaspberry Pi: The Ultimate Start Guide to Raspberry Pi Rating: 0 out of 5 stars0 ratingsKinect in Motion – Audio and Visual Tracking by Example Rating: 0 out of 5 stars0 ratings
Hardware For You
CompTIA A+ Complete Review Guide: Exam Core 1 220-1001 and Exam Core 2 220-1002 Rating: 5 out of 5 stars5/5Electrical Engineering | Step by Step Rating: 0 out of 5 stars0 ratingsChip War: The Fight for the World's Most Critical Technology Rating: 4 out of 5 stars4/5iPhone 16 Pro Max User Manual: The Complete Step-By-Step Guide to Maximize your New iPhone 16 Pro Max and iOS 18 Rating: 0 out of 5 stars0 ratingsmacOS Sonoma For Dummies Rating: 0 out of 5 stars0 ratingsMacBook For Dummies Rating: 4 out of 5 stars4/5Mastering ChatGPT Rating: 0 out of 5 stars0 ratingsAmazon Web Services (AWS) Interview Questions and Answers Rating: 5 out of 5 stars5/5CompTIA A+ Complete Review Guide: Core 1 Exam 220-1101 and Core 2 Exam 220-1102 Rating: 5 out of 5 stars5/52025 Procreate and Procreate Dreams: A Complete Guide to Digital Art and Animation on iPad Rating: 0 out of 5 stars0 ratingsExploring Arduino: Tools and Techniques for Engineering Wizardry Rating: 4 out of 5 stars4/5iPhone For Dummies Rating: 0 out of 5 stars0 ratingsHow Computers Really Work: A Hands-On Guide to the Inner Workings of the Machine Rating: 0 out of 5 stars0 ratingsCompTIA A+ Complete Study Guide: Core 1 Exam 220-1101 and Core 2 Exam 220-1102 Rating: 0 out of 5 stars0 ratingsHacking Electronics: Learning Electronics with Arduino and Raspberry Pi, Second Edition Rating: 0 out of 5 stars0 ratingsThe Official Raspberry Pi Beginner's Guide: How to use your new computer Rating: 4 out of 5 stars4/5How to Build a Computer: The Best Beginner's Guide to Building Your Own PC from Scratch! Rating: 0 out of 5 stars0 ratingsRaspberry Pi Cookbook for Python Programmers Rating: 0 out of 5 stars0 ratingsGitHub For Dummies Rating: 0 out of 5 stars0 ratingsDancing with Qubits: How quantum computing works and how it can change the world Rating: 5 out of 5 stars5/5iMac For Dummies Rating: 0 out of 5 stars0 ratingsBuild Your Own PC Do-It-Yourself For Dummies Rating: 4 out of 5 stars4/5Learning Python with Raspberry Pi Rating: 0 out of 5 stars0 ratingsHow to Install Kodi On FireTV stick 2018 Rating: 0 out of 5 stars0 ratings3D Printing For Dummies Rating: 4 out of 5 stars4/5Macs For Dummies Rating: 5 out of 5 stars5/5Raspberry Pi Sensors Rating: 0 out of 5 stars0 ratings
Reviews for Raspberry Pi 3 Projects for Java Programmers
0 ratings0 reviews
Book preview
Raspberry Pi 3 Projects for Java Programmers - John Sirach
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
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.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: Let's take a look at the main(String args[]) method.
A block of code is set as follows:
/**
* Run the LCD example.
*/
public final void runExample(){
/// Clear the display
handler.clear();
/// Cursor to home position (0,0)
handler.setHome();
/// Write to the first line.
handler.write(-- RASPI3JAVA --
);
/// Create a time format for output
SimpleDateFormat formatter = new SimpleDateFor-
mat(HH:mm:ss
);
/// Sets the cursor on the second line at the first posi-
tion.
handler.setCursor(1, 0);
/// Write the current time in the set format.
handler.write(---
+ formatter.format(new Date()) + ---
);
}
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: In the top of the window there are a set of buttons present which when started has the Welcome button selected. On the same level there is a Breadboard button.
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
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