38,39 €
The age of connected devices is here, be it fitness bands or smart homes. It's now more important than ever to understand how hardware components interact with the internet to collect and analyze user data. The Internet of Things (IoT), combined with the popular open source language Python, can be used to build powerful and intelligent IoT systems with intuitive interfaces.
This book consists of three parts, with the first focusing on the "Internet" component of IoT. You'll get to grips with end-to-end IoT app development to control an LED over the internet, before learning how to build RESTful APIs, WebSocket APIs, and MQTT services in Python. The second part delves into the fundamentals behind electronics and GPIO interfacing. As you progress to the last part, you'll focus on the "Things" aspect of IoT, where you will learn how to connect and control a range of electronic sensors and actuators using Python. You'll also explore a variety of topics, such as motor control, ultrasonic sensors, and temperature measurement. Finally, you'll get up to speed with advanced IoT programming techniques in Python, integrate with IoT visualization and automation platforms, and build a comprehensive IoT project.
By the end of this book, you'll be well-versed with IoT development and have the knowledge you need to build sophisticated IoT systems using Python.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 656
Veröffentlichungsjahr: 2020
Copyright © 2020 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Karan SadawanaAcquisition Editor: Shrilekha InaniContent Development Editor: Romy DiasSenior Editor: Rahul DsouzaTechnical Editor:Aurobindo KarCopy Editor: Safis EditingProject Coordinator: Neil DmelloProofreader: Safis EditingIndexer: Manju ArasanProduction Designer: Joshua Misquitta
First published: October 2020
Production reference: 1151020
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-83898-246-1
www.packt.com
Packt.com
Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Fully searchable for easy access to vital information
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors
Gary Smart is a senior software engineer and an IoT and integration expert. The commencement of Gary's IT career coincided with the birth of the World Wide Web and has grown in line with the internet and emerging technologies ever since, including the rise of mobile phones and tablets, embedded technologies, SaaS and business migration to the cloud, and in recent years, the IoT revolution. Gary's practical experience includes both technical and management positions and experience in both small and large organizations, including Hewlett-Packard, Deakin University, and Pacific Hydro-Tango, boutique consulting firms, and innovative internet and IoT start-ups.
Federico Gonzalez is an Argentinian-based cooperative developer and teacher. He studies information systems engineering at UTN with a focus on development. He is part of Devecoop, a cooperative where he works on projects using a broad range of technologies, currently focusing on developing software and teaching React.js. He contributes to open source projects such as Lelylan (an IoT cloud platform with microservices architecture), EventoL (conference and installfest management software), and some minor contributions to projects with a Docker environment, Python, or JavaScript code. He also gives various workshops at universities, conferences, and companies in Argentina featuring React.js, Python, Docker, open source free software, and cooperatives.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Practical Python Programming for IoT
About Packt
Why subscribe?
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Code in Action
Download the color images
Conventions used
Get in touch
Reviews
Section 1: Programming with Python and the Raspberry Pi
Setting Up your Development Environment
Technical requirements
Understanding your Python installation
Setting up a Python virtual environment
Installing Python GPIO packages with pip
Anatomy of a virtual environment
Alternative methods of executing a Python script
Using sudo within virtual environments
Executing Python scripts outside of their virtual environments
Running a Python script at boot
Configuring the GPIO interface on our Raspberry Pi
Configuring the PiGPIO daemon
Summary
Questions
Further reading
Getting Started with Python and IoT
Technical requirements
Creating a breadboard prototype circuit
Understanding the breadboard
Positioning and connecting the push button
Positioning and connecting the LED
Positioning and connecting the resistor
Reading an electronic schematic diagram
Reading the push button schematic connection
Reading the LED and resistor schematic connection
Introducing ground connections and symbols
Exploring two ways to flash an LED in Python
Blinking with GPIOZero
Imports
Pin Factory configuration
Blinking the LED
Blinking with PiGPIO
Imports
PiGPIO and pin configuration
Blinking the LED
Comparing the GPIOZero and PiGPIO examples
Exploring two ways to integrate a push button in Python
Responding to a button press with GPIOZero
Imports
Button pressed handler
Button configuration
Preventing the main thread from terminating
Responding to a button press with PiGPIO
Button pin configuration
Button pressed handler
Creating your first IoT program
Running and testing the Python server
Understanding the server code
Imports
Variable definitions
The resolve_thing_name() method
The get_lastest_dweet() method
The poll_dweets_forever() method
The process_dweet() method
The main program entry point
Extending your IoT program
Implementing a dweeting button
PiGPIO LED as a class
Summary
Questions
Further reading
Networking with RESTful APIs and Web Sockets Using Flask
Technical requirements
Introducing the Flask microservices framework
Creating a RESTful API service with Flask-RESTful
Running and testing the Python server
Understanding the server code
Imports
Flask and Flask-RESTful API instance variables
Global variables
The init_led() method
Serving a web page
The LEDControl class
The get() class method
The post() class method
LEDController registration and starting the server
Introduction to PWM
Adding a RESTful API client web page
Understanding the client-side code
JavaScript imports
The getState() function
The postUpdate() function
The updateControls() function
Registering event handlers with jQuery
The web page HTML
Creating a Web Socket service with Flask-SocketIO
Running and testing the Python server
Server code walkthrough
Imports
Flask and Flask-RESTful API instance variables
Serving a web page
Connecting and disconnecting handlers
LED handler
Starting the server
Adding a Web Socket client web page
Understanding the client-side code
Imports
Socket.IO connect and disconnect handlers
The on LED handler
The document ready function
The web page HTML
Comparing the RESTful API and Web Socket servers
Summary
Questions
Further reading
Networking with MQTT, Python, and the Mosquitto MQTT Broker
Technical requirements
Installing the Mosquitto MQTT broker
Learning MQTT by example
Publishing and subscribing MQTT messages
Exploring MQTT topics and wildcards
Applying Quality of Service to messages
Retaining messages for later delivery
Publishing a retained message
Creating durable connections
Saying goodbye with a Will
Using MQTT broker services
Introducing the Python Paho-MQTT client library
Controlling an LED with Python and MQTT
Running the LED MQTT example
Understanding the code
Imports
Global variables
The set_led_level(data) method
The on_connect() and on_disconnect() MQTT callback methods
The on_message() MQTT callback method
The init_mqtt() method
Main entry point
Building a web-based MQTT client
Understanding the code
Imports
Global variables
The Paho JavaScript MQTT client
Connecting to the broker
The onConnectionLost and onMessageArrived handler methods
JQuery document ready function
Summary
Questions
Further reading
Section 2: Practical Electronics for Interacting with the Physical World
Connecting Your Raspberry Pi to the Physical World
Technical requirements
Understanding Raspberry Pi pin numbering
Exploring popular Python GPIO libraries
Reviewing GPIOZero – simple interfacing for beginners
Reviewing RPi.GPIO – a low-level GPIO for beginners
Reviewing Circuit Python and Blinka – interfacing for complex devices
Reviewing PiGPIO – a low-level GPIO library
Exploring remote GPIO with PiGPIO (and GPIOZero)
Reviewing SPIDev and SMBus – dedicated SPI and I2C libraries
Why PiGPIO?
Exploring Raspberry Pi electronic interfacing options
Understanding digital IO
Understanding analog IO
Understanding Pulse-Width Modulation
Creating PWM signals
Understanding SPI, I2C, and 1-wire interfaces
Understanding the serial / UART protocol
Interfacing with an analog-to-digital converter
Building the ADS1115 ADC circuit
Making sure the ADS1115 is connected to your Raspberry Pi
Reading analog input with the ADS1115
Understanding the code
Imports
ADS1115 setup and configuration
Global variables
Program entry point
Using PWM to control an LED
Understanding the code
Global variables
Range mapping function
Generating the PWM signal
Visually exploring PWM with PiScope
Visualizing software and hardware-timed PWM
Summary
Questions
Further reading
Electronics 101 for the Software Engineer
Technical requirements
Fitting out your workshop
Buying electronic modules and components
Purchasing lose components
Purchasing open source hardware modules
Keeping your Raspberry Pi safe
Three ways electronic components fail
Electronics interfacing principles for GPIO control
Ohm's Law and power
Kirchhoff's circuit laws
Why are we using a 200 Ohm resistor for the LED circuit?
Calculating the resistor value
Factoring in the Raspberry Pi's current limits
Calculating the resistor's power dissipation
Exploring digital electronics
Digital output
Digital input
Using pull-up and pull-down resistors
The resistor solution
The code solution
Exploring analog electronics
Analog output
Analog input
Voltage dividers
Understanding logic-level conversion
Voltage dividers as logic-level converters
Logic-level converter ICs and modules
Comparing voltage dividers and logic-level converters
Summary
Questions
Further reading
Section 3: IoT Playground - Practical Examples to Interact with the Physical World
Turning Things On and Off
Technical requirements
Exploring a relay driver circuit
Determining a load's voltage and current
Measuring the current requirement of a DC motor
Measuring the current requirement of a relay and LED
Using an optocoupler as a switch
Building the optocoupler circuit
Controlling the optocoupler with Python
Using a transistor as a switch
Building the MOSFET circuit
Controlling the MOSFET with Python
Using a relay as a switch
Building the relay driver circuit
Controlling the Relay Driver Circuit with Python
Summary
Questions
Further reading
Lights, Indicators, and Displaying Information
Technical requirements
Making color with an RGB LED and PWM
Creating the RGB LED circuit
Running and exploring the RGB LED code
Controlling a multi-color APA102 LED strip with SPI
Creating the APA102 circuit
Powering the APA102 circuit
Configuring and running the APA102 LED strip code
APA102 LED strip code walkthrough
Discussion of APA102 and the SPI interface
APA102 LED strip troubleshooting tips
Using an OLED display
Connecting the OLED display
Verifying whether the OLED display is connected
Configuring and running the OLED example
OLED code walkthrough
Making sound with buzzers and PWM
Building the RTTTL circuit
Running the RTTTL music example
Summary
Questions
Further reading
Measuring Temperature, Humidity, and Light Levels
Technical requirements
Measuring temperature and humidity
Creating the DHT11/DHT22 circuit
Running and exploring the DHT11/DHT22 code
Detecting light
Creating an LDR light-detecting circuit
Running the LDR example code
LDR code walkthrough
LDR configuration summary
Detecting moisture
Comparing detection options
Summary
Questions
Movement with Servos, Motors, and Steppers
Technical requirements
Using PWM to rotate a servo
Connecting a servo to your Raspberry Pi
How a servo is controlled using PWM
Running and exploring the servo code
Different types of servos
Using an H-Bridge IC to control a motor
Building the motor driver circuit
Running the example H-Bridge code to control a motor
motor.py
motor_class.py
Introduction to stepper motor control
Connecting the stepper motor to the L293D circuit
Running and exploring the stepper motor code
Summary
Questions
Measuring Distance and Detecting Movement
Technical requirements
Detecting movement with a PIR sensor
Creating the PIR sensor circuit
Running and exploring the PIR sensor code
Measuring distance with an ultrasonic sensor
How an ultrasonic distance sensor works
HC-SR04 distance measurement process
Building the HC-SR04 circuit
Running and exploring the HC-SR04 example code
Detecting movement and distance with Hall-effect sensors
Creating a Hall-effect sensor circuit
Running and exploring the Hall-effect sensor code
Summary
Questions
Advanced IoT Programming Concepts - Threads, AsyncIO, and Event Loops
Technical requirements
Building and testing our circuit
Building the reference circuit
Running the examples
Exploring the event-loop approach
Exploring a threaded approach
Exploring the publisher-subscriber alternative
Exploring an AsyncIO approach
An asynchronous experiment
Summary
Questions
Further reading
IoT Visualization and Automation Platforms
Technical requirements
Triggering an IFTTT Applet from your Raspberry Pi
Creating the temperature monitoring circuit
Creating and configuring an IFTTT Applet
Triggering an IFTTT Webhook
Triggering an IFTTT Applet in Python
Actioning your Raspberry Pi from an IFTTT Applet
Method 1 – using the dweet.io service as an intermediary
Method 2 – creating a Flask-RESTful service
Creating the LED circuit
Running the IFTTT and LED Python program
Creating the IFTTT Applet
Controlling the LED from an email
IFTTT troubleshooting
Visualizing data with the ThingSpeak platform
Configuring the ThinkSpeak platform
Configuring and running the ThinkSpeak Python program
Other IoT and automation platforms for further exploration
Zapier
IFTTT platform
ThingsBoard IoT platform
Home Assistant
Amazon Web Services (AWS)
Microsoft Azure, IBM Watson, and Google Cloud
Summary
Questions
Tying It All Together - An IoT Christmas Tree
Technical requirements
Overview of the IoT Christmas tree
Building the IoTree circuit
Three IoTree service programs
Configuring, running, and using the Tree API service
Configuring the Tree API service
Running the Tree API service
Configuring, running, and using the Tree MQTT service
Configuring the Tree MQTT service
Running the Tree MQTT service program
Integrating the IoTree with dweet.io
Configuring the Tree MQTT service
Running the dweet integration service program
Integrating with email and Google Assistant via IFTTT
Integration with email
Integration with Google Assistant
Ideas and suggestions to extend your IoTree
Summary
Questions
Assessments
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Other Books You May Enjoy
Leave a review - let other readers know what you think
Section 1: Programming with Python and the Raspberry Pi
In this first section of our journey, our primary focus will be on the Internet part of IoT.
We'll start by learning how to properly set up your Python development environment, before exploring and playing with a variety of networking techniques using Python to build network- and internet-connected services and applications. We will also create simple web user interfaces that work with the techniques and examples we will learn about.
However, I am sure if you are reading this book you are eager to jump right in, learn about and play with electronics, and start building and tinkering. I know I would be! So, Chapter 2, Getting Started with Python and IoT is dedicated to building a simple internet-connected IoT project from the ground up – electronics and all – so that we have a reference example for later chapters (and something to tinker with!).
Let's get started!
This section comprises the following chapters:
Chapter 1
,
Setting Up Your Development Environment
Chapter
2
,
Getting Started with Python and IoT
Chapter
3
,
Networking with RESTful APIs and Web Sockets using Flask
Chapter
4
,
Networking with MQTT, Python, and the Mosquitto MQTT Broker
Setting Up your Development Environment
An important yet often overlooked aspect of Python programming is how to correctly set up and maintain a Python project and its runtime environment. It is often overlooked because it presents as an optional step for the Python ecosystem. And while this might be fine for learning Python language fundamentals, it can quickly become a problem for more complex projects where we need to maintain separate code bases and dependencies to ensure our projects do not interfere with one another, or worse as we will discuss, break operating system tools and utilities.
So, before we jump into IoT code and examples in later chapters, it is so very important for us to cover the steps required to set up a Python project and its run time environment.
In this chapter, we will cover the following topics:
Understanding your Python installation
Setting up a Python virtual environment
Installing Python GPIO packages with
pip
Alternative methods of executing a Python script
Raspberry Pi GPIO interface configuration
To perform the hands-0n exercises in this chapter, you will need the following:
Raspberry Pi 4 Model B
Raspbian OS Buster (with desktop and recommended software)
Minimum Python version 3.5
These requirements are what the code examples in this book are based on. It's reasonable to expect that the code examples should work without modification on a Raspberry Pi 3 Model B or a different version of Raspbian OS as long as your Python version is 3.5 or higher.
The full source code for this book can be found on GitHub at the following URL: https://github.com/PacktPublishing/Practical-Python-Programming-for-IoT. We will clone this repository shortly when we come to the Setting up a Python virtual environment section.
In this section, we will find out which versions of Python you have installed on your Raspberry Pi. As we will discover, there are two versions of Python that come pre-installed on Raspbian OS. Unix-based operating systems (such as Raspbian OS) typically have Python version 2 and 3 pre-installed because there are operating-system-level utilities built with Python.
To find out which versions of Python you have on your Raspberry Pi, follow these steps:
Open a new Terminal and execute the python --version command:
$ python --version
Python 2.7.16
In my example, we see that Python version 2.7.16 has been installed.
Next, run the python3 --version command:
$ python3 --version
Python 3.7.3
In my example, we see that the second version of Python (that is, python3, with the 3) that is installed is version 3.7.3.
Don't worry if the minor versions (the numbers .7.16 after the 2 and .7.3 after 3) are not the same; it is the major versions 2 and 3 that are of interest. Python 2 is a legacy version of Python, while Python 3 is the current and supported version of Python at the time of writing. When we are starting a new Python development, we will practically always use Python 3 unless there are legacy issues we need to contend with.
If you are an experienced Python programmer, you may be able to discern whether a script is written for Python 2 or 3, but it's not always obvious by simply looking at a piece of code. Many new-to-Python developers experience frustrations by mixing up Python programs and code fragments that are meant for different Python versions. Always remember that code written for Python 2 is not guaranteed to be upward-comparable with Python 3 without modification.
A quick tip I can share to visually help to determine which Python version a code fragment is written for (if the programmer has not made it clear in the code comments) is to look for a print statement.
If you look at the following example, you will see that there are two print statements. The first print statement without the parentheses is a give-away that it will only work with Python 2:
print "Hello" # No parentheses - This only works in Python 2, a dead give-away that this script is for Python 2.
print("Hello") # With parentheses - this will work in Python 2 and Python 3
Of course, you can always run the code against both Python 2 and 3 and see what happens.
We have now seen that there are two Python versions available by default on Raspbian OS, and made mention that there are system-level utilities that are written in Python that reply on these versions. As Python developers, we must take care not to disrupt the global Python installations as this can potentially break system-level utilities.
We will now turn our attention to a very important Python concept, the Python virtual environment, which is the way we isolate or sandbox our own Python projects from the global installation.
In this section, we will discuss how Python interacts with your operating system installation and cover the steps necessary to set up and configure a Python development environment. In addition, as part of our setup process, we will clone the GitHub repository that contains all of the code (organized by chapter) for this book.
By default, Python and its package management tool,pip, operate globally at the system level and can create some confusion for Python beginners because this global default is in contrast to many other language ecosystems that operate locally on a project folder level by default. Unwearyingly working and making changes to the global Python environment can break Python-based system-level tools, and remedying the situation can become a major headache.
As a Python developer, we use Python virtual environments to sandbox our Python projects so they will not adversely interfere with system-level Python utilities or other Python projects.
In this book, we will be using a virtual environment tool known as venv, which comes bundled as a built-in module with Python 3.3 and above. There are other virtual environment tools around, all with their relative strengths and weaknesses, but they all share the common goal of keeping Python dependencies isolated to a project.
Let's begin and clone the GitHub repository and create a new Python virtual environment for this chapter's source code. Open a new Terminal window and work through the following steps:
Change into or create a folder where you want to store this book's source code and execute the following commands. With the last command, we rename the cloned folder to be
pyiot
. This has been done to help shorten Terminal command examples throughout the book:
$ cd ~
$ git clone https://github.com/PacktPublishing/Practical-Python-Programming-for-IoT$ mv Practical-Python-Programming-for-IoT pyiot
$ cd ~/pyiot/chapter01
Execute the following command, which creates a new Python virtual environment using the
venv
tool.
It's important that you type
python3
(with the 3) and remember that
venv
is only available with Python 3.3 and above:
$ python3 -m venv venv
The options that we are passing to python3 include -m venv, which tells the Python interpreter that we want to run the module namedvenv. The venv parameter is the name of the folder where your virtual environment will be created.
To use a Python virtual environment, we must
activate
it, which is accomplished with the
activate
command:
# From with in the folder ~/pyiot/chapter01
$ source venv/bin/activate
(venv) $
When your Terminal has a Python virtual environment activated, all Python-related activity is sandboxed to your virtual environment.
Next, execute
which python
(
without
the
3
) in your Terminal, and notice that the location of the Python executable is beneath your
venv
folder and if you check the version of Python, it's Python version 3:
(venv) $ which python
/home/pi/pyiot/chapter01/venv/bin/python
(venv) $ python --version
Python 3.7.3
To leave an activated virtual environment, use the
deactivate
command as illustrated here:
(venv) $ deactivate
$
Notice also that (venv) $ is no longer part of the Terminal prompt text once the virtual environment has been deactivated.
Finally, now that you are outside of our Python virtual environment if you execute
which python
(without
the
3
) and
python --version
again, notice we're back to the default system-level Python interpreter, which is version 2:
$ which python
/usr/bin/python
$ python --version
Python 2.7.13
As we just illustrated in the preceding examples, when we ran python --version in an activated virtual environment, we see that it's Python version 3 whereas in the last example, at the start of this chapter, the system level, python --version, was version 2, and we needed to type python3 --version for version 3. In practice, python (with no number) relates to the default version of Python. Globally, this is version 2. In your virtual environment, we only have one version of Python, which is version 3, so it becomes the default.
We have now seen how to create, activate, and deactivate a Python virtual environment and why it is important to use a virtual environment to sandbox Python projects. This sandboxing means we can isolate our own Python projects and their library dependencies from one another, and it prevents us from potentially disrupting the system-level installation of Python and breaking any system-level tools and utilities that rely on them.
Next, we will see how to install and manage Python packages in a virtual environment using pip.
In this section, we learn how to install and manage Python packages in a Python virtual environment you created and explored in the previous section. A Python package (or library if you prefer that term) allows us to extend the core Python language with new features and functionality.
We will need to install many different packages throughout this book, however, for starters and to explore and learn the basic concepts related to package installation and management, we will be installing two common GPIO-related packages in this section that we will use throughout this book. These two packages are the following:
The
GPIOZero
library, an entry-level and easy to use GPIO library for controlling simple electronics
The
PiGPIO
library, an advanced GPIO library with many features for more complex electronic interfacing
In the Python ecosystem, package management is done with the pip command (pip stands for Python installs packages). The official public package repository that pip queries is known as the Python Package Index, or simply PyPi, and it is available for browsing on the web at https://pypi.org.
There will be code examples in this book where we will be interacting with your Raspberry Pi's GPIO pins, so we need to install a Python package (or two) so that your Python code can work with your Raspberry Pi's GPIO pins. For now, we are just going to check for and install two GPIO-related packages. In Chapter 2, Getting Started with Python and IoT, and Chapter 5, Connecting Your Raspberry Pi to the Physical World, we will cover these GPIO packages and other alternatives in greater detail.
In your chapter01 source code folder, you will find a file named gpio_pkg_check.py, which is replicated in the following. We will use this file as the basis to learn about pip and package management in the context of a Python virtual environment. This script simply reports the availability of a Python package depending on whether using import succeeds or raises an exception:
"""
Source File: chapter01/gpio_pkg_check.py
"""
try:
import gpiozero
print('GPIOZero Available')
except:
print('GPIOZero Unavailable. Install with "pip install gpiozero"')
try:
import pigpio
print('pigpio Available')
except:
print('pigpio Unavailable. Install with "pip install pigpio"')
Let's check for the availability of GPIO packages using gpio_pkg_check.py and with pip. I'll kill the suspense by telling you that they're not going to be available in your freshly-created virtual environment (yet), however, we are going to install them!
The following steps will walk us through the process of upgrading pip, exploring the tool's options, and installing packages:
As the first step, we will upgrade the
pip
tool.
In a Terminal window, run the following command, remembering that all commands that follow
must be performed in an
activated
v
irtual environment—meaning you should see the text
(venv)
i
n the Terminal prompt:
(venv) $ pip install --upgrade pip
...output truncated...
The preceding upgrade command may take a minute or two complete and will potentially output a lot of text to the Terminal.
With
pip
now upgraded, we can see what Python packages are already installed in our virtual environment using the
pip list
command:
(venv) $ pip list
pip (9.0.1)
pkg-resources (0.0.0)
setuptools (33.1.1)
What we see in the preceding are the default Python packages in our fresh virtual environment. Do not worry if the exact package list or version numbers do not match exactly with the example.
Run our Python script with the
python
gpio_pkg_check.py
command and observe that our GPIO packages are
not
installed:
(venv) $ python gpio_pkg_check.py
GPIOZero Unavailable. Install with "pip install gpiozero"
pigpio Unavailable. Install with "pip install pigpio"
To install our two required GPIO packages, we use the
pip install
command as shown in the following example:
(venv) $ pip install gpiozero pigpio
Collecting gpiozero...
... output truncated ...
Now, run the
pip list
command again; we will see these new packages are now installed in our virtual environment:
(venv) $ pip list
colorzero (1.1)
gpiozero (1.5.0) # GPIOZero
pigpio (1.42) # PiGPIO
pip (9.0.1)
pkg-resources (0.0.0)
setuptools (33.1.1)
You may have noticed that there is a package called colorzero (this is a color manipulation library) that we did not install. gpiozero (version 1.5.0) has a dependency on colorzero, so pip has installed it for us automatically.
Re-run
python gpio_pkg_check.py
and we now see that our Python modules are available for import:
(venv) $ python gpio_pkg_check.py
GPIOZero Available
pigpio Available
Great! We now have a virtual environment with two GPIO packages installed. As you work on Python projects, you will inevitably install more and more packages and want to keep track of them.
Take a snapshot of the packages you have previously installed with the
pip freeze
command:
(venv) $ pip freeze > requirements.txt
The preceding example freezes all installed packages into a file named requirements.txt, which is a common filename to use for this purpose.
Look inside the
requirements.txt
file and you will see all of the Python packages listed together with their version numbers:
(venv) $ cat requirements.txt
colorzero==1.1
gpiozero==1.5.0
pigpio==1.42
pkg-resources==0.0.0
In the future, if you move your Python project to another machine or a new virtual environment, you can use your requirement.txtfile to install all of your captured packages in one go using the pip install -r requirements.txt command.
We've now completed the basic installation life cycle of Python packages using pip. Note that whenever you install new packages with pip install, you also need to re-run pip freeze > requirements.txt to capture the new packages and their dependencies.
To finish our exploration of pip and package management, here are a few other common pip commands:
# Remove a package
(venv) $ pip uninstall <package name>
# Search PyPi for a package (or point your web browser at https://pypi.org)
(venv) $ pip search <query text>
# See all pip commands and options (also see Further Reading at the end of the chapter).
(venv) $ pip --help
Congratulations! We've reached a milestone and covered the essential virtual environment principles that you can use for any Python project, even ones that are not Raspberry Pi related!
Now that we have seen how to create a virtual environment and install packages, let's take a look at a typical Python project folder structure such as ~/pyiot/chapter01 and discover what lies beneath the venv folder.
This section relates to venv, which we have been using in this chapter, and will apply to virtualenv but not pipenv, which we listed as alternative virtual environment tools. The example is also specific to a Raspbian OS and is typical of a standard Unix-based OS. It's important to, at a minimum, understand the basic structure of a virtual environment deployment since we will be mixing our own Python programming code in with the files and folders that make up the virtual environment.
Here is the folder structure of our virtual environment. Yep, its a screenshot from a Mac. That's so I could get everything on screen at once:
The following points explain the core subfolders that are found within our ~/pyiot/chapter01 folder after we ran python3 -m venv venv and installed packages using pip:
The
venv
folder contains all of the Python virtual environment files. There is no real practical need to be touching anything under this folder manually—let the tools do that for you. Remember that the folder is named
venv
only because that's what we called it when it was created.
The
venv/bin
folder contains the Python interpreter (in the
venv
case, there are symbolic links to the system interpreter) and other core Python tools, including