INTRODUCTION

CONNECTIVITY IS AN EMERGENT TOPIC in home automation. Your tablet should be discovered automatically by your home entertainment system, offering you full control of the film you want to see or the music you want to play. Your refrigerator should be smart enough to keep track of all the groceries in your home and even tell your smartphone what to buy when you arrive at the supermarket. Your car should connect to your cell phone automatically as you turn the ignition on, enabling it to access your music library and all of your contacts — as well as reject incoming phone calls with a pleasant voice, kindly informing whoever is calling that you’re currently driving and shouldn’t be disturbed.

The idea of a connected life where anything digital sends and receives data from the Internet, and not just your TV or fridge, is something we’re both working with on a daily basis as researchers and teachers at Malmö University’s School of Arts and Design, Sweden. This research field and new computing paradigm is known as the Internet of Things. It centers its efforts on analyzing the implications of connecting our everyday life to the network through a multitude of devices.

We spend our days bringing to life visions of the future. This book is about some hands-on techniques that will help you realize your own ideas. We would love to see you get your hands dirty experimenting with hardware and software, which is why we want to give you that little extra nudge into the Maker movement. In this book you will be building seven different projects using Arduino and Android in different ways, and detailing how you could potentially refine and continue building on them.

WHO THIS BOOK IS FOR

This book is intended for the more seasoned Android developer; you may have already written and published your first application on Google Play and want to explore new frontiers.

In some places we assume you have enough knowledge about the Android frameworks that you feel comfortable browsing classes and libraries you have not yet used.

If you’re also familiar with the electronics prototyping platform called Arduino, you can even skip certain parts of Chapters 7 and 8 because those deal with the introduction to electronic sensors and actuators, and connecting those with Arduino.

WHAT THIS BOOK COVERS

The Android operating system offers you, as a developer, the possibility of creating accessories in an open fashion. You can design, manufacture, and sell electronics to be attached to Android phones in a completely standard way that is fully supported by the operating system. The Android Open Accessory Protocol (AOAP) is the way any Android device connects to accessories, and it has been available since Android’s revision 2.3.4. The first part of the book has been made to accommodate any version of Android as long as it supports the AOAP.

You also learn about a much more recent version of Android. The latter chapters explore the use of Jelly Bean (Android’s revision number 4), launched in the summer of 2012. It offers high-speed video and some other interesting features needed to build the most advanced projects you will find at the end of the book.

When it comes to the electronics, you will be using the latest revision of the Arduino IDE. At the time of writing it was 1.0.2. You should not try the code provided here with earlier versions because we cannot assure its functionality. This revision of the IDE runs with both the Arduino Mega ADK (compatible with the Google ADK v1) and the Arduino Due (compatible with the Google ADK v2).

We have tried all the examples with the Arduino Mega ADK. We haven’t tested them with other compatible boards, but as long as they are compatible, things should run in the very same way. Please take into account that a lot of different manufacturers produce boards and we don’t have access to all of them.

HOW THIS BOOK IS STRUCTURED

This book has two major parts with several chapters each. The first part of the book deals with the basics of getting up and running with the Android Open Accessory framework, and building the tools you’ll use for the second part. The second part of the book is all about projects — designing and building your Android accessory prototypes using the tools from Part I.

Part I of the book runs from Chapter 1 to Chapter 8.

Chapter 1, “Introduction to Android Open Accessory,” introduces you to the two systems you use in the book, Android and Arduino.

Chapter 2, “Setting up the (Arduino) Hardware,” is all about electronics, telling you about all the different options available when you want to connect an Arduino-based prototype to your Android phone.

Chapter 3, “Understanding Data Communication,” covers the basics of data communication; how data protocols work and are designed. It also introduces the protocol that is used in this book, called P2PMQTT, based on MQTT which is a machine-to-machine messaging protocol designed by IBM.

Chapter 4, “Setting up Development Environments,” guides you through setting up the two development environments used in this book: Android and Arduino. In this chapter you also test run your very first Android Accessory.

In Chapter 5, “Creating the Accessory Library,” you build the first version of the MQTT-based Android library used to develop all the accessory projects in this book. We strongly recommend that you read Chapter 3 before building the library. Apart from MQTT, you also add the Android Open Accessory-specific code to send and receive messages from and to your Arduino-based accessory.

When you’ve developed the library in Chapter 5 you can move on to Chapter 6, “Using Your Accessory Library,” where you create Android accessory applications for the smaller projects you build in Chapters 7 and 8, using your new library.

Chapter 7, “Digital Arduino,” is an introduction to digital sensors and actuators using Arduino. In this chapter you start by learning the basics of Arduino, and finish building smaller accessory-enabled projects that connect to the applications you developed in Chapter 6.

Chapter 8, “Analog Arduino,” continues with the introduction from Chapter 7, but in this chapter you switch focus from digital sensors and actuators to the analog counterparts, such as motors and potentiometers. It starts off with some basic Arduino examples, and by the time you’re done you should have built two smaller accessory-enabled projects.

Part II of the book deals with three more significant projects, where you use more than one type of sensor or actuator, and exchange information often in both directions between the two devices.

Chapter 9, “Bike Ride Recorder,” describes our process of attaching electronic sensors and actuators to a racer bike. You will build an accessory that enables you to record a bike ride with your phone while monitoring your effort in terms of the amount of pedaling you do. At the end of the ride, the phone will render your trip while also displaying your actual speed and the speed detected by your peddling.

The project you build in Chapter 10, “Kitchen Lamp,” enables you to control the lighting in a room through your Android device when special events happen on the phone, such as a phone call or SMS, and even change the lighting pattern depending on who is calling or texting you.

Chapter 11, “Mr. Wiley,” is the final chapter of the book. In this chapter you build a robot with an “Android brain” that enables it to react in certain ways depending on its environment, such as “running” away from strangers or following a special pattern on the floor.

WHAT YOU NEED TO USE THIS BOOK

To begin creating accessories using the Android Open Accessory framework and Arduino, it’s highly recommended that you have at least an Android device running Android 3.1 or above (Andorid 2.3.4 will also work, but it’s not recommended) and an Arduino Mega ADK microcontroller board. Without these two components you can’t run any of the code examples found in this book.

You also need two different development environments, one for Android and one for Arduino. It’s not required that you use the Eclipse or Arduino IDEs, but it’s recommended because those are the best documented ways of developing for either platform.

Building Arduino prototypes is more than just code — you need at least the very basic sensors and actuators from each example in the first part of the book to build the mini projects. The Arduino Store has been kind enough to assemble a kit specifically for this book, and you can find it at http://store.arduino.cc. If you check the back of the book you will find a one-stop source for the components to the examples and projects for that first part of the book. The projects in the second half can also be sourced at the same place, but they end up being somehow expensive and therefore it is up to the reader to purchase the components needed in each one of the three projects presented in part two.

However, if you want to acquire the material bit-by-bit, or you just want to buy it elsewhere, you can use the list in Table I-1.

TABLE I-1: Electronic Components Needed for Part I of This Book

image

Most of these components are completely standard and you can find them at a store close to you. If you happen to be in the US, online stores like http://adafruit.com and http://sparkfun.com are well known among hobbyists as good places to find parts, Arduino boards and all sorts of materials needed to build projects.

If you are in Europe there is a long list of possible distributors, you can find many of them at your own country. If you want to buy parts saving money on delivery and import taxes, you should check http://arduino.cc/en/Main/Buy where you will find a list of possible vendors of Arduino boards as well as many other materials for the projects in this book.

CONVENTIONS

To help you get the most from the text and keep track of what’s happening, we’ve used a number of conventions throughout the book.


WARNING Boxes like this one hold important, not-to-be-forgotten information that is directly relevant to the surrounding text.


NOTE Tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.

As for styles in the text:

  • We highlight new terms and important words when we introduce them.
  • We show keyboard strokes like this: Ctrl+A.
  • We show filenames, URLs, and code within the text like so: persistence.properties.
  • We present code in two different ways:
We use a monofont type with no highlighting for most code examples.

We use bold to emphasize code that is particularly important in the present context or to show changes from a previous code snippet.

SOURCE CODE

As you work through the examples in this book, you may choose either to type in all the code manually, or to use the source code files that accompany the book. All the source code used in this book is available for download at www.wrox.com. Specifically for this book, the code download is on the Download Code tab at:

www.wrox.com/remtitle.cgi?isbn=1118454766

You can also search for the book at www.wrox.com by ISBN (the ISBN for this book is 978-1-1184-5476-3 to find the code. And a complete list of code downloads for all current Wrox books is available at www.wrox.com/dynamic/books/download.aspx.

Most of the code on www.wrox.com is compressed in a .ZIP, .RAR archive or similar archive format appropriate to the platform. Once you download the code, just decompress it with an appropriate compression tool.


NOTE Because many books have similar titles, you may find it easiest to search by ISBN; this book’s ISBN is 978-1-118-45476-3.

Once you download the code, just decompress it with your favorite compression tool. Alternatively, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

There are also public Git repositories at https://github.com/aoabook where all the code for this book is published, and maintained.

ERRATA

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata you may save another reader hours of frustration and at the same time you will be helping us provide even higher quality information.

To find the errata page for this book, go to http://www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list including links to each book’s errata is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.

P2P.WROX.COM

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

1. Go to p2p.wrox.com and click the Register link.
2. Read the terms of use and click Agree.
3. Complete the required information to join as well as any optional information you wish to provide and click Submit.
4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

NOTE You can read messages in the forums without joining P2P but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset