Beginning Sensor Networks With Xbee, Raspberry Pi, and Arduino: Sensing The World With Python and Micropython 2Nd Edition Charles Bell
Beginning Sensor Networks With Xbee, Raspberry Pi, and Arduino: Sensing The World With Python and Micropython 2Nd Edition Charles Bell
com
https://textbookfull.com/product/beginning-robotics-with-raspberry-pi-
and-arduino-using-python-and-opencv-2nd-edition-jeff-cicolani/
textbookfull.com
https://textbookfull.com/product/beginning-robotics-with-raspberry-pi-
and-arduino-using-python-and-opencv-2nd-edition-jeff-cicolani-2/
textbookfull.com
https://textbookfull.com/product/beginning-robotics-with-raspberry-pi-
and-arduino-using-python-and-opencv-jeff-cicolani/
textbookfull.com
https://textbookfull.com/product/30-days-to-market-mastery-a-step-by-
step-guide-to-profitable-trading-1st-edition-bernstein-jake/
textbookfull.com
Caves and Karst of the Upper Midwest, USA: Minnesota,
Iowa, Illinois, Wisconsin Greg A. Brick
https://textbookfull.com/product/caves-and-karst-of-the-upper-midwest-
usa-minnesota-iowa-illinois-wisconsin-greg-a-brick/
textbookfull.com
https://textbookfull.com/product/greetings-pushkin-stalinist-cultural-
politics-and-the-russian-national-bard-1st-edition-jonathan-brooks-
platt/
textbookfull.com
https://textbookfull.com/product/mathematics-is-the-poetry-of-science-
first-edition-edition-cedric-villani/
textbookfull.com
https://textbookfull.com/product/the-year-of-voting-dangerously-the-
derangement-of-american-politics-first-trade-paperback-edition-dowd/
textbookfull.com
https://textbookfull.com/product/german-military-geology-and-
fortification-of-the-british-channel-islands-during-world-war-ii-
edward-p-f-rose/
textbookfull.com
Rejected Wolf (Virga’s Doom #1) 1st Edition Rose
https://textbookfull.com/product/rejected-wolf-virgas-doom-1-1st-
edition-rose/
textbookfull.com
TECHNOLOGY IN AC TION™
Beginning Sensor
Networks with
XBee, Raspberry Pi,
and Arduino
Sensing the World with Python
and MicroPython
—
Second Edition
—
Charles Bell
Beginning Sensor
Networks with XBee,
Raspberry Pi, and
Arduino
Sensing the World with Python
and MicroPython
Second Edition
Charles Bell
Beginning Sensor Networks with XBee, Raspberry Pi, and Arduino:
Sensing the World with Python and MicroPython
Charles Bell
Warsaw, VA, USA
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
viii
Table of Contents
ix
Table of Contents
Arduino Tutorial�������������������������������������������������������������������������������������������������288
Learning Resources�������������������������������������������������������������������������������������289
The Arduino IDE�������������������������������������������������������������������������������������������290
Project: Hardware “Hello, World!”����������������������������������������������������������������294
Hosting Sensors with Arduino���������������������������������������������������������������������������300
Project: Building an Arduino Temperature Sensor���������������������������������������������302
Hardware Setup�������������������������������������������������������������������������������������������302
Software Setup��������������������������������������������������������������������������������������������304
Writing the Sketch���������������������������������������������������������������������������������������306
Test Execution���������������������������������������������������������������������������������������������310
Project: Using an Arduino As a Data Collector for XBee Sensor Nodes�������������312
XBee Sensor Node���������������������������������������������������������������������������������������312
Coordinator Node�����������������������������������������������������������������������������������������313
Arduino with XBee Shield����������������������������������������������������������������������������314
Testing the Final Project������������������������������������������������������������������������������326
For More Fun�����������������������������������������������������������������������������������������������328
Component Shopping List���������������������������������������������������������������������������������328
Summary����������������������������������������������������������������������������������������������������������331
x
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Table of Contents
xi
Table of Contents
xii
Table of Contents
xiii
Table of Contents
Appendix�������������������������������������������������������������������������������������������683
Consolidated Shopping Lists�����������������������������������������������������������������������������683
Alternative Connection Systems�����������������������������������������������������������������������691
Grove�����������������������������������������������������������������������������������������������������������������691
Qwiic�����������������������������������������������������������������������������������������������������������������695
STEMMA QT������������������������������������������������������������������������������������������������������700
Summary����������������������������������������������������������������������������������������������������������701
Index�������������������������������������������������������������������������������������������������703
xiv
About the Author
Charles Bell conducts research in emerging
technologies. He is a principal software
developer of the Oracle MySQL Development
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.
xv
About the Technical Reviewer
Sai Yamanoor is an embedded systems engineer working for an industrial
gases company in Buffalo, NY. His interests, deeply rooted in DIY and
open source hardware, include developing gadgets that aid behavior
modification. He has published two books with his brother, and in his
spare time, he likes to contribute to build things that improve quality of
life. You can find his project portfolio at http://saiyamanoor.com.
xvii
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, Natalie Pao; coordinating editor, Jessica Vakili; and development
editor, James Markham. Each was instrumental in the success of this
project. I appreciate their encouragement and guidance as well as patience
in dealing with my many questions. 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, Sai Yamanoor, 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 science experiments on the dining table.
xix
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!
Better still, this edition has been updated to include updated tools
and software, project examples, as well as the latest use and programming
of the XBee 3 modules. Yes, we’re writing code to control them in
MicroPython. There’s an entire chapter dedicated to MicroPython as well
as extended coverage of the XBee platform.
xxi
Introduction
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.
xxii
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 you 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 Raspberry Pi (Chapter 5)
and Arduino (Chapter 6) chapters. Regardless, it is a good idea to read the
book and work on the projects in order.
xxiii
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Introduction
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.
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).
xxiv
Introduction
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.
The projects in this book are designed for a current, readily available
version of the Arduino as well as the most recently retired boards. The
projects can be completed with the Uno or Mega 2560 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.
1
nd replace the components you implode, explode, or otherwise turn into silicon
A
slag. Hey, it happens.
xxv
Introduction
D
ownloading the Code
The code for the examples shown in this book is available on the Apress
website, 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.
R
eporting Errata
Should 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.
xxvi
CHAPTER 1
Introduction
to Sensor Networks
Sensor networks are no longer expensive industrial constructs. You can
build a simple sensor network from easily procured, low-cost hardware.
All you need are some simple sensors and a microcontroller or computer
with input/output capabilities. Yes, your Arduino and Raspberry Pi are
ideal platforms for building sensor networks. If you’ve worked with either
platform and have ever wanted to monitor your garden pond, track
movement in your home or office, monitor the temperature in your house,
monitor the environment, or even build a low-cost security system, you’re
halfway there!
As inviting and easy as that sounds, don’t start warming up the
soldering iron just yet. There are a lot of things you need to know about
sensor networks. It’s not quite as simple as plugging things together and
turning them on. If you want to build a reliable and informative sensor
network, you need to know how such networks are constructed.
In addition, you may have heard of something called the Internet of
Things (IoT). This phrase refers to the use of devices that can communicate
over a network (local or Internet). IoT devices are therefore network-aware
devices that can send data to other resources, thereby virtualizing the
effects of the devices on users and their experience. Sensor networks play
a prominent role in the IoT. What you will learn in this book will provide a
firm foundation for building IoT solutions using sensor networks.
If you want to know more about IoT in general, several books have
been written on the topic, including the following. If you’re interested in
learning more about the IoT and how sensor networks are used, check out
some of these titles:
• Building Internet of Things with the Arduino by
Charalampos Doukas (CreateSpace Independent
Publishing Platform, 2012)
2
Random documents with unrelated
content Scribd suggests to you:
The zircon can be distinguished from the garnet by its peculiar
diamond-like brilliancy and its specific gravity.
Turquois.
The turquois is never found in crystals, but in reniform or
stalactitic masses. The color varies from pea- and apple-green to
greenish-blue, sky-blue and dark-blue.
The hardness of the turquois is 6., specific gravity 2.6 to 2.8,
lustre waxy, and condition opaque to slightly translucent.
Before the reducing flame of the blow-pipe, the turquois does not
melt, but becomes brown and colors the flame green. With borax
and salts of phosphorus the turquois melts to a clear glass, while it
is also soluble in hydrochloric acid. Oriental or mineral turquois is
composed of:
Alumina 47.45
Phosphoric acid 27.34
Water 18.18
Oxide of copper 2.02
Iron 1.10
Oxide of manganese 0.50
Phosphate of lime 3.41
100.00
The best color is a clear deep sky-blue, and in the true turquois
this color improves by artificial light; imitation turquoises, however,
lose their fine color under the same conditions.
The finest gem turquoises come from the northeastern part of
Persia, between Nishapoor and Meshed. Here they are mined and
partly cut, and then the Persian merchants carry them to Russia,
where they are sold at the great annual fair of Nijni-Novgorod and in
Moscow. Mineral turquoises are also found in New Mexico, Arizona,
and Nevada, but not of sufficient size or sufficiently good color to
make gem stones, although they are prized for collections.
Specimens are also found in Burmah, Khorassan, Thibet, China,
Silesia, Saxony, and on the Isthmus of Suez. The stones from these
places have, as a rule, but little value, as the color fades or turns
green from exposure to the light. Of late however, some very good
turquoises have come from Egypt. The color of a faded Persian
turquois can sometimes be restored by simply repolishing the stone.
Occidental or bone turquoises called new rock or odontolites, to
distinguish them from the Persian or old rock stones, are of organic
origin.
They are cut from the teeth of mammoths, mastodons,
dinotheriums, etc., and are found near the town of Simor, in Lower
Languedoc, France.
These teeth, the enamel of which is nearly as hard as the mineral
turquois, are colored by contact with phosphate of iron and copper,
which gives them a dark-blue, light-blue, and bluish-green color.
They are easily attacked by a file, and totally destroyed by aqua-
fortis.
When heated, the fossil turquois or odontolite gives an offensive
odor, owing to the decomposition of animal matter.
The odontolite is lighter than the mineral turquois, changes color
by artificial light, loses color in distilled water and alcohol, and is
translucent on the edges.
This fossil turquois does not fade like the mineral turquois, but by
artificial light appears of a dirty grayish-blue.
Turquoises are sometimes artificially stained, but this can be
detected by applying a drop of ammonia to the back of the stone,
and if the color is artificial the ammonia will eat it off, leaving a
green spot. Ammonia does not affect the color of the Persian
turquois. The so-called “reconstructed” turquoises are very close
imitations of the real, but are easily distinguished, as they change
rapidly to a deeper blue when immersed in water, and while wet the
surface of the stone shows cracks in every direction. These stones
become softer through soaking in water or alcohol. The original
color, however, returns when the stone is dry, but the cracks remain
in faint outline.
Tourmaline.
The tourmaline or precious schorl is known under many different
names, and no other mineral has such a suite of colors.
The colorless variety is known as achroite; the red, as rubellite or
siberite; the blue, indicolite or Brazilian sapphire; the green, Brazilian
emerald; and the yellowish-green, Ceylon chrysolite or Ceylon
peridot. Besides the above colors and their shadings, the tourmaline
occurs in black and brown.
The crystallization is obtuse rhomboid, and generally forms six-,
nine-, and twelve-sided prisms.
Some of the crystals are very large, specimens over eight inches
long having been mined.
The tourmaline crystals are remarkable for their varied and
beautiful groupings of colors. Some are internally blue or brown,
surrounded by a bright carmine red or dull yellow; others are red
internally and are enveloped by a green exterior; crystals are
sometimes pink at the summit and light green at the base, or
crimson tipped with black, or white at one end shading into green
and finally into red at the other end. The hardness of the tourmaline
is 7 to 7.5, specific gravity 3 to 3.1, and lustre vitreous.
The tourmaline becomes decidedly electric by heating or rubbing,
and will readily attract small pieces of paper and other small objects.
The rubellite or red tourmaline is composed of:
Silica 42.13
Alumina 36.43
Boracic acid 5.74
Oxide of manganese 6.32
Lime 1.20
Potash 2.41
Lithia 2.04