Android Things Quick Start Guide Build your own smart devices using the Android Things platform Raul Portales BIRMINGHAM - MUMBAI Android Things Quick Start Guide 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 George Acquisition Editor: Noyonika Das Content Development Editor: Kirk Dsouza Technical Editor: Jinesh Topiwala Copy Editor: Safis Editing Project Coordinator: Hardik Bhinde Proofreader: Safis Editing Indexer: Mariammal Chettiyar Graphics: Jason Monteiro Production 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 Sometimes, hammers glow for a reason -Iridessa, about Tinkerbell To my parents, who always supported me on my endeavours; such as when they gave me my first electronics kit -even if it did not made sense to them as a toy- which planted the seeds for this book. – Raul Portales mapt.io 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. Why subscribe? 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 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.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. Contributors About the author 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. About the reviewer 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. Packt is searching for authors like you 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. Table of Contents Preface 1 Chapter 1: Introducing Android Things 8 Technical requirements 8 What is Android Things? 9 Internet of Things (IoT) vs. smart devices 10 Android Things vs. other platforms 10 Emulators and testing 11 Android Studio 12 Design concepts behind Android Things 13 Displays are optional 13 Home activity support 14 Permissions 14 Android Things Console 15 Supported APIs 16 Hardware you will need 16 Android Things developer kits 17 Raspberry Pi 17 NXP Pico iMX7D 18 Rainbow HAT 20 Components, circuits, and so on 21 Wires and breadboards 23 Setting up a developer kit 24 Installing Android Things 24 Configuring Wi-Fi using the script 27 Configuring Wi-Fi using the system UI 28 Connecting to your Android Things 30 Creating an Android Things project 32 Manifest 35 Gradle configuration 36 Activity code 37 Summary 38 Chapter 2: The Rainbow HAT 39 Technical requirements 40 Android Things architecture 41 Peripheral lifecycle 41 User space drivers 42 LEDs 42 The Arduino way 43 Table of Contents Threads 45 Coroutines 46 Using Handler and Runnable 48 Using Timer and Timertask 49 Buttons 51 Button driver 51 Debouncing 52 Button input drivers 53 Piezo buzzer 55 Alphanumeric display (Ht16k33) 57 Temperature and pressure sensor (Bmx280) 58 Direct read 58 Continuous updates with sensor driver 61 Adding pressure driver 63 LED strip (Apa102) 64 Summary 67 Chapter 3: GPIO - Digital Input/Output 70 Technical requirements 71 Making the code work on any developer kit 71 Using GPIO for output 73 LEDs 73 Relays 76 Using GPIO for input 78 Buttons 78 Button driver 78 Input drivers 80 Controlling buttons directly with GPIO 80 Other sensors 81 Other usages of GPIO 84 DC motor controller (L298N) 84 Stepper motor (28BYJ-48) with controller (ULN2003) 87 Ultrasonic distance sensor (HC-SR04) 89 LCD display (TM1637) 92 Summary 94 Chapter 4: PWM - Buzzers, Servos, and Analog Output 95 Technical requirements 96 PWM overview 96 Piezo buzzers 98 Servos 100 Using the servo driver 101 Tweaking the servo configuration 106 PWM as analog output 107 Summary 110 [ ii ]