23
Beginning Sensor Networks with Arduino and Raspberry Pi Charles Bell

Beginning Sensor Networks with Arduino and Raspberry Pi978-1-4302-5825-4/1.pdf · v Contents at a Glance About the Author

Embed Size (px)

Citation preview

Beginning Sensor Networks with Arduino

and Raspberry Pi

Charles Bell

Beginning Sensor Networks with Arduino and Raspberry Pi

Copyright © 2013 by Charles Bell

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.

ISBN-13 (pbk): 978-1-4302-5824-7

ISBN-13 (electronic): 978-1-4302-5825-4

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

President and Publisher: Paul ManningLead Editor: Michelle LowmanDevelopmental Editor: Tom WelshTechnical Reviewer: Andrew MorganEditorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, James DeWolf,

Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Steve Weiss, Tom Welsh

Coordinating Editor: Jill BalzanoCopy Editor: Tiffany TaylorCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi GlobalCover Designer: Anna Ishchenko

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

For information on translations, please e-mail [email protected], or visit www.apress.com.

Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.

Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ .

I dedicate this book to my loving wife, Annette.

—Dr. Bell

v

Contents at a Glance

About the Author ���������������������������������������������������������������������������������������������������������������� xv

About the Technical Reviewer ������������������������������������������������������������������������������������������ xvii

Acknowledgments ������������������������������������������������������������������������������������������������������������� xix

Introduction ����������������������������������������������������������������������������������������������������������������������� xxi

Chapter 1: Introduction to Sensor Networks ■ ���������������������������������������������������������������������1

Chapter 2: Tiny Talking Modules: An Introduction to XBee Wireless Modules ■ ����������������19

Chapter 3: Arduino-Based Sensor Nodes ■ ������������������������������������������������������������������������51

Chapter 4: Raspberry Pi-based Sensor Nodes ■ �����������������������������������������������������������������97

Chapter 5: Where to Put It All: Storing Sensor Data ■ ������������������������������������������������������145

Chapter 6: Turning Your Raspberry Pi into a Database Server ■ ��������������������������������������191

Chapter 7: MySQL and Arduino: United at Last! ■ ������������������������������������������������������������223

Chapter 8: Building Your Network: Arduino Wireless Aggregator + Wireless ■Sensor Node + Raspberry Pi Server ������������������������������������������������������������������������������267

Chapter 9: Planning Wireless Sensor Networks ■ ������������������������������������������������������������309

Appendix: Shopping List ■ ������������������������������������������������������������������������������������������������335

Index ���������������������������������������������������������������������������������������������������������������������������������339

vii

Contents

About the Author ���������������������������������������������������������������������������������������������������������������� xv

About the Technical Reviewer ������������������������������������������������������������������������������������������ xvii

Acknowledgments ������������������������������������������������������������������������������������������������������������� xix

Introduction ����������������������������������������������������������������������������������������������������������������������� xxi

Chapter 1: Introduction to Sensor Networks ■ ���������������������������������������������������������������������1

Anatomy of a Sensor Network �������������������������������������������������������������������������������������������������������1

Examples of Sensor Networks ������������������������������������������������������������������������������������������������������������������������������� 1

The Topology of a Sensor Network ������������������������������������������������������������������������������������������������������������������������ 6

Communication Media ������������������������������������������������������������������������������������������������������������������7

Wired Networks ����������������������������������������������������������������������������������������������������������������������������������������������������� 7

Wireless Networks ������������������������������������������������������������������������������������������������������������������������������������������������� 8

Hybrid Networks ���������������������������������������������������������������������������������������������������������������������������������������������������� 8

Types of Sensor Nodes ������������������������������������������������������������������������������������������������������������������8

Basic Sensor Nodes ����������������������������������������������������������������������������������������������������������������������������������������������� 8

Data Nodes ������������������������������������������������������������������������������������������������������������������������������������������������������������ 9

Aggregator Nodes �������������������������������������������������������������������������������������������������������������������������������������������������� 9

Sensors ���������������������������������������������������������������������������������������������������������������������������������������10

How Sensors Measure ����������������������������������������������������������������������������������������������������������������������������������������� 11

Storing Sensor Data ��������������������������������������������������������������������������������������������������������������������������������������������� 13

Examples of Sensors ������������������������������������������������������������������������������������������������������������������������������������������� 14

Summary �������������������������������������������������������������������������������������������������������������������������������������17

■ Contents

viii

Chapter 2: Tiny Talking Modules: An Introduction to XBee Wireless Modules ■ ����������������19

What Is an XBee? ������������������������������������������������������������������������������������������������������������������������19

XBee Primer ��������������������������������������������������������������������������������������������������������������������������������20

Choosing XBee Modules �������������������������������������������������������������������������������������������������������������������������������������� 20

Interacting with an XBee-ZB Module ������������������������������������������������������������������������������������������������������������������� 22

Pin Layout ������������������������������������������������������������������������������������������������������������������������������������������������������������ 25

Configuring Modules�������������������������������������������������������������������������������������������������������������������������������������������� 26

For More Information ������������������������������������������������������������������������������������������������������������������������������������������� 35

An XBee Wireless Chat Room ������������������������������������������������������������������������������������������������������36

Loading the Firmware for the Modules ��������������������������������������������������������������������������������������������������������������� 36

Capturing Serial Numbers ����������������������������������������������������������������������������������������������������������������������������������� 38

Configuring the Coordinator �������������������������������������������������������������������������������������������������������������������������������� 39

Configuring the Router ���������������������������������������������������������������������������������������������������������������������������������������� 39

Let the Chat Begin ����������������������������������������������������������������������������������������������������������������������������������������������� 40

For More Fun ������������������������������������������������������������������������������������������������������������������������������������������������������� 42

Building an XBee-ZB Mesh Network �������������������������������������������������������������������������������������������42

Loading the Firmware for the Modules ��������������������������������������������������������������������������������������������������������������� 43

Configuring the XBee Modules ���������������������������������������������������������������������������������������������������������������������������� 43

Forming Test Messages ��������������������������������������������������������������������������������������������������������������������������������������� 43

Testing the Network ��������������������������������������������������������������������������������������������������������������������������������������������� 44

For More Fun ������������������������������������������������������������������������������������������������������������������������������������������������������� 48

Component Shopping List �����������������������������������������������������������������������������������������������������������48

Troubleshooting Tips and Common Issues ����������������������������������������������������������������������������������49

Things to Check ��������������������������������������������������������������������������������������������������������������������������������������������������� 49

Common Issues ��������������������������������������������������������������������������������������������������������������������������������������������������� 50

Summary �������������������������������������������������������������������������������������������������������������������������������������50

Chapter 3: Arduino-Based Sensor Nodes ■ ������������������������������������������������������������������������51

What Is an Arduino? ��������������������������������������������������������������������������������������������������������������������51

Arduino Models ���������������������������������������������������������������������������������������������������������������������������������������������������� 52

Arduino Clones ���������������������������������������������������������������������������������������������������������������������������������������������������� 57

■ Contents

ix

So, Which Do I Buy? ��������������������������������������������������������������������������������������������������������������������������������������������� 63

Where to Buy ������������������������������������������������������������������������������������������������������������������������������������������������������� 63

Arduino Tutorial ���������������������������������������������������������������������������������������������������������������������������64

Learning Resources ��������������������������������������������������������������������������������������������������������������������������������������������� 65

The Arduino IDE ��������������������������������������������������������������������������������������������������������������������������������������������������� 65

Project: Hardware “Hello, World!” ������������������������������������������������������������������������������������������������������������������������ 68

Hosting Sensors with Arduino �����������������������������������������������������������������������������������������������������73

Project: Building an Arduino Temperature Sensor �����������������������������������������������������������������������74

Hardware Setup ��������������������������������������������������������������������������������������������������������������������������������������������������� 75

Software Setup ���������������������������������������������������������������������������������������������������������������������������������������������������� 76

Writing the Sketch ����������������������������������������������������������������������������������������������������������������������������������������������� 76

Test Execution ����������������������������������������������������������������������������������������������������������������������������������������������������� 80

Project: Using an Arduino as a Data Collector for XBee Sensor Nodes ���������������������������������������81

XBee Sensor Node ����������������������������������������������������������������������������������������������������������������������������������������������� 81

Arduino with XBee Shield ������������������������������������������������������������������������������������������������������������������������������������ 87

Testing the Final Project �������������������������������������������������������������������������������������������������������������������������������������� 94

For More Fun ������������������������������������������������������������������������������������������������������������������������������������������������������� 95

Component Shopping List �����������������������������������������������������������������������������������������������������������95

Summary �������������������������������������������������������������������������������������������������������������������������������������96

Chapter 4: Raspberry Pi-based Sensor Nodes ■ �����������������������������������������������������������������97

What Is a Raspberry Pi? ��������������������������������������������������������������������������������������������������������������97

Noble Origins ������������������������������������������������������������������������������������������������������������������������������������������������������� 98

Models ����������������������������������������������������������������������������������������������������������������������������������������������������������������� 99

A Tour of the Board �������������������������������������������������������������������������������������������������������������������������������������������� 100

Required Accessories ���������������������������������������������������������������������������������������������������������������������������������������� 102

Recommended Accessories ������������������������������������������������������������������������������������������������������������������������������ 103

Where to Buy ����������������������������������������������������������������������������������������������������������������������������������������������������� 106

Raspberry Pi Tutorial �����������������������������������������������������������������������������������������������������������������109

Getting Started �������������������������������������������������������������������������������������������������������������������������������������������������� 109

Installing a Boot Image �������������������������������������������������������������������������������������������������������������������������������������� 109

■ Contents

x

Booting Up ��������������������������������������������������������������������������������������������������������������������������������������������������������� 111

GPIO Pin Mapping ���������������������������������������������������������������������������������������������������������������������������������������������� 114

Required Software ��������������������������������������������������������������������������������������������������������������������������������������������� 117

Project: Hardware “Hello, World!” ���������������������������������������������������������������������������������������������������������������������� 118

Hosting Sensors with Raspberry Pi �������������������������������������������������������������������������������������������123

Project: Building a Raspberry Temperature Sensor Node����������������������������������������������������������123

Hardware Setup ������������������������������������������������������������������������������������������������������������������������������������������������� 123

Testing the Hardware ����������������������������������������������������������������������������������������������������������������������������������������� 125

Software Setup �������������������������������������������������������������������������������������������������������������������������������������������������� 126

Testing the Sensor ��������������������������������������������������������������������������������������������������������������������������������������������� 129

For More Fun ����������������������������������������������������������������������������������������������������������������������������������������������������� 130

Project: Building a Raspberry Barometric Pressure Sensor Node ���������������������������������������������130

Hardware Setup ������������������������������������������������������������������������������������������������������������������������������������������������� 132

Testing the Hardware ����������������������������������������������������������������������������������������������������������������������������������������� 133

Software Setup �������������������������������������������������������������������������������������������������������������������������������������������������� 133

Testing the Sensor ��������������������������������������������������������������������������������������������������������������������������������������������� 135

For More Fun ����������������������������������������������������������������������������������������������������������������������������������������������������� 136

Project: Creating a Raspberry Pi Data Collector for XBee Sensor Nodes ����������������������������������137

XBee Sensor Node ��������������������������������������������������������������������������������������������������������������������������������������������� 137

Hardware ����������������������������������������������������������������������������������������������������������������������������������������������������������� 137

Software ������������������������������������������������������������������������������������������������������������������������������������������������������������ 139

Testing the Final Project ������������������������������������������������������������������������������������������������������������������������������������ 142

For More Fun ����������������������������������������������������������������������������������������������������������������������������������������������������� 142

Component Shopping List ���������������������������������������������������������������������������������������������������������143

Summary �����������������������������������������������������������������������������������������������������������������������������������144

Chapter 5: Where to Put It All: Storing Sensor Data ■ ������������������������������������������������������145

Storage Methods �����������������������������������������������������������������������������������������������������������������������145

Local Storage Options for the Arduino ���������������������������������������������������������������������������������������146

Nonvolatile Memory ������������������������������������������������������������������������������������������������������������������������������������������� 146

SD Card �������������������������������������������������������������������������������������������������������������������������������������������������������������� 147

■ Contents

xi

Project: Saving Data in Nonvolatile Memory������������������������������������������������������������������������������������������������������ 147

Project: Writing Data to an SD Card ������������������������������������������������������������������������������������������������������������������� 158

Local Storage Options for the Raspberry Pi ������������������������������������������������������������������������������167

Project: Writing Data to Files ����������������������������������������������������������������������������������������������������������������������������� 168

Remote Storage Options �����������������������������������������������������������������������������������������������������������169

Storing Data in the Cloud ����������������������������������������������������������������������������������������������������������������������������������� 169

Storing Sensor Data in a Database �������������������������������������������������������������������������������������������������������������������� 188

Component Shopping List ���������������������������������������������������������������������������������������������������������188

Summary �����������������������������������������������������������������������������������������������������������������������������������189

Chapter 6: Turning Your Raspberry Pi into a Database Server ■ ��������������������������������������191

What Is MySQL? ������������������������������������������������������������������������������������������������������������������������191

Getting Started with MySQL ������������������������������������������������������������������������������������������������������194

How and Where MySQL Stores Data ������������������������������������������������������������������������������������������������������������������ 195

The MySQL Configuration File ��������������������������������������������������������������������������������������������������������������������������� 199

How to Start, Stop, and Restart MySQL ������������������������������������������������������������������������������������������������������������� 199

Creating Users and Granting Access ����������������������������������������������������������������������������������������������������������������� 200

MySQL and Python—MySQL Utilities ���������������������������������������������������������������������������������������������������������������� 201

Building a Raspberry Pi MySQL Server �������������������������������������������������������������������������������������204

Partitioning and Formatting the Drive ��������������������������������������������������������������������������������������������������������������� 204

Setting Up Automatic Drive Mounting ���������������������������������������������������������������������������������������������������������������� 207

Project: Installing MySQL Server on a Raspberry Pi ������������������������������������������������������������������������������������������ 208

Advanced Project: Using MySQL Replication to Back Up Your Sensor Data ������������������������������������������������������ 214

Component Shopping List ���������������������������������������������������������������������������������������������������������222

Summary �����������������������������������������������������������������������������������������������������������������������������������222

Chapter 7: MySQL and Arduino: United at Last! ■ ������������������������������������������������������������223

Introducing Connector/Arduino �������������������������������������������������������������������������������������������������223

Hardware Requirements ������������������������������������������������������������������������������������������������������������������������������������ 224

What About Memory? ���������������������������������������������������������������������������������������������������������������������������������������� 227

How to Get MySQL Connector/Arduino �������������������������������������������������������������������������������������������������������������� 227

Limitations ��������������������������������������������������������������������������������������������������������������������������������������������������������� 229

■ Contents

xii

Building Connector/Arduino-Enabled Sketches ������������������������������������������������������������������������229

Database Setup ������������������������������������������������������������������������������������������������������������������������������������������������� 229

Setting Up the Arduino ��������������������������������������������������������������������������������������������������������������������������������������� 231

Starting a New Sketch ��������������������������������������������������������������������������������������������������������������������������������������� 232

Testing the Sketch ��������������������������������������������������������������������������������������������������������������������������������������������� 234

Troubleshooting Connector/Arduino ������������������������������������������������������������������������������������������240

MySQL Server Configuration������������������������������������������������������������������������������������������������������������������������������ 240

MySQL User Account Problems ������������������������������������������������������������������������������������������������������������������������� 242

Networking Configuration ���������������������������������������������������������������������������������������������������������������������������������� 243

Connector Installation ���������������������������������������������������������������������������������������������������������������������������������������� 244

Other ������������������������������������������������������������������������������������������������������������������������������������������������������������������ 244

None of These Solved My Problem—What Next?���������������������������������������������������������������������������������������������� 245

A Tour of the MySQL Connector/Arduino Code ��������������������������������������������������������������������������246

Library Files ������������������������������������������������������������������������������������������������������������������������������������������������������� 246

Field Structure ��������������������������������������������������������������������������������������������������������������������������������������������������� 247

Public Methods �������������������������������������������������������������������������������������������������������������������������������������������������� 248

Example Uses ���������������������������������������������������������������������������������������������������������������������������������������������������� 249

Project: Building a MySQL Arduino Client ����������������������������������������������������������������������������������252

Hardware Setup ������������������������������������������������������������������������������������������������������������������������������������������������� 252

Software Setup �������������������������������������������������������������������������������������������������������������������������������������������������� 254

Setting Up the Sensor Database ������������������������������������������������������������������������������������������������������������������������ 254

Writing the Code ������������������������������������������������������������������������������������������������������������������������������������������������ 254

Test Execution ��������������������������������������������������������������������������������������������������������������������������������������������������� 257

For More Fun ����������������������������������������������������������������������������������������������������������������������������������������������������� 259

Project Example: Inserting Data from Variables ������������������������������������������������������������������������259

Project Example: How to Perform SELECT Queries �������������������������������������������������������������������260

Displaying a Result Set in the Serial Monitor ���������������������������������������������������������������������������������������������������� 260

Writing Your Own Display Method ��������������������������������������������������������������������������������������������������������������������� 261

Example: Getting a Lookup Value from the Database ���������������������������������������������������������������������������������������� 263

Component Shopping List ���������������������������������������������������������������������������������������������������������265

Summary �����������������������������������������������������������������������������������������������������������������������������������265

■ Contents

xiii

Chapter 8: Building Your Network: Arduino Wireless Aggregator + Wireless ■Sensor Node + Raspberry Pi Server ������������������������������������������������������������������������������267

Data-Aggregate Nodes ��������������������������������������������������������������������������������������������������������������267

Local-Storage Data Aggregator ������������������������������������������������������������������������������������������������������������������������� 268

Project: Data-Aggregate Node with Local Storage �������������������������������������������������������������������������������������������� 269

Remote-Storage Data Aggregator ��������������������������������������������������������������������������������������������������������������������� 288

Project: Arduino Data-Aggregate Node with Database Storage ������������������������������������������������������������������������� 289

Project: Raspberry Pi Data-Aggregate Node with Database Storage ���������������������������������������������������������������� 299

Component Shopping List ���������������������������������������������������������������������������������������������������������306

Summary �����������������������������������������������������������������������������������������������������������������������������������307

Chapter 9: Planning Wireless Sensor Networks ■ ������������������������������������������������������������309

Sensor Networks Best Practices �����������������������������������������������������������������������������������������������309

Considerations for Data-Aggregate Nodes �������������������������������������������������������������������������������������������������������� 309

Considerations for Sensor Network Databases ������������������������������������������������������������������������������������������������� 312

Other Considerations ����������������������������������������������������������������������������������������������������������������������������������������� 316

Choosing Sensor Nodes ������������������������������������������������������������������������������������������������������������321

Wired Or Wireless? �������������������������������������������������������������������������������������������������������������������������������������������� 321

Arduino or Raspberry Pi? ����������������������������������������������������������������������������������������������������������������������������������� 321

Alternative Hosts ����������������������������������������������������������������������������������������������������������������������������������������������� 324

Project: Home Temperature-Monitoring Network ����������������������������������������������������������������������329

Planning Considerations ������������������������������������������������������������������������������������������������������������������������������������ 329

Planning the Nodes�������������������������������������������������������������������������������������������������������������������������������������������� 329

Cost Considerations ������������������������������������������������������������������������������������������������������������������������������������������� 330

What About Implementation? ���������������������������������������������������������������������������������������������������������������������������� 331

Conclusion ��������������������������������������������������������������������������������������������������������������������������������������������������������� 331

For More Fun ����������������������������������������������������������������������������������������������������������������������������������������������������� 332

Optional Component Shopping List �������������������������������������������������������������������������������������������332

Summary �����������������������������������������������������������������������������������������������������������������������������������332

Appendix: Shopping List ■ ������������������������������������������������������������������������������������������������335

Index ���������������������������������������������������������������������������������������������������������������������������������339

xv

About the Author

Dr. Charles A. Bell conducts research in emerging technologies. He is a member of the Oracle MySQL Development team and is the team lead for the MySQL Utilities team. He lives in a small town in rural Virginia with his loving wife. He received his Doctor of Philosophy in Engineering from Virginia Commonwealth University in 2005. Dr. Bell is an expert in the database field and has extensive knowledge and experience in software development and systems engineering. His research interests include microcontrollers, three-dimensional printing, database systems, software engineering, and sensor networks. He spends his limited free time as a practicing Maker focusing on microcontroller projects and refinement of three-dimensional printers.Dr. Bell maintains a blog on his research projects and many other interests. You can find his blog at http://drcharlesbell.blogspot.com/ .

xvii

About the Technical Reviewer

Andrew Morgan is currently a product manager with Oracle Corporation, responsible for MySQL’s High Availability solutions, including replication and MySQL Cluster—this gives him lots of opportunity to travel to present at conferences or meet customers, and his record is five continents, seven countries, and nine cities within a week. He first combined his interest in the Raspberry Pi and MySQL when back in 2012 he decided to find out what it would take to get MySQL Cluster (the super-tough, hugely scalable, telco-grade database) running on the humble Pi (http://www.clusterdb.com/mysql-cluster/mysql-cluster-running-on-raspberry-pi/). Earlier in his career, he worked on telecoms software development at Nortel. Andrew holds a BSc in Computer Science from the University of Warwick. You can read Andrew’s blog at www.clusterdb.com or follow him on Twitter @andrewmorgan.

xix

Acknowledgments

I would like to thank all of the many talented and energetic professionals at Apress. I appreciate the understanding and patience of my managing editor, Michelle Lowman, coordinating editor, Jill Balzano, and development editor, Tom Welsh. Each was instrumental in the success of this project. I appreciate their encouragement and guidance. I would also like to thank the small army of publishing professionals at Apress for making me look so good in print. Thank you all very much!

I’d like to especially thank the technical reviewer and my colleague, Andrew, at Oracle for his patience, insight, and impressive attention to detail. Most importantly, I want to thank my wife, Annette, for her unending patience and understanding during the many hours I spent hunched over my laptop or conducting experiments on the dining table.

xxi

Introduction

The world of microcontrollers and increasingly capable and popular small computing platforms is enabling many more people to learn, experience, and complete projects that would previously have required dedicated (and expensive) hardware. Rather than purchase a commercial or made-for-consumers kit, enterprising developers can now build their own solutions to meet their needs. Sensor networks are just one example of how these small, powerful, and inexpensive components have made it possible for anyone with a moderate skill set to build their own sensor network.

This book presents a beginner’s guide to sensor networks. I cover topics including what types of sensors exist, how they communicate their values (observations or events), how they can be used in Arduino and Raspberry Pi projects, and how to build your own home temperature sensor network.

I also include an introduction to the MySQL database server and how you can connect to, store, and retrieve data. Why, I even show you how to do it directly from an Arduino!

Who This Book Is ForI have written this book with a wide variety of readers in mind. It is intended for anyone who wants to get started building their own sensor networks or those who want to learn how to use components, devices, and sensors with an Arduino or Raspberry Pi.

Whether you have already been working with sensor networks, or maybe have taken an introductory electronics course, or even have read a good Apress book on the Arduino or Raspberry Pi, you will get a lot out of this book. Best of all, if you ever wanted to know how to combine sensors, Arduinos, XBee, MySQL, and Raspberry Pi to form a cohesive solution, this book is just what you need!

Most importantly, I wrote this book to meet my own needs. Although there are some excellent books on the Arduino, Raspberry Pi, sensors, and MySQL, I could not find a single reference that showed how to put all of these together. That is, until now.

About the ProjectsThere are nine chapters, seven of which include projects that demonstrate and teach key concepts of building sensor networks. Depending on your skill level with the chapter topic, you may find some of the projects easier to complete than others. It is my hope that you find the projects challenging and enlightening (but, more importantly, informative) so that you can complete your own sensor network projects.

In this section, I present some guidance on how best to succeed and get the most out of the projects.

StrategiesI have tried to construct the projects so that the majority of readers can accomplish them with little difficulty. If you encounter topics that you are very familiar with, I recommend working through the projects anyway instead of simply reading or skipping through the instructions. This is because some of the later projects build on the earlier projects.

■ IntroduCtIon

xxii

On the other hand, if you encounter topics that you are unfamiliar with, I recommend reading through the chapter or section completely at least once before attempting the project. Take some time to fully absorb the material, and pay particular attention to the numerous links, tips, and cautionary portions. Some of those are pure gold for beginners.

Perhaps the most significant advice I can offer when approaching the projects is to attempt them one at a time. By completing the projects one at a time, you gain knowledge that you can build on for future projects. It also helps your establish a pace to work through the book. Although some accomplished readers can probably complete all the projects in a weekend, I recommend working through the book at a pace best suited for your availability (and enjoyment).

With some exceptions, the earlier chapters are independent and can be tackled in any order. This is especially true for the Arduino (Chapter 4) and Raspberry Pi (Chapter 5) chapters. Regardless, it is a good idea to read the book and work on the projects in order.

Tips for Buying HardwareThe hardware list for this book contains a number of common components such as temperature sensors, breadboards, jumper wires, and resistors. Most of these items can be found in electronics stores that stock supplies for electronics enthusiasts. The list also includes a number of specialized components such as XBee modules, XBee adapters, XBee shields, Arduino boards, and Raspberry Pi boards.

Each chapter has a list of the components used at the end of the chapter. In some cases, you reuse the hardware from previous chapters. I include a separate list for these items. I have placed the component lists at the end of each chapter to encourage you to read the chapter before attempting the projects.

The lists include the name of each component and at least one link to an online vendor that stocks the component. In addition, I include the quantity needed for the chapter and an estimated cost. If you add up all the components needed and sum the estimated cost, the total may be a significant investment for some readers.

The following sections are for anyone looking to save a little on the cost of completing the projects in this book or wanting to build up their own inventory of sensor network hardware on a budget.

Buy Only What You Need When You Need ItOne way to mitigate a significant initial investment in hardware is to pace your buying. If you follow my advice and work on one project at a time, you can purchase only the hardware needed for that project. This will allow you to spread the cost over however long you plan to work through the book.

However, if you are buying your hardware from an online retailer, you may want to balance ordering the hardware for one project at a time against the potentially higher total shipping cost for multiple orders.

As mentioned, the more common electronics like LEDs, breadboards, and so on, can be found in traditional brick-and-mortar stores, but the cost may be a little higher. Once again, the cost of shipping to your location may dictate whether it would be cheaper to buy the higher-priced items from a local electronics shop versus an online retailer.

Online Auctions One possible way to save money is to buy your components at a discount on online auction sites. In many cases, the components are the very same ones listed. In other cases, the components may be from vendors that specialize in making less-expensive alternatives. I have had a lot of success in buying quality hardware from online auction sites (namely eBay).

If you are not in a hurry and have time to wait for auctions to close and the subsequent shipping times, you can sometimes find major components like Arduinos, shields, power supplies, and the like at a reduced price by bidding for them. For example, open source hardware manufacturers sometimes offer their products via auctions or at special pricing for quantities. I have found a number of Arduino clones and shields at nearly half the cost of the same boards found on other sites or in electronics stores.

■ IntroduCtIon

xxiii

A NOTE ABOUT NEWER ARDUINO BOARDS

Hey, Buddy, Can You Spare an Arduino?Another possible way to save some money on the hardware is to borrow it from your friends! If you have friends who are electronics, Arduino, or Raspberry Pi enthusiasts, chances are they have many of the components you need. Just be sure you return the components in working order!1

the projects in this book are designed for a current, readily available version of the Arduino. the projects can be completed with the duemilanove or uno boards without modification. Although you can use the Leonardo (see specific notes in the chapters about the differences), you should consider the newer boards carefully before buying.

some newer boards may require additional changes or extra steps to use. For example, the due is perfectly suitable and an excellent choice for projects that require larger sketches; but you must use the newest beta version of the Arduino Ide, which may require slight changes to your sketch. there are a couple of other things unique to the due, but I highlight these in the chapters. Finally, you can use the Yún, but you would be using only the Arduino side of the board so this may not be a cost-effective solution.

Downloading the CodeThe code for the examples shown in this book is available on the Apress web site, www.apress.com. A link can be found on the book’s information page under the Source Code/Downloads tab. This tab is located underneath the Related Titles section of the page.

Reporting ErrataShould you find a mistake in this book, please report it through the Errata tab on the book’s page at www.apress.com. You will find any previously confirmed errata in the same place.

1And replace the components you implode, explode, or otherwise turn into silicon slag. Hey, it happens.