Table Of ContentTECHNOLOGY IN ACTION™
Sensor
Projects with
Raspberry Pi
Internet of Things and Digital
Image Processing
—
Guillermo Guillen
Sensor Projects with
Raspberry Pi
Internet of Things and Digital
Image Processing
Guillermo Guillen
Sensor Projects with Raspberry Pi: Internet of Things and Digital
Image Processing
Guillermo Guillen
Ciudad de Mexico, Mexico
ISBN-13 (pbk): 978-1-4842-5298-7 ISBN-13 (electronic): 978-1-4842-5299-4
https://doi.org/10.1007/978-1-4842-5299-4
Copyright © 2019 by Guillermo Guillen
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.
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.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Aaron Black
Development Editor: James Markham
Coordinating Editor: Jessica Vakili
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 orders-ny@springer-sbm.com, 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 rights@apress.com, or visit www.apress.
com/rights-permissions.
Apress titles 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
Print and eBook Bulk Sales web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on GitHub via the book’s product page, located at www.apress.com/
978-1-4842-5298-7. For more detailed information, please visit www.apress.com/
source-code.
Printed on acid-free paper
This book is dedicated to all those people in my family
who are no longer with us, and who died…
my grandmother Sofia, my uncle Miguel, and
my brother Polo.
Table of Contents
About the Author ���������������������������������������������������������������������������������xi
About the Technical Reviewer �����������������������������������������������������������xiii
Acknowledgments ������������������������������������������������������������������������������xv
Introduction ��������������������������������������������������������������������������������������xvii
Chapter 1: Theoretical Fundamentals ���������������������������������������������������1
Programming with the Raspberry Pi ���������������������������������������������������������������������2
Elements of the Language �������������������������������������������������������������������������������4
Flow Control Structures �����������������������������������������������������������������������������������9
Important Differences Between Python 2�7�x and Python 3�x �����������������������16
Internet of Things ������������������������������������������������������������������������������������������������20
History �����������������������������������������������������������������������������������������������������������21
Smart Home Applications ������������������������������������������������������������������������������22
Elder Care Applications����������������������������������������������������������������������������������23
Medical and Healthcare Applications ������������������������������������������������������������23
Transportation Applications ���������������������������������������������������������������������������25
Building and Home Automation Applications �������������������������������������������������26
Manufacturing Applications ���������������������������������������������������������������������������27
Agriculture Applications ��������������������������������������������������������������������������������29
Metropolitan-Scale Deployments ������������������������������������������������������������������29
Energy Management Applications �����������������������������������������������������������������31
Environmental Monitoring Applications ���������������������������������������������������������32
Living Lab Application������������������������������������������������������������������������������������33
v
TTaabbllee ooff CCoonnTTeennTTss
IoT Security ����������������������������������������������������������������������������������������������������34
What’s the Difference Between OT and IoT? �������������������������������������������������34
Digital Image Processing ������������������������������������������������������������������������������������34
History �����������������������������������������������������������������������������������������������������������36
Tasks �������������������������������������������������������������������������������������������������������������37
Techniques ����������������������������������������������������������������������������������������������������39
Applications ���������������������������������������������������������������������������������������������������43
Summary�������������������������������������������������������������������������������������������������������������44
Chapter 2: Alarm System ��������������������������������������������������������������������47
Hardware ������������������������������������������������������������������������������������������������������������47
Software ��������������������������������������������������������������������������������������������������������������51
Procedure ������������������������������������������������������������������������������������������������������������54
Creating a Project in ThingSpeak ������������������������������������������������������������������54
Using Your Twitter Account in ThingSpeak ����������������������������������������������������57
Sending an Alert to Your Twitter Account �������������������������������������������������������59
Challenges ����������������������������������������������������������������������������������������������������������61
Conclusion ����������������������������������������������������������������������������������������������������������61
Chapter 3: Gas Leak Alarm �����������������������������������������������������������������63
Hardware ������������������������������������������������������������������������������������������������������������63
Software ��������������������������������������������������������������������������������������������������������������68
Procedure ������������������������������������������������������������������������������������������������������������70
Installing the ADS1115 Sensor Library ����������������������������������������������������������70
Enabling the 12C Interface ����������������������������������������������������������������������������71
Creating a Project in ThingSpeak ������������������������������������������������������������������71
Using Your Twitter Account in ThingSpeak ����������������������������������������������������75
Sending an Alert to Your Twitter Account �������������������������������������������������������75
vi
TTaabbllee ooff CCoonnTTeennTTss
Challenges ����������������������������������������������������������������������������������������������������������78
Conclusion ����������������������������������������������������������������������������������������������������������78
Chapter 4: Weather Station �����������������������������������������������������������������79
Hardware ������������������������������������������������������������������������������������������������������������80
Software ��������������������������������������������������������������������������������������������������������������84
Procedure ������������������������������������������������������������������������������������������������������������89
Installing the DHT11 Sensor Library �������������������������������������������������������������������89
Installing the BMP085 Sensor Library �����������������������������������������������������������������90
Enabling the 12C Interface ����������������������������������������������������������������������������������90
Creating a Project in ThingSpeak ������������������������������������������������������������������������90
Using Your Twitter Account in ThingSpeak ����������������������������������������������������������94
Sending an Alert to Your Twitter Account ������������������������������������������������������������94
Challenges ����������������������������������������������������������������������������������������������������������96
Conclusion ����������������������������������������������������������������������������������������������������������96
Chapter 5: Digital Image Processing with Python and OpenCV ���������97
Installing the Software ����������������������������������������������������������������������������������������98
Step 1: Installing Python 2�7 and 3 ���������������������������������������������������������������������99
Step 2: Installing Dependencies ��������������������������������������������������������������������������99
Step 3: Getting the Latest OpenCV Source Code �����������������������������������������������102
Step 4: Installing pip and virtualenv ������������������������������������������������������������������103
Step 5: Creating a Virtual Environment �������������������������������������������������������������105
Step 6: Installing Numpy and Scipy ������������������������������������������������������������������106
Step 7: Installing OpenCV ����������������������������������������������������������������������������������106
Step 8: Testing the OpenCV Installation ������������������������������������������������������������111
Classifiers ���������������������������������������������������������������������������������������������������������113
vii
TTaabbllee ooff CCoonnTTeennTTss
Step 1: Collecting Images for a Database ���������������������������������������������������������113
Step 2: Arranging the Negative Images �������������������������������������������������������������114
Step 3: Cropping and Marking Positive Images ������������������������������������������������116
Step 4: Creating a Vector of Positive Images ����������������������������������������������������121
Step 5: Haar Training �����������������������������������������������������������������������������������������123
Step 6: Creating the XML File ����������������������������������������������������������������������������125
Testing with Images ������������������������������������������������������������������������������������������127
Testing with Videos �������������������������������������������������������������������������������������������128
Moving a Robot Arm ������������������������������������������������������������������������������������������132
Software ������������������������������������������������������������������������������������������������������������139
Challenges ��������������������������������������������������������������������������������������������������������139
Conclusion ��������������������������������������������������������������������������������������������������������139
Appendix: Hardware Specifications ��������������������������������������������������141
Raspberry Pi 3B ������������������������������������������������������������������������������������������������142
Technical Specifications �����������������������������������������������������������������������������143
Raspberry Pi Zero W ������������������������������������������������������������������������������������������145
PIR Motion Sensor ���������������������������������������������������������������������������������������������146
Advantages ��������������������������������������������������������������������������������������������������146
Specifications ����������������������������������������������������������������������������������������������146
MQ2 Gas Sensor ������������������������������������������������������������������������������������������������147
Characteristics ��������������������������������������������������������������������������������������������148
Applications �������������������������������������������������������������������������������������������������148
ADS1115 ����������������������������������������������������������������������������������������������������������� 149
Features������������������������������������������������������������������������������������������������������� 149
Applications ������������������������������������������������������������������������������������������������� 150
viii
TTaabbllee ooff CCoonnTTeennTTss
BMP085 Pressure Sensor ���������������������������������������������������������������������������������150
Description ��������������������������������������������������������������������������������������������������151
Technical Details �����������������������������������������������������������������������������������������151
DHT11 Humidity Sensor ������������������������������������������������������������������������������������152
Features and Applications ���������������������������������������������������������������������������152
Description ��������������������������������������������������������������������������������������������������153
Technical Specifications ������������������������������������������������������������������������������153
Index �������������������������������������������������������������������������������������������������155
ix
About the Author
Guillermo Guillen is an Electronics and
Communications Engineer, and a Military
Engineer. He has worked in companies as
a citizen and on government projects. He is
the winner of “Make with Ada 2018-19” and
several prizes in three other contests. He has
written this book and over 40 articles about the
research, design, development, and testing of
electronic equipment used in various systems
with the support of AWS or other. He currently
works as a micro- entrepreneur, and you can
find more information at www.guillengap.com.
xi