-->
Navigation
MicroPython for the Internet of Things

MicroPython for the Internet of Things

Now pay Easier and Secure using Paypal
Price:

Read more

 A Beginner’s Guide to Programming with Python on Microcontrollers

Breaking the C-like language barrier to make device programming easy and fast

by Charles Bell


e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 3.00
 Pages
 454 p
 File Size 
 12,245 KB
 File Type
 PDF format
 ISBN-13 (pbk) 
 ISBN-13 (electronic) 
 978-1-4842-3122-7
 978-1-4842-3123-4 
 Copyright©   
 2017 by Charles Bell 

About the Author
Dr. Charles Bell conducts research in emerging
technologies. He is a member of the Oracle MySQL
Development Team as a Senior Developer assisting in
the development of MySQL high availability solutions.
He lives in a small town in rural Virginia with his
loving wife. He received his doctorate of philosophy in
engineering from Virginia Commonwealth University
in 2005. His research interests include database
systems, software engineering, sensor networks, and 3D
printing. He spends his limited free time as a practicing
Maker focusing on microcontroller and 3D printers and printing projects.

About the Technical Reviewer
Nelson Goncalves is a member of the MySQL
Developer Team at Oracle, currently working with
the InnoDB cluster Team. He is based in Portugal
and working for MySQL since 2013. Before joining
MySQL, he was an MSc student at Universidade do
Minho where he specialized into Formal Methods and
Distributed Systems. He loves all things Python related.

Introduction
Internet of Things (IOT) solutions are not nearly as complicated as the name may seem
to indicate. Indeed, the IOT is largely another name for what we have already been doing.
You may have heard of “connected devices” or “Internet-ready” or even “cloud-enabled.”
All of these refer to the same thing — be it a single device such as a toaster or a plant
monitor or a complex, multidevice product like home automation solutions. They all
share one thing in common: they can be accessed via the Internet to either display data
or interact with the devices directly. The trick is applying knowledge of technologies
to leverage them to the best advantages for your IOT solution. In this book, we explore
how to build IOT solutions using an easy-to-understand programming language named
MicroPython running on small, dedicated microcontroller boards.

Intended Audience
I wrote this book to share my passion for Python and IOT solutions. I especially wanted
to show how anyone can program their own IOT solutions in Python using MicroPython
on small microcontroller boards. The intended audience therefore includes anyone
interested in learning how to build IOT solutions, hobbyists, and enthusiasts who don’t
want to spend a lot of time learning a complicated programming language to control
hardware through software in IOT solutions.

Table of Contents
About the Author ............................................................................ xiii
About the Technical Reviewer ......................................................... xv
Acknowledgments ......................................................................... xvii
Introduction .................................................................................... xix
■■Chapter 1: What Is the Internet of Things? ..................................... 1
The Internet of Things and You ................................................................ 2
IOT Is More Than Just Connected to the Internet ..................................................... 3
IOT Services ..............................................................................................................4
A Brief Look at IOT Solutions ....................................................................................5
IOT and Security .....................................................................................................15
Python and the IOT ................................................................................ 19
Origins ....................................................................................................................19
Online Python Simulator .........................................................................................20
Summary ............................................................................................... 24
■■Chapter 2: Introducing MicroPython ............................................ 27
Getting Started ...................................................................................... 27
Origins ....................................................................................................................29
MicroPython Features .............................................................................................30
MicroPython Limitations .........................................................................................31
What Does MicroPython Run On? ...........................................................................31
Experimenting with Python on Your PC .................................................................. 32
How MicroPython Works ....................................................................... 38
The Run, Evaluate, Print Loop (REPL Console) ........................................................39
Off and Running with MicroPython ........................................................ 44
Additional Hardware ...............................................................................................46
Example 1: ..............................................................................................................50
Example 2: ..............................................................................................................52
Example 3: ..............................................................................................................55
Summary ............................................................................................... 57
■■Chapter 3: MicroPython Hardware ............................................... 59
Getting Started with MicroPython Boards ............................................. 59
Firmware Updates ..................................................................................................59
Networking Issues ..................................................................................................60
One Step at a Time! ................................................................................................61
Programming Tools .................................................................................................61
Some Assembly Required .......................................................................................63
GPIO Pins ................................................................................................................64
Other Tips ...............................................................................................................64
MicroPython-Ready Boards ................................................................... 69
Pyboard ..................................................................................................................69
WiPy ........................................................................................................................82
MicroPython-Compatible Boards ........................................................... 96
BBC micro:bit ..........................................................................................................96
Circuit Playground Express (Developer Edition) .....................................................99
Adafruit Feather Huzzah .......................................................................................104
Other Boards ....................................................................................... 108
Breakout Boards and Add-Ons ............................................................ 108
Breakout Boards ...................................................................................................109
Board-Specific Shields/Skins ...............................................................................112
Board-Specific Accessories ..................................................................................116
Which Board Should I Buy? ................................................................. 121
Summary ............................................................................................. 123
■■Chapter 4: How to Program in MicroPython ............................... 125
Basic Concepts .................................................................................... 126
Code Blocks ..........................................................................................................126
Comments ............................................................................................................127
Arithmetic .............................................................................................................128
Output to Screen ...................................................................................................129
Variables ...............................................................................................................130
Types ....................................................................................................................132
Basic Data Structures .......................................................................... 133
Lists ......................................................................................................................133
Tuples ...................................................................................................................134
Dictionaries ...........................................................................................................134
Statements .......................................................................................... 136
Conditional Statements ........................................................................................136
Loops ....................................................................................................................137
Modularization; Modules, Functions, and Classes .............................. 138
Including Modules ................................................................................................138
Functions ..............................................................................................................139
Classes and Objects .............................................................................................140
Learning Python by Example ............................................................... 147
Example 1: Using Loops ........................................................................................147
Example 2: Using Complex Data and Files ............................................................150
Example 3: Using Functions ................................................................................. 156
Example 4: Using Classes .....................................................................................162
For More Information .......................................................................... 171
Summary ............................................................................................. 171
■■Chapter 5: MicroPython Libraries ............................................... 173
Built-In and Standard Libraries ........................................................... 174
Overview ...............................................................................................................174
Common Standard Libraries .................................................................................177
Built-In Functions and Classes .............................................................................187
Exceptions ............................................................................................................192
MicroPython Libraries ......................................................................... 195
Overview ...............................................................................................................196
Common MicroPython Libraries ...........................................................................196
Custom Libraries ................................................................................. 199
Summary ............................................................................................. 204
■■Chapter 6: Low-Level Hardware Support .................................... 205
Board-Specific Libraries ...................................................................... 206
Pyboard ................................................................................................................206
WiPy ......................................................................................................................215
Low-Level Examples ........................................................................... 220
Drivers and Libraries to the Rescue! ....................................................................220
Real-Time Clock (RTC) ..........................................................................................221
Callbacks ..............................................................................................................224
Using Breakout Boards ........................................................................ 226
Inter-Integrated Circuit (I2C) .................................................................................227
Serial Peripheral Interface (SPI) ........................................................................... 233
Summary ............................................................................................. 237
■■Chapter 7: Electronics for Beginners .......................................... 239
The Basics ........................................................................................... 240
Tools .....................................................................................................................240
Multimeter ............................................................................................................241
Soldering Iron .......................................................................................................242
Wire Strippers .......................................................................................................244
Helping Hands ......................................................................................................244
Using a Multimeter ...............................................................................................246
Powering Your Electronics ....................................................................................254
Electronic Components ....................................................................... 255
Button ...................................................................................................................255
Capacitor ..............................................................................................................256
Diode ....................................................................................................................257
Fuse ......................................................................................................................257
Light Emitting Diode (LED) ....................................................................................258
Relay .....................................................................................................................260
Resistor ................................................................................................................260
Switch ...................................................................................................................261
Transistor ..............................................................................................................262
Voltage Regulator .................................................................................................263
Breakout Boards and Circuits ...............................................................................264
Using a Breadboard to Build Circuits .................................................. 265
What Are Sensors? .............................................................................. 268
How Sensors Measure ..........................................................................................269
Examples of Sensors ............................................................................................271
Summary ............................................................................................. 282
■■Chapter 8: Project 1: Hello, World! MicroPython Style ............... 283
Overview ............................................................................................. 284
Required Components ......................................................................... 285
Set Up the Hardware ........................................................................... 288
WiPy ......................................................................................................................290
Pyboard ................................................................................................................291
Write the Code ..................................................................................... 293
Design ...................................................................................................................293
Libraries Needed ..................................................................................................293
Planning the Code .................................................................................................299
Test the Parts of the Code .................................................................................... 304
Completed Code ...................................................................................................306
Execute! .............................................................................................. 309
Taking It Further .................................................................................. 311
Summary ............................................................................................. 313
■■Chapter 9: Project 2: Stoplight Simulator ..................................... 315
Overview ............................................................................................. 316
Required Components ......................................................................... 316
Set Up the Hardware ........................................................................... 319
WiPy ......................................................................................................................320
Pyboard ................................................................................................................321
Write the Code ..................................................................................... 323
Part 1: Stoplight Simulator – Using a Pushbutton ................................................323
Part 2: Stoplight Simulator – Remote Control with HTML .....................................329
Completed Code ...................................................................................................335
Execute! .............................................................................................. 341
Taking it Further .................................................................................. 343
Summary ............................................................................................. 344
■■Chapter 10: Project 3: Plant Monitoring ..................................... 345
Overview ............................................................................................. 345
Required Components ......................................................................... 347
Set Up the Hardware ........................................................................... 349
WiPy ......................................................................................................................350
Pyboard ................................................................................................................351
Write the Code ..................................................................................... 351
Calibrating the Sensor ..........................................................................................352
Part 1: Sensor Code Module .................................................................................354
Part 2: Main Code .................................................................................................364
Execute! .............................................................................................. 375
Taking it Further .................................................................................. 376
Summary ............................................................................................. 377
■■Chapter 11: Project 4: Using Weather Sensors ........................... 379
Overview ............................................................................................. 380
Message Queue Telemetry Transport .................................................. 380
How It Works .........................................................................................................381
Getting Started with Adafruit IO ............................................................................382
Required Components ......................................................................... 384
Set Up the Hardware ........................................................................... 385
Configure Adafruit IO ........................................................................... 387
Set Up Feeds ........................................................................................................387
Set Up a Dashboard ..............................................................................................388
Get Your Credentials .............................................................................................396
Write the Code ..................................................................................... 397
MQTT Driver ..........................................................................................................398
BME280 Library ....................................................................................................398
Weather Class .......................................................................................................399
Main Code .............................................................................................................404
Execute! .............................................................................................. 407
Taking it Further .................................................................................. 412
Summary ............................................................................................. 412
■■Chapter 12: Where to Go from Here ............................................ 415
More Projects to Explore ..................................................................... 415
MicroPython Project Samples ...............................................................................416
Forums .................................................................................................................416
Documentation .....................................................................................................417
Repositories ..........................................................................................................417
Community Project Sites: Hackster.io ...................................................................418
Knowledge Repositories: learn.adafruit.io ............................................................420
Join the Community ............................................................................ 421
Why Contribute? ...................................................................................................422
Which License, Where? .........................................................................................422
How We Share ......................................................................................................423
Keep Your Designs Original ...................................................................................424
Check the License ................................................................................................424
Keep It Appropriate ...............................................................................................425
Annotate Your Work ..............................................................................................426
Be a Good Citizen ..................................................................................................427
Suggested Communities .......................................................................................427
Become a Maker ................................................................................. 429
What’s a Maker? ...................................................................................................429
Share Your Ideas ...................................................................................................430
Attend an Event ....................................................................................................430
Summary ............................................................................................. 431
Appendix ........................................................................................ 433
Required Components ......................................................................... 433
Optional Components .......................................................................... 435
Recommended Tools ........................................................................... 436
Index .............................................................................................. 439


Bookscreen
e-books shop

How to Use This Book
This book is designed to guide you through learning more about what the Internet of
Things is, discovering the power of MicroPython, and seeing how to build your own IOT solutions.
If you already have your own MicroPython board and are familiar with some of
the topics early in the book, I recommend you skim them so that you are familiar with
the context presented so that the later chapters, especially the examples, are easy to
understand and implement on your own. You may also want to read some of the chapters
out of order so that you can get your project moving, but I recommend going back to the
chapters you skip to ensure you get all of the data presented.
If you are just getting started with MicroPython and microcontrollers, I recommend
reading the book in its entirety before developing your own IOT solutions. That said,
many of the examples presented in the early chapters are building blocks for what follows
in the project chapters.

0