Android Things Projects - Francesco Azzola - E-Book

Android Things Projects E-Book

Francesco Azzola

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

Android Things makes developing connected embedded devices easy by providing the same Android development tools, best-in-class Android framework, and Google APIs that make developers successful on mobile.

With this book, you will be able to take advantage of the new Android framework APIs to securely build projects using low-level components such as sensors, resistors, capacitors, and display controllers. This book will teach you all you need to know about working with Android Things through practical projects based on home automation, robotics, IoT, and so on. We’ll teach you to make the most of the Android Things and build enticing projects such as a smart greenhouse that controls the climate and environment automatically. You’ll also create an alarm system, integrate Android Things with IoT cloud platforms, and more.

By the end of this book, you will know everything about Android Things, and you’ll have built some very cool projects using the latest technology that is driving the adoption of IoT. You will also have primed your mindset so that you can use your knowledge for profitable, practical projects.

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

EPUB
MOBI

Seitenzahl: 214

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.



Android ThingsProjects

 

 

 

 

 

 

 

 

 

 

Efficiently build IoT projects with Android Things

 

 

 

 

 

 

 

 

Francesco Azzola

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Android Things Projects

 

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 author, 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: June 2017

 

Production reference: 1290617

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

ISBN 978-1-78728-924-6

 

www.packtpub.com

Credits

Author

Francesco Azzola

Copy Editor

Safis Editing

Reviewers

Ali Utku Selen

Raimon Rafols Montane

 

Project Coordinator

Kinjal Bari

Commissioning Editor

Vijin Boricha

Proofreader

Safis Editing

Acquisition Editor

Namrata Patil

Indexer

Mariammal Chettiyar

Content Development Editor

Mamata Walkar

Graphics

Kirk D'Penha

Technical Editor

Varsha Shivhare

Production Coordinator

Melwyn Dsa

 

About the Author

Francesco Azzola is an electronic engineer with over 15 years of experience in computer programming and JEE architecture. He is a Sun Certified Enterprise Architect (SCEA), SCWCD, and SCJP. He is an Android and IoT enthusiast who loves creating IoT projects using Arduino, Raspberry Pi, Android, and other platforms.He is interested in the convergence of IoT and mobile applications. Previously, he worked in the mobile development field for several years. He has created a blog called Surviving with Android, where he shares posts on coding in Android and IoT projects.

About the Reviewers

Ali Utku Selen is a system engineer at Sony Mobile, who has been working on flagship Android devices for more than five years. He started programming at age 11, and since then he has developed a great interest in software development. He holds an MSc degree from the Computer Engineering Department of Dokuz Eylul University.

 

Raimon Rafols Montane has been developing for mobile devices since 2004. He has experience in developing on several technologies specializing in UI, build systems, and client-server communications. He is currently working as a Engineering Manager at AXA Group Solutions in Barcelona, although has been working in the past for Imagination Technologies near London and Service2Media in the Netherlands. In his spare time, he enjoys programming, photography, and giving talks at mobile conferences about Android performance optimization and Android custom views.

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.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.

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/1787289249.

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

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

Getting Started with Android Things

Internet of Things overview

IoT components

Android Things overview

Things support library

Android Things board compatibility

How to install Android Things on Raspberry

How to install Android Things using Windows

How to install Android Things using OS X

Testing the installation

How to install Android Things on Intel Edison

Configuring the WiFi

Creating the first Android Things project

Cloning the template project

Create the project manually

Differences between Android and Android Things

Create your first Android Things app

Summary

Creating an Alarm System Using Android Things

Alarm system project description

PIR sensor

Project schematic

How to use GPIO pins

Reading from the GPIO pin

How to add a listener to GPIO

Declare the event to listen to

Implementing the callback class

How to close the connection with a GPIO pin

Handle different boards in Android Things

Android Things board pinout

How to identify the board

How to implement the notification system

Configuring firebase

Add the notification feature to the Android Things app

Android companion app

Summary

How to Make an Environmental Monitoring System

Environmental monitoring system project overview

Project components

Project schematic

How to read data from sensors

Handling sensors using the Android sensor framework

Implementing the sensor callback

How to handle dynamic sensors

Putting it all together - acquiring data

How to close the sensor connection

How to control GPIO pins

Initialize the GPIO pin

Diving into I2C protocol

I2C protocol overview

How to implement a custom sensor driver

Low-level sensor driver

Summary

Integrate Android Things with IoT Cloud Platforms

IoT cloud architecture

An IoT cloud platform overview

IoT cloud architecture overview

Streaming data to the IoT cloud platform

How to configure Artik Cloud

Artik client description

How to implement the Android Things Artik client

Implement a StringRequest with Volley

Implement a custom HTTP header

Send the data using a custom body request

Sending data from the Android Things app

Creating a dashboard

Data logging

Adding voice capabilities to Android Things

Configure Temboo choreo

Integrate Temboo in the Android Things app

Summary

Create a Smart System to Control Ambient Light

Ambient light control system description

Project components

Project architecture

Building the Arduino project

How Arduino exposes the services

Implementing the Android Things app

How to develop an Android Things app UI

Attaching the layout to the Activity

Handling UI events

Invoking the Arduino services

How to implement a web interface

Implementing a simple HTTP web server

Creating the HTML page with the UI

Embedding the HTTP Server into the Android Things app

Summary

Remote Weather Station

Remote weather station project description

Project components

The M2M architecture and the MQTT protocol

MQTT protocol overview

MQTT message details

Security and QoS

Using MQTT in our remote weather station

Implementing the MQTT publisher

Connecting to MQTT and sending data

Implementing the MQTT subscriber using Android Things

Implementing the Android Things Activity

Displaying the information using OLED display

Connect the OLED display to Android Things board

Installing the MQTT server

Installing the MQTT broker

Configuring the MQTT broker

Summary

Build a Spying Eye

Spying eye Android Things project overview

Project components

Pulse Width Modulation overview

How to use PWM with Android Things

Implementing the spying eye project in Android Things

Controlling a servomotor in Android Things

Using a camera in Android Things

Getting ready to use the camera

Assembling the app

Summary

Android with Android Things

Architecture to connect Android and Android Things

How to control a LED strip using an Android app

Connecting the Android app to Android Things

How to develop an Android app that retrieves data from Android Things

How to implement a Bluetooth connection

Creating the Android app

Implementing the Bluetooth server in Android Things

Summary

Preface

Android Things is the new OS developed by Google for building professional IoT projects using Android. Throughout the course of this book, you will gain deep knowledge of Android Things and get ready for the next technological revolution. You will learn how to create real-life IoT projects covering all the aspects of Android Things.

What this book covers

Chapter 1, Getting Started with Android Things, introduces IoT and explains why it has such huge impact on everyday life. This chapter also introduces Android Things and explains how to use it in your first IoT project.

Chapter 2, Creating an Alarm System Using Android Things, shows how to use two-state sensors (or binary devices) in Android Things. This chapter also covers creating an alarm system that detects motion and sends a notification to a user's smartphone.

Chapter 3, How to Make an Environmental Monitoring System, shows how to connect sensors to Android Things and how to read data using the I2C bus. These concepts are applied to an IoT project that monitors the environmental parameters and uses an RGB LED to visualize it.

Chapter 4, Integrate Android Things with IoT Cloud Platforms, covers how to use Android Things in an IoT cloud architecture. This chapter describes how to stream real-time data from sensors to IoT cloud platforms.

Chapter 5, Create a Smart System to Control Ambient Light, demonstrates how to use a simple integration pattern to integrate Android Things with Arduino using the HTTP protocol.

Chapter 6, Remote Weather Station, covers how to use Android Things in Machine to Machine (M2M) architecture. In this chapter, we will build a remote weather station that monitors temperature, humidity, pressure, and light, and sends data using the MQTT protocol.

Chapter 7, Build a Spying Eye, shows how to develop an Android Things app that controls servo motors using Pulse Width Modulation (PWM) and how to use the camera with Android Things.

Chapter 8, Android with Android Things, covers how to develop Android companion apps that interact with Android things.

What you need for this book

To build the examples in this book, you need to have Windows OS or Mac OS X. Moreover, in order to develop, compile, and install the Android Things app, you have to install Android Studio as specified during the chapters.

Who this book is for

This book is for Android enthusiasts, hobbyists, IoT experts, and Android developers who want to gain deep knowledge of Android Things. The main focus is on implementing IoT projects using Android Things. This book also covers Android Things API and how to use them in IoT. The reader will use sensors, resistors, capacitors, and IoT cloud platforms.

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:

"We can include other contexts through the use of the include directive."

A block of code is set as follows:

adb shell am startservice-n com.google.wifisetup/.WifiSetupService-a WifiSetupService.Connect-e ssid <Your_WIFI_SSID>-e passphrase <WIFI_password>

Any command-line input or output is written as follows:

sudo dd bs=1m if=path_of_your_image.img of=/dev/rdiskn

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 text like this:

"When the board is connected to your PC or Mac, it appears in the Platform Flash Tool Light."

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 to 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 at https://github.com/PacktPublishing/Android-Things-Projects. We also have other code bundles from our rich catalog of books and videos available at https://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/AndroidThingsProjects_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.

Getting Started with Android Things

Recently, Google released its first operating system built for the Internet of Things, called Android Things. During this book, you will learn how to build IoT projects using this OS with compatible development boards and peripherals such as sensors, LEDs, servo, and so on.

In this chapter, at the beginning, we will give an overview of Android Things, covering what it is and how it differs from Android. You will learn how to reuse your Android knowledge in Android Things projects. After this, you will learn how to install Android Things on different boards such as Raspberry Pi 3 and Intel Edison with an Arduino breakout kit. This will help you to familiarize yourself with development boards while we set up our development environment. Once you are comfortable with it, we will move to creating the first Android Things project and you will learn how to use simple peripherals such as LEDs and buttons (or switches). In more detail, we will explore how to convert an Android project into an Android Things project. Moreover, you will have an overview of the most import Android Things API and how to use it in a real IoT project.

The main topics covered in this chapter are:

Internet of things overview Android Things layer structure

How to install Android Things on Raspberry Pi 3

How to install Android Things on Intel Edison with Arduino breakout kit

How to create an Android Things project

Internet of Things overview

Internet of Things, or briefly IoT, is one of the most promising trends in technology. According to many analysts, Internet of things can be the most disruptive technology in the upcoming decade. It will have a huge impact on our lives and it promises to modify our habits. IoT is and will be in the future a pervasive technology that will span its effects across many sectors:

Industry

Healthcare

Transportation

Manufacturing

Agriculture

Retail

Smart cities

All these areas will benefit from using IoT. Before diving into IoT projects, it is important to know what IoT means. There are several definitions about the Internet of things, addressing different aspects and considering different areas of application. Anyway, it is important to underline that the IoT is much more than a network of smartphones, tablets, and PCs connected to each other. Briefly, IoT is an ecosystem where objects are interconnected and, at the same time, they connect to the internet. The Internet of things includes every object that can potentially connect to the internet and exchange data and information. These objects are always connected anytime, anywhere, and they exchange data.

The concept of connected objects is not new and over the years it has been developed. The level of circuit miniaturization and the increasing power of CPU with a lower consumption makes it possible to imagine a future where there are millions of "things" that talk to each other.

The first time that the Internet of things was officially recognized was in 2005. The International Communication Union (ITU) in a report titled The Internet of things (https://www.itu.int/osg/spu/publications/internetofthings/InternetofThings_summary.pdf), gave the first definition:

"A new dimension has been added to the world of information and communication technologies (ICTs): from anytime, any place connectivity for anyone, we will now have connectivity for anything ... . Connections will multiply and create an entirely new dynamic network of networks - an Internet of Things "

In other words, the IoT is a network of smart objects (or things) that can receive and send data and we can control it remotely.

IoT components

There are several elements that contribute to creating the IoT ecosystem and it is important to understand the role they play in order to have a clear picture about IoT. This will be useful to better understand the projects we will build using Android Things. The basic brick of IoT is a smart object. It is a device that connects to the internet and it is capable of exchanging data. It can be a simple sensor that measures a quantity such as pressure, temperature, and so on, or a complex system. Extending this concept, our oven, our coffee machine, and even our washing machine are all examples of smart objects once they connect to the internet. All of these smart objects contribute to developing the internet of things network. Anyway, it's not only household appliances that are examples of smart objects, but also cars, buildings, actuators, and so on. We can reference these objects, when connected, using a unique identifier and start talking to them.

At the low level, these devices exchange data using a network layer. The most important and known protocols at the base of Internet of things are:

Wi-Fi

Bluetooth

Zigbee

Cellular network

NB-IoT

LoRA

From an application point of view, there are several application protocols widely used in the internet of things. Some protocols derive from different contexts (such as the web); others are IoT-specific. To name a few of them, we can remember:

HTTP

MQTT

CoAP

AMQP

Rest

XMPP

Stomp

By now, they could be just names or empty boxes, but throughout this book we will explore how to use these protocols with Android Things.

Prototyping boards play an important role in the Internet of things and they help to develop the number of connected objects. Using prototyping boards, we can experiment with IoT projects and in this book, we will explore how to build and test IoT projects using boards compatible with Android Things. As you may already know, there are several prototyping boards available on the market, each one having specific features. Just to name a few of them, we can list:

Arduino (in different flavors)

Raspberry Pi (in different flavors)

Intel Edison

ESP8266

NXP

We will focus our attention on Raspberry Pi 3 and Intel Edison because Android Things officially supports these two boards. During the books, we will also use other development boards so that you can understand how to integrate them.

Android Things overview

Android Things is the new operating system developed by Google to build IoT projects. This helps you to develop professional applications using trusted platforms and Android. Yes Android, because Android Things is a modified version of Android and we can reuse our Android knowledge to implement smart Internet of things projects. This OS has great potential because Android developers can smoothly move to IoT and start developing and building projects in a few days. Before diving into Android Things, it is important to have an overview. Android Things OS has the layer structure shown in the following diagram:

Source: https://developer.android.com/things/sdk/index.html

This structure is slightly different from Android OS because it is much more compact so that apps for Android Things have fewer layers beneath and they are closer to drivers and peripherals than normal Android apps. Even if Android Things derives from Android, there are some APIs available in Android not supported in Android Things. We will now briefly describe the similarities and the differences.

Let us start with the content providers, widely used in Android, and not present in Android Things SDK. Therefore, we should pay attention when we develop an Android Things app. To have more information about these content providers not supported, please refer to the Official Android Things website at https://developer.android.com/things/sdk/index.html.

Moreover, like a normal Android app, an Android Things app can have a User Interface (UI), even if this is optional, and it depends on the type of application we are developing. A user can interact with the UI to trigger events as they happen in an Android app. From this point of view, as we will see later, the developing process of a UI is the same as used in Android. This is an interesting feature because we can develop an IoT UI easily and fast, re- using our Android knowledge.

It is worth noting that Android Things fits perfectly in the Google services. Almost all cloud services implemented by Google are available in Android Things with some exceptions. Android Things does not support Google services strictly connected to the mobile world and those that require user input or authentication. Do not forget that user interface for an Android Things app is optional. To have a detailed list of Google services available in Android Things refer to the official page at https://developer.android.com/things/sdk/index.html.