Beginning Sensor Networks with XBee, Raspberry Pi, and Arduino: Sensing the World with Python and MicroPython 2nd Edition Charles Bell pdf download
Beginning Sensor Networks with XBee, Raspberry Pi, and Arduino: Sensing the World with Python and MicroPython 2nd Edition Charles Bell pdf download
https://textbookfull.com/product/beginning-robotics-with-
raspberry-pi-and-arduino-using-python-and-opencv-2nd-edition-
jeff-cicolani/
https://textbookfull.com/product/beginning-robotics-with-
raspberry-pi-and-arduino-using-python-and-opencv-2nd-edition-
jeff-cicolani-2/
https://textbookfull.com/product/beginning-robotics-with-
raspberry-pi-and-arduino-using-python-and-opencv-jeff-cicolani/
https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-
loucas/
Raspberry Pi Home Automation with Arduino 2nd Edition
Andrew K. Dennis
https://textbookfull.com/product/raspberry-pi-home-automation-
with-arduino-2nd-edition-andrew-k-dennis/
https://textbookfull.com/product/hacking-electronics-learning-
electronics-with-arduino-and-raspberry-pi-2nd-edition-simon-monk/
https://textbookfull.com/product/get-started-with-micropython-on-
raspberry-pi-pico-1st-edition-gareth-halfacree/
https://textbookfull.com/product/action-movement-light-and-sound-
with-arduino-and-raspberry-pi-first-edition-monk/
https://textbookfull.com/product/internet-of-things-with-
raspberry-pi-and-arduino-first-edition-edition-rajesh-singh/
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
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
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
Chapter 1 Introduction to Sensor Networks
A
utomotive
Almost every modern automobile has a network of sophisticated sensors
that monitor the performance of the engine and its subsystems. Some
cars have additional sensors for monitoring external air temperature, tire
pressure, and even proximity to objects and other vehicles. Newer vehicles
have a host of safety mechanisms including lane departure, obstacle
avoidance, auto braking, and more.1
If you take a late-model car in for service and get a chance to look in
the garage area, you may notice several machines that resemble computer
terminals, tablet computers, or in some cases an iPad. These systems
are diagnostic machines designed to connect to your car and read all the
data the sensors and computer have stored. Some manufacturers use the
industry standard interface called onboard diagnostics (OBD).2 There are
several versions of this interface and its protocols; most dealerships have
equipment that supports all the latest protocols.
1
I nterestingly, I have heard a few motorists who despise some of these features
because their driving habits place the vehicle more to one side of the road
or another, which triggers the lane departure warning. Similarly, those that
habitually cross the center line when driving on curving roads tend to turn off
the departure warning. Clearly one of these is an understandable annoyance,
whereas the other is exactly why the feature is needed.
2
http://en.wikipedia.org/wiki/On-board_diagnostics
3
Exploring the Variety of Random
Documents with Different Content
she must have had French blood in her veins, to breed such eyes,
and such a tongue, for the beautiful speech which came out of that
ugly (it was that) face, and the glitter and depth too of the eyes, like
live coals—perfectly honest the while, both lips and eyes—these
seemed to me to be attributes of the highest French, or rather Gallic,
not of the highest English, woman. In any case, she was a triumph
of mind over matter, of spirit over flesh, which gave the lie to all
materialism, and puts Professor Bain out of court—at least out of
court with those who use fair induction about the men and women
whom they meet and know.”—About 1851.
James Payn’s “I seem to see the dear little old lady now, looking like
Literary a venerable fairy, with bright sparkling eyes, a clear,
Recollections.
incisive voice, and a laugh that carried you away with
it. I never saw a woman with such an enjoyment of—I was about to
say a joke, but the word is too coarse for her—of a pleasantry. She
was the warmest of friends, and with all her love of fun never
alluded to their weaknesses.... I well remember our first interview. I
expected to find the authoress of Our Village in a most picturesque
residence, overgrown with honeysuckle and roses, and set in an old-
fashioned garden. Her little cottage at Swallowfield, near Reading,
did not answer this picture at all. It was a cottage, but not a pretty
one, placed where three roads met, with only a piece of green
before it. But if the dwelling disappointed me, the owner did not. I
was ushered upstairs (for at that time, crippled by rheumatism, she
was unable to leave her room) into a small apartment, lined with
books from floor to ceiling, and fragrant with flowers; its tenant rose
from her arm-chair with difficulty, but with a sunny smile and a
charming manner bade me welcome. My father had been an old
friend of hers, and she spoke of my home and belongings as only a
woman can speak of such things. Then we plunged, in medias res,
into men and books.”—1852.
LADY MARY WORTLEY MONTAGU
1690-1762
Leigh Hunt’s “Moore’s forehead was bony and full of character, with
Autobiograph ‘bumps’ of wit, large and radiant enough to transport a
y.
phrenologist. Sterne had such another. His eyes were
as dark and fine as you would wish to see under a set of vine-
leaves; his mouth generous and good-humoured, with dimples; and
his manner was as bright as his talk, full of the wish to please and
be pleased. He sang, and played with great taste on the pianoforte,
as might be supposed from his musical compositions. His voice,
which was a little hoarse in speaking (at least I used to think so),
softened into a breath, like that of a flute, when singing. In speaking
he was emphatic in rolling the letter r, perhaps out of a despair of
being able to get rid of the national peculiarity.”
S. C. Hall’s “His eyes sparkle like a champagne bubble; there is a
Memories of kind of wintry red, of the tinge of an October leaf, that
Great Men.
seems enamelled on his cheek; his lips are delicately
cut, slight, and changeable as an aspen; the slightly-turned nose
confirms the fun of the expression; and altogether it is a face that
sparkles, beams, and radiates—
‘The light that surrounds him is all from within.’”
1835.
S. C. Hall’s“I recall him at this moment—his small form and
Retrospect ofintellectual face rich in expression, and that expression
a Long Life.
the sweetest, the most gentle, and the kindliest. He
had still in age the same bright and clear eye, the same gracious
smile, the same suave and winning manner I had noticed as the
attributes of what might in comparison be styled his youth (I have
stated I knew him as long ago as 1821); a forehead not remarkably
broad or high, but singularly impressive, firm, and full, with the
organs of music and gaiety large, and those of benevolence and
veneration greatly preponderating; the nose, as observed in all his
portraits, was somewhat upturned. Standing or sitting, his head was
invariably upraised, owing, perhaps, mainly to his shortness of
stature. He had so much bodily activity as to give him the attribute
of restlessness, and no doubt that usual accompaniment of genius
was eminently a characteristic of his. His hair was, at the time I
speak of, thin and very gray, and he wore his hat with the jaunty air
that has been often remarked as a peculiarity of the Irish. In dress,
although far from slovenly, he was by no means precise. He had but
little voice, yet he sang with a depth of sweetness that charmed all
hearers; it was true melody, and told upon the heart as well as the
ear. No doubt much of this charm was derived from association, for
it was only his own melodies he sang.”—1845.
HANNAH MORE
1745-1833
Kemble’s “When I first knew Caroline Sheridan she had not long
Records of been married to the Hon. George Norton. She was
a Girlhood.
splendidly handsome, of an un-English character of
beauty, her rather large and heavy head and features recalling the
grandest Grecian and Italian models, to the latter of whom her rich
colouring and blue-black braids of hair gave her an additional
resemblance. Though neither as perfectly lovely as the Duchess of
Somerset, nor as perfectly charming as Lady Dufferin, she produced
a far more striking impression than either of them, by the
combination of the poetical genius with which she alone, of the
three, was gifted, with the brilliant power of repartee which they
(especially Lady Dufferin) possessed in common with her, united to
the exceptional beauty with which they were all three endowed. Mrs.
Norton was exceedingly epigrammatic in her talk, and comically
dramatic in her manner of relating things.... She was no musician,
but had a deep, sweet contralto voice, precisely the same in which
she always spoke, and which, combined with her always lowered
eyelids (‘downy eyelids’ with sweeping silken fringes), gave such
incomparably comic effect to her sharp retorts and ludicrous
stories.... I admired her extremely.—1827.
“The next time ... was at an evening party at my sister’s house,
where her appearance struck me more than it had ever done. Her
dress had something to do with this effect, no doubt. She had a rich
gold-coloured silk on, shaded and softened all over with black lace
draperies, and her splendid head, neck, and arms, were adorned
with magnificently simple Etruscan ornaments, which she had
brought from Rome, whence she had just returned, and where the
fashion of that famous antique jewellery had lately been revived.
She was still ‘une beauté triomphante à faire voir aux
ambassadeurs.’”
A personal “The most beautiful of ‘the beautiful Sheridans,’
friend. Caroline Norton will also live in the memory of her
friends as one of the most fascinating of women. Her
voice was exceedingly sweet and musical, her movements
wonderfully graceful, and, with the solitary exception of Theodore
Hook, whose rough, coarse wit spared no one, her queenly bearing
won her general adulation and deference. Her face was a pure oval,
her head was crowned by heavy braids of the darkest hair, while the
warmth and light which suffused her expressive countenance gave
her a somewhat un-English appearance. Her eyes were dark; black
curly lashes swept over the warmly-tinted cheek; the lips were of
geranium red; the teeth, dazzlingly white. Altogether she was a vivid
piece of colouring, and as she was always very beautifully dressed, it
did not require her literary reputation to make her at all times
sought after and admired.”
S. C. Hall’s“It seems but yesterday—it is not so very long ago
Retrospect ofcertainly—that I saw for the last time the Hon. Mrs.
a long Life.
Norton. Her radiant beauty was then faded, but her
stately form had been little impaired by years, and she had retained
much of the grace that made her early womanhood so surpassingly
attractive. She combined, in a singular degree, feminine delicacy
with masculine vigour; though essentially womanly, she seemed to
have the force of character of man. Remarkably handsome she
perhaps excited admiration rather than affection. I can easily
imagine greater love to be given to a far plainer woman. She had, in
more than full measure, the traditional beauty of her family, and no
doubt inherited with it some of the waywardness that is associated
with the name of Sheridan.”
THOMAS OTWAY
1651-1685
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com