Beginning Sensor Networks with Arduino and Raspberry Pi - Charles Bell.pdf

(13643 KB) Pobierz
Technology in AcTion
™
Beginning
Sensor Networks
with
Arduino
and
Raspberry P
i
Rebuild Old Toys with Arduino
LeARN how to mAke ARduiNo
ANd RASPBeRRy Pi-BASed
SeNSoR NetwoRkS
Charles Bell
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
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
v
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 For
I 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 Projects
There 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.
Strategies
I 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.
xxi
■
IntroduCtIon
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 Hardware
The 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 It
One 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.
xxii
Zgłoś jeśli naruszono regulamin