23,99 €
Android Things is the IoT platform made by Google, based on Android. It allows us to build smart devices in a simple and convenient way, leveraging on the Android ecosystem tools and libraries, while letting Google take care of security updates.
This book takes you through the basics of IoT and smart devices. It will help you to interact with common IoT device components and learn the underlying protocols. For a simple setup, we will be using Rainbow HAT so that we don't need to do any wiring.
In the first chapter, you will learn about the Android Things platform, the design concepts behind it, and how it relates to other IoT frameworks. We will look at the Developer Kits and learn how to install Android Things on them by creating a simple project.
Later, we will explore the real power of Android Things, learning how to make a UI, designing and communicating with companion apps in different ways, showcasing a few libraries. We will demonstrate libraries and you will see how powerful the Android Things operating system is.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 192
Veröffentlichungsjahr: 2018
Copyright © 2018 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 author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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.
Commissioning Editor:Gebin GeorgeAcquisition Editor: Noyonika DasContent Development Editor: Kirk DsouzaTechnical Editor: Jinesh TopiwalaCopy Editor: Safis EditingProject Coordinator: Hardik BhindeProofreader: Safis EditingIndexer: Mariammal ChettiyarGraphics: Jason MonteiroProduction Coordinator: Deepika Naik
First published: August 2018
Production reference: 1310818
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78934-179-9
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
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.com and 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.
Raul Portales is a software engineer who has had a love for computers, electronics, and gadgets in general for as long as he remembers.
He jumped into Android as soon as it was released. Raul has worked on social networks, education, healthcare, and even founded a gaming studio and a consultancy company. Specializing in mobile and UX, he speaks frequently at conferences and meetups.
Raul's love for electronics reignited when Google announced Android Things. He started tinkering with it with the first Developer Preview, which lead to adding the IoT category on his Google Developer expert profile.
Gautier Mechling is a software craftsman who's passionate about Android, and a Google Developer expert for IoT.
I would like to personally thank Raul for giving me the opportunity to be among the first readers of this book. It was an interesting and enjoyable read.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Android Things Quick Start Guide
Dedication
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Code in action
Conventions used
Get in touch
Reviews
Introducing Android Things
Technical requirements
What is Android Things?
Internet of Things (IoT) vs. smart devices
Android Things vs. other platforms
Emulators and testing
Android Studio
Design concepts behind Android Things
Displays are optional
Home activity support
Permissions
Android Things Console
Supported APIs
Hardware you will need
Android Things developer kits
Raspberry Pi
NXP Pico iMX7D
Rainbow HAT
Components, circuits, and so on
Wires and breadboards
Setting up a developer kit
Installing Android Things
Configuring Wi-Fi using the script
Configuring Wi-Fi using the system UI
Connecting to your Android Things
Creating an Android Things project
Manifest
Gradle configuration
Activity code
Summary
The Rainbow HAT
Technical requirements
Android Things architecture
Peripheral lifecycle
User space drivers
LEDs
The Arduino way
Threads
Coroutines
Using Handler and Runnable
Using Timer and Timertask
Buttons
Button driver
Debouncing
Button input drivers
Piezo buzzer
Alphanumeric display (Ht16k33)
Temperature and pressure sensor (Bmx280)
Direct read
Continuous updates with sensor driver
Adding pressure driver
LED strip (Apa102)
Summary
GPIO - Digital Input/Output
Technical requirements
Making the code work on any developer kit
Using GPIO for output
LEDs
Relays
Using GPIO for input
Buttons
Button driver
Input drivers
Controlling buttons directly with GPIO
Other sensors
Other usages of GPIO
DC motor controller (L298N)
Stepper motor (28BYJ-48) with controller (ULN2003)
Ultrasonic distance sensor (HC-SR04)
LCD display (TM1637)
Summary
PWM - Buzzers, Servos, and Analog Output
Technical requirements
PWM overview
Piezo buzzers
Servos
Using the servo driver
Tweaking the servo configuration
PWM as analog output
Summary
I2C - Communicating with Other Circuits
Technical requirements
Overview of I2C
Addressing circuits
Revisiting Rainbow HAT components
Alphanumeric display (Ht16k33)
Temperature and pressure sensor (Bmx280)
Extension components
Analog to digital converter (ADC) – PCF8591
Analog sensors
PWM expander – PCA9685
GPIO expander – PCF8575
Small displays
Other I2C sensors
Accelerometer/gyroscope – MPU6050
Summary
SPI - Faster Bidirectional Communication
Technical requirements
Overview of SPI
LED strip
Usage on displays
LED matrix
SPI version of SSD1306
Summary
The Real Power of Android Things
Technical requirements
Using Android UI
Companion apps and communication
REST API using NanoHttpd/Retrofit
Firebase Realtime Database
Nearby
Advertising and discovering
Initiating and confirming connections
Sending and receiving data
More cool stuff
TensorFlow – image classifier
Publish subscribe buses
Google Assistant – Home control
Online resources
Summary
Pinouts diagrams and libraries
Raspberry Pi Pinout
NXP iMX7D Pinout
Supported and unsupported libraries
Unsupported features
Common intents and content providers
Available Google APIs
Unavailable Google APIs
Other Books You May Enjoy
Leave a review - let other readers know what you think
This book will give you a quick start on Android Things, the platform for IoT made by Google and based on Android. We will go through the basics of IoT and smart devices, interact with a few components that are commonly used on IoT devices, and learn the protocols that work underneath, using examples and a hands-on approach.
We take our hands-on learning approach by going straight into playing with hardware using the Rainbow HAT, so we don't need to do any wiring. We then dig through layer after layer to understand what is being used underneath, but only after we have seen them working. If you are curious about more in-depth learning (such as writing your own drivers), you can always go into the next layer, because almost all the code referenced in this book is open source.
Since Android Things is a simplified version of Android, you only need a very basic knowledge of how Android works. If you have never done any Android development you will be able to follow along because our examples are kept simple by design.
If you have some experience with other similar platforms, that will be handy, but not necessary. This guide is designed for people that have little to no experience with electronics and microcontrollers but want to get started with them.
Basic knowledge of electronics is desired. That implies that you are familiar with the concepts of voltage and current, as well as resistors, diodes, and capacitors. You also need to know how to read a diagram (there will be a few in this book). All that is very basic knowledge. If you are not familiar with these concepts, you still can follow along, but ultimately you need to understand them to design your own schematics.
Throughout the book we will be using Kotlin as the programming language because it is more modern than Java and allows us to write more concise and easier-to-follow code. Kotlin is meant to be the future of Android development, and it makes a lot of sense to use it on the newest Android platform.
Chapter 1, Introducing Android Things, goes over the big picture of Android Things, the vision behind the platform, and how it compares to other – in principle – similar ones, such as Arduino. Then we will explore the Dev Kit, how to install Android Things on them and how connect to the boards to deploy our code to them. Finally, we will create of a project for Android Things using Android Studio, see how it is structured, and what are the differences from a default Android project.
Chapter 2, The Rainbow HAT, explains how to use the Rainbow HAT to get started with interactions with hardware. This HAT (Hardware On Top) is a selection of components that you can plug into a Dev Kit in order to start working with them without the need for any wiring. We will learn how to use LED, buttons, read temperature and pressure from a sensor and display it on an LCD alphanumeric display, play around with an RGB LED strip, and even make a simple piano, all using high-level abstraction drivers.
In the next four chapters we will take a look at what is under the hood and we will start exploring the different protocols in depth.
Chapter 3, GPIO – Digital Input/Output,goes over General Purpose Input Output (GPIO), which is what we used for the buttons and LEDs. We will learn how to access them directly and then look at other sensors that use GPIO, such as proximity and smoke detectors, as well as other components that also interact via GPIO, such as relays, DC motor controllers, stepper motors, ultrasound proximity sensors, and a numeric LCD display.
Chapter 4, PWM – Buzzers, Servos, and Analog Output, focuses on Pulse Width Modulation (PWM) and its basic usages, of which we have already have seen the piezo buzzer. The most popular use of PWM is servo motors, so we will see how they work. Finally, we'll learn how to use PWM as an analog output.
Chapter 5, I2C – Communicating with Other Circuits, covers the Inter-Integrated Circuit (I2C ) protocol. We have already used it for the temperature/pressure sensor and the LCD alphanumeric display. I2C is the most widely used protocol for simple communication between circuits, and we will explore a few of them. One of the key ones is analog to digital converters (ADC), and we will see how we can use them to read from analog sensors. Other components include magnetometers, accelerometers and IMUs in general, as well as GPIO and PWM expansion boards.
Chapter 6, SPI – Faster Bidirectional Communication, is based on the last protocol we'll look into: Serial Parallel Interface (SPI). We have already used this protocol for the RGB LED strip, and in this chapter we will look at some other drivers, such as OLED displays and LED matrix.
Chapter 7, The Real Power of Android Things, explores some areas where Android Things really shines by using some libraries and services that enable us to make the most of our developer kit. Among the topics we will cover are the use of Android UI, making companion apps using a REST API, Firebase, and Nearby, and we will briefly explore other libraries, such as Tensorflow for machine learning.
Appendix, Pinouts diagrams and libraries, we go over the Pinout diagrams of Rasberry PI and NXP iMX7D. We will then go into details about the state of unsupported Android features and intents on Android Things 1.0, as well as the state of available and unavailable Google APIs on Android Things.
To get the most out of this book you'll need an Android Things Dev Kit and a Rainbow HAT because it will allow you to run all the examples in Chapter 2, The Rainbow HAT.
There are a lot of other hardware components that you don't require, but they are interesting to have just to see them working. I recommend that you acquire the ones you are interested in. We go into more detail about developer kits and how to pick the right one, as well as a summary of the other hardware, as part of Chapter 1, Introducing Android Things.
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packtpub.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
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/Android-Things-Quick-Start-Guide. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/files/downloads/AndroidThingsQuickStartGuide_ColorImages.pdf.
Visit the following link to check out videos of the code being run:
http://bit.ly/2C4VX1M.
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "There is also the concept of AutoCloseable, which is an interface that all the peripherals implement."
A block of code is set as follows:
dependencies { [...] implementation 'com.google.android.things.contrib:driver-rainbowhat:+'}
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
class MainActivity : Activity() { private lateinit var led: Gpio override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)
setup()
while (true) {
loop()
} }
Any command-line input or output is written as follows:
$ sudo ./android-things-setup-utility-macos
Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "We select Install Android Things."
Feedback from our readers is always welcome.
General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packtpub.com.
Welcome to Android Things Quick Start Guide. In this introductory chapter, we will look at the big picture of Android Thing, how it compares to other—in principle—similar platforms, such as Arduino, and explore the vision behind the platform. We will check the different developer kits available exploring the pros and cons of each one. We will also look at the other components we will use throughout the book. We will also learn how to install Android Things on a developer kit and how to connect to it from our development computer in order to deploy our code. Finally, we will follow the creation of a basic project for Android Things using Android Studio, see how it is structured, and discuss the differences with a default Android project. The topics covered are as follows:
What is Android Things?
Design concepts behind Android Things
Hardware you will need
Setting up a developer kit
Creating an Android Things project
So, let's get started.
You will be required to have Android Studio and Android Things installed on a developer kit. You also will require many hardware components to effectively perform the tasks given in this chapter. The components are very interesting to have, just to see them working, but the Rainbow HAT is particularly important. We go into details about the developer kits and how to pick the right one, as a part of Chapter 1, Introducing Android Things. Finally, to use the Git repository of this book, you need to install Git.
The code files of this chapter can be found on GitHub:https://github.com/PacktPublishing/Android-Things-Quick-Start-Guide.
Check out the following video to see the code in action:
http://bit.ly/2wC0tyR.
Android Things is the IoT platform made by Google and based on Android. You could have guessed that by its name. It is similar to Android Wear, Android Auto, and Android TV in the way that it is an adaptation of Android to another domain, where most of the concepts of the platform are still valid, but there are significant differences as well.
The platform is intended to design and build IoT devices and bring them to the mass market. The key idea behind it is that you can easily prototype your project and, once it is ready, you can move from your developer kit to a simpler and smaller carrier board with just the SoM (System-on-Module).
SoMs are very handy. They are a step up from SoCs (System-on-Chips). They integrate RAM, flash storage, Wi-Fi, and Bluetooth on a single module. The official boards come with the FCC certifications, so all the process of getting the software onto them is streamlined.
Throughout the process of going to mass market, Google will provide security updates to the platform, so keeping your IoT devices up to date and secure is no longer something you have to worry about.
All the areas of this process are centralized on the Android Things Console, which has a similar function to the Google Play Console, but is more focused on building images that you can then distribute and deploy.
One of the key advantages of Android Things is that it can make use of almost all of the already existing Android libraries and frameworks, which gives it a head start in terms of tooling. From the developer point of view, it also lowers the entry barrier to make IoT devices, since all the knowledge you have of the Android framework, libraries, and tools is fully transferable.
Before we go any further, let's try to establish what most people understand by IoT and what smart objects are.
An IoT device is meant to use the internet as something integral for its behavior. Examples include remote controlled lights, any appliance that you can interact with over an API (such as lights, ovens, thermostats, coffee makers, and so on) even if it is only for reading data, such as an umbrella that glows when there is a rain forecast, or writing it, such as a simple weather station. For all those systems, the internet is an integral part of the concept.