Practical Python Programming for IoT
eBook - ePub

Practical Python Programming for IoT

Gary Smart

Share book
  1. 516 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Practical Python Programming for IoT

Gary Smart

Book details
Book preview
Table of contents
Citations

About This Book

Leverage Python and Raspberry Pi to create complex IoT applications capable of creating and detecting movement and measuring distance, light, and a host of other environmental conditionsKey Features• Learn the fundamentals of electronics and how to integrate them with a Raspberry Pi• Understand how to build RESTful APIs, WebSocket APIs, and MQTT-based applications• Explore alternative approaches to structuring IoT applications with PythonBook DescriptionThe 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.What you will learn• Understand electronic interfacing with Raspberry Pi from scratch• Gain knowledge of building sensor and actuator electronic circuits• Structure your code in Python using Async IO, pub/sub models, and more• Automate real-world IoT projects using sensor and actuator integration• Integrate electronics with ThingSpeak and IFTTT to enable automation• Build and use RESTful APIs, WebSockets, and MQTT with sensors and actuators• Set up a Raspberry Pi and Python development environment for IoT projectsWho this book is forThis IoT Python book is for application developers, IoT professionals, or anyone interested in building IoT applications using the Python programming language. It will also be particularly helpful for mid to senior-level software engineers who are experienced in desktop, web, and mobile development, but have little to no experience of electronics, physical computing, and IoT.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Practical Python Programming for IoT an online PDF/ePUB?
Yes, you can access Practical Python Programming for IoT by Gary Smart in PDF and/or ePUB format, as well as other popular books in Informatique & Réseaux informatiques. We have over one million books available in our catalogue for you to explore.

Information

Year
2020
ISBN
9781838982836
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 2Getting 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 4Networking 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

Technical requirements

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.

Understanding your Python installation

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:
  1. 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.
  1. 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.
Python 2 officially became end-of-life in January 2020. It is no longer maintained and will not receive any further enhancements, bug fixes, or security patches.
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.

Setting up a Python virtual environment

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 t...

Table of contents