Preface

One of the drivers behind the recent explosive growth in the Internet of Things has been the Bluetooth Low Energy (LE) standard. What makes it so appealing is the ubiquity of smartphones—both Apple and otherwise—with support for the standard, which means that thing-makers no longer have to worry about a display or user interface. And that means that things like smart lightbulbs can look a lot more like lightbulbs rather than a computer that happens to have a light attached to it.

Bluetooth LE is very different from classic Bluetooth—in fact, pretty much the only thing that is the same is the name. You’re probably used to thinking about radios as sort of like serial connections working similarly to a phone call between two phones—once you establish a connection, each person talks as the other listens and vice versa. They stay connected, even if neither is saying anything, until one hangs up and the call is ended.

In systems like these, data is transferred using a queue, and when data is read by the receiver, it’s erased from the queue—just as once my words reach your ears over the phone, they’re out of the communications channel. Effectively, this is how classic Bluetooth works.

What Makes Bluetooth LE Unique?

Instead of communicating via a point-to-point connection like a phone, a Bluetooth LE radio acts like a community bulletin board, with each radio acting as either a board or a reader of the board.

If your radio is a bulletin board—called a peripheral device in Bluetooth LE parlance—it posts data on its board for everyone in the community to read. If your radio is a reader—called a central device in Bluetooth LE terms—it can read from any of the boards (the peripheral devices) that have information it cares about.

If you don’t like that analogy, you can also think of peripheral devices as the servers in a client-server transaction. Similarly, central devices are the clients of the Bluetooth LE world because they read information from the peripherals.

But I Like Serial Connections!

Most (perhaps all?) of the Bluetooth LE radios breakout boards available to makers right now—the RedBearLab BLE mini and the Adafruit Bluefruit LE, for instance—pretend to look like serial devices for simplicity’s sake and present a UART service to the user. Effectively these radios are “faking” old-style serial communication on top of the underlying bulletin board paradigm. It’s a hack, and not a bad one.

Serial over Bluetooth LE makes the transition from classic Bluetooth to Bluetooth LE easier for people who are used to Serial Port Profile (SPP) and UART. However, there’s a downside. It doesn’t take advantage of Bluetooth LE. These Bluetooth serial services are just stuffing data across generic transmit and receive pipes. If a device uses serial, it also needs to define a protocol for the data that is sent and received.

Bluetooth LE offers device makers the ability to create Bluetooth LE devices with self-describing services. If the characteristics are well designed and the descriptors make sense, you can use services without documentation. (An example of this is using a Smartbotics Lightbulb with the LightBlue iOS app.)

Contrast this with other devices using SPP, like services where you need to learn the details of which bytes to send over the wire to turn on an LED and change its color. These devices need really good documentation and/or additional libraries to do simple stuff like set the color of a LED.

Returning to our bulletin board example, we’re creating a board (the service) that has a sticky note attached (known as a characteristic in Bluetooth LE parlance), which we can read, letting us know if the LED is on or off, or write to—allowing us to control the LED.

Building a Custom Service

Unfortunately, until recently, building custom services for Bluetooth LE has been fairly complicated and not for the faint-hearted. However, it’s getting simpler as several good tools now exist to do most of the heavy lifting for you.

In light of that, we decided to look at one platform—the Nordic Semiconductor radios—and figure out a complete toolchain that would allow you to build a custom service for those radios, and make use of that service from an Arduino project. We picked this particular radio because it’s readily available and there is good library support.

This book is the result.

Who Should Read This Book?

This book provides an introduction to the topic of how to build and deploy Bluetooth LE sensors and devices. It takes a hands-on approach and teaches you how to build things and how to use them, not just the protocols and architecture behind the standard.

If you’re a programmer or a maker who wants to get started with Bluetooth LE, this book is for you.

What You Should Already Know

This book is intended as an introduction to working with Bluetooth LE, and it assumes a technical background. However, we walk you through installing and using all the software and development environments you’ll need, including how to get started with the Arduino, Raspberry Pi, Node.js, and PhoneGap.

What You Will Learn

This book will guide you through building a series of connected projects—from lightbulbs, to locks, to beacons, and drones. It will walk you through your first hardware prototypes, show you how to improve them, and teach you how to build Bluetooth LE connected devices for the Internet of Things.

What’s In This Book

Chapter 1, Introduction

This chapter talks about the Bluetooth LE standard and the concepts you’ll need to know about before you can start building Bluetooth LE devices.

Chapter 2, Getting Started

This chapter walks you through setting up the tools and software you’ll need to set up your development environment.

Chapter 3, Smart Light Switch

This chapter shows you how to build a smart light switch that not only lets you turn the light on or off using the switch but also remotely via Bluetooth LE. The switch knows its current status—in other words, whether the bulb is on or off—and will send out a notification if that status changes.

Chapter 4, BLE Lock

This chapter shows you how to build a lock that can be opened using your phone via Bluetooth LE. We will also walk through writing a mobile app that will run on iOS or Android using PhoneGap, which will control the lock.

Chapter 5, Bleno Lock

This chapter re-creates the Bluetooth LE lock built in the previous chapter but this time using Node.js on the Raspberry Pi. It uses the same lock service as the original Bluetooth LE lock so it can be controlled using the same mobile app.

Chapter 6, Weather Station

This chapter walks you through building a Bluetooth-LE-enabled weather station that can measure temperature, humidity, and pressure.

Chapter 7, NeoPixel Lamp

This chapter will show you how to build an RGB lamp that is controllable from your phone using an Arduino and a NeoPixel Ring with 16 LEDs.

Chapter 8, SensorTag Remote

This chapter shows you how to turn a TI SensorTag into a remote control for your computer, triggering an action on your computer when one of the SensorTag buttons is pressed.

Chapter 9, HID over GATT

This chapter explores the Human Interface Device (HID) profile and shows you how to build a Bluetooth LE volume control.

Chapter 10, Beacons

In this chapter we explore Bluetooth LE beacons, showing how to create them using Node.js and detect them using smartphones.

Chapter 11, Drones

This chapter shows you how to control a Parrot Rolling Spider drone over Bluetooth LE using a computer and Node.js.

Chapter 12, Going Further

This chapter provides a collection of pointers to more advanced material on the topics we covered in the book, and material covering some of those topics that we didn’t manage to talk about in this book.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Tip

This element signifies a general note, tip, or suggestion.

Warning

This element indicates a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from Make: books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. All the code from this book is available on GitHub at https://github.com/MakeBluetooth.

Support material for the book is also available at the book’s website.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Make: Bluetooth by Alasdair Allan, Don Coleman, Sandeep Mistry (O’Reilly). Copyright 2016 Alasdair Allan, Don Coleman, Sandeep Mistry, 978-1-457-18709-4.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals.

Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • Make:
  • 1160 Battery Street East, Suite 125
  • San Francisco, CA 94111
  • 877-306-6253 (in the United States or Canada)
  • 707-639-1355 (international or local)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/make-BT.

Make: unites, inspires, informs, and entertains a growing community of resourceful people who undertake amazing projects in their backyards, basements, and garages. Make: celebrates your right to tweak, hack, and bend any technology to your will. The Make: audience continues to be a growing culture and community that believes in bettering ourselves, our environment, our educational system—our entire world. This is much more than an audience, it’s a worldwide movement that Make is leading we call it the Maker Movement.

For more information about Make:, visit us online:

To comment or ask technical questions about this book, send email to .

Acknowledgements by Alasdair Allan

Everyone has one book in them, but this isn’t mine. Depending how you count them, this is my ninth book. But every book is different, and they do not write themselves. So I’d like to thank my co-authors Don Coleman and Sandeep Mistry, and my editors at Make:, Brian Jepson and Roger Stewart, for holding my hand throughout the process.

I very much want to thank my wife, Gemma Hobson, for her continued support and encouragement. Those small, and sometimes larger, sacrifices an author’s spouse routinely has to make don’t get any less inconvenient the second, or third, or the n’th time around. I’m not sure why she lets me write—perhaps because I claim to enjoy it so much. Thank you, Gemma. Finally to my son Alex who, seven years on from my first book, is actually almost old enough to read this one.

Acknowledgements by Don Coleman

I’d like to thank my wife, Meghan, and son, Liam, for their support and patience during the many hours I spent working on this book. Tom Igoe was very helpful by working through ideas and testing early versions of code and libraries with his students. Guan Yang’s work on Arduino nRF8001 provided a huge kickstart in getting custom BLE services running on Arduino without proprietary tools. Brian Jepson, Roger Stewart, and the team at Make: deserve a shout-out for their patience with us and their persistence in getting this book published. I appreciate the support of my colleagues at Chariot Solutions, LLC. Lastly, it’s been awesome working with my talented co-authors Alasdair and Sandeep.

Acknowledgements by Sandeep Mistry

This is the first book I’ve been involved in. I would like to thank my co-authors Alasdair Allan and Don Coleman, and the editors Brian Jepson and Roger Stewart for guiding me through the process and providing valuable feedback throughout its production.

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

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