Book Image

Practical Python Programming for IoT

By : Gary Smart
Book Image

Practical Python Programming for IoT

By: Gary Smart

Overview of this book

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.
Table of Contents (20 chapters)
1
Section 1: Programming with Python and the Raspberry Pi
6
Section 2: Practical Electronics for Interacting with the Physical World
9
Section 3: IoT Playground - Practical Examples to Interact with the Physical World

What this book covers

Chapter 1, Setting Up Your Development Environment, explores the Python ecosystem in the context of the Raspberry Pi OS and teaches you how to correctly set up a Python development project for success. You will also learn alternative ways of starting Python programs and how to configure your Raspberry Pi for GPIO interfacing.

Chapter 2Getting Started with Python and IoT, teaches you the basics of electronics and GPIO interfacing with Python. You will build and experiment with simple electronic circuits that are controlled using Python, and combine this learning to build a simple yet complete internet-controllable IoT application from the ground up using the dweet.io platform.

Chapter 3Networking with RESTful APIs and Web Sockets Using Flask, explores how to build network servers in Python using two approaches – RESTful APIs and Web Sockets. You will learn how to use these servers in conjunction with Python and an HTML/JavaScript user interface to control electronic circuits over a network from a web browser.

Chapter 4Networking with MQTT, Python, and the Mosquitto MQTT Broker, teaches networking approaches using Message Queue Telemetry Transport, a popular choice for distributed IoT applications. You will learn how to use MQTT in conjunction with Python and an HTML/JavaScript user interface to control electronic circuits over a network and from a web browser.

Chapter 5Connecting Your Raspberry Pi to the Physical World, explores different Python-based software options and techniques used to interface and control electronics using a Raspberry Pi's GPIO pins. You will also build and learn to use an ADS1115 analog-to-digital converter module to expand your Raspberry Pi's native interfacing options, and be introduced to Pulse Width Modulation (PWM), an important electronic and interfacing concept that you'll be using in later chapters.

Chapter 6, Electronics 101 for the Software Engineer, teaches you core electronic concepts and fundamentals. You will learn the essential how and why behind common electronic and interfacing circuits and how they are used practically to correctly and safely interface sensors and actuators to your Raspberry Pi. You will also learn the differences between digital and analog electronics and how each applies to and influences interfacing circuit requirements. Many of the fundamentals you learn about in this chapter are seen applied practically in subsequent chapters as we work with different electronic components and modules. 

Chapter 7Turning Things On and Off, teaches you how to use optocouplers, MOSFET transistors, and relays to turn other circuits on and off using your Raspberry Pi and Python. You will also learn about circuit loads, how they are measured, and how this influences the choice and use of optocouplers, MOSFET transistors, and relays in circuits.

Chapter 8Lights, Indicators, and Displaying Information, teaches you how to use an APA102 LED lighting strip, RGB LEDs, OLED displays, and buzzers in conjunction with Python to create visual and auditable orientated circuits and applications.

Chapter 9Measuring Temperature, Humidity, and Light Levels, teaches you how to measure common environmental attributes with your Raspberry Pi and Python. You will build a circuit using a DHT11/22 temperature and humidity sensor and learn about and use Light-Dependent-Resistors (LDRs) to detect the presence or absence of light. In this chapter, you will also deepen your practical understanding and experience of analog electronics, and apply the basic principle to build a moisture detection circuit and application.

Chapter 10Movement with Servos, Motors, and Steppers, teaches you how to create movement using popular mechanical devices together with your Raspberry Pi and Python. You will learn how to control a servo using PWM to create angular movement, use an H-bridge IC circuit together with a motor to control its speed and direction of rotation. Plus, you will learn how to adapt the H-bridge IC circuit for use with a stepper motor for those projects where you need precise control over movement.

Chapter 11Measuring Distance and Detecting Movement, teaches you the principles behind distance measurements using an HC-SR04 ultrasonic distance sensor and how to use an HC-SR501 PIR sensor to detect movement on a macro scale. You will also learn how to use both a ratiometric and switch-type Hall-effect sensor to detect movement and measure relative distance on micro scales.

Chapter 12Advanced IoT Programming Concepts – Threads, AsyncIO, and Event Loops, is an advanced programming chapter that looks at alternative approaches to structuring complex Python programs. You will learn about Python threading, asynchronous I/O, classic event loops, and publisher-subscriber patterns, all within the context of electronic interfacing. By the end of the chapter, you will have experimented with and understood four functionally equivalent applications that are written in four very different ways.

Chapter 13, IoT Visualization and Automation Platforms, is a journey into the world of IoT-related online services and integration. You will be creating two environmental-monitoring applications based on the DHT11/22 temperature and humidity circuit from Chapter 9, Measuring Temperature, Humidity, and Light LevelsFirst, you will leverage your MQTT understanding from Chapter 4Networking with MQTT, Python, and the Mosquitto MQTT Broker, to create an online dashboard at ThingSpeak.com to display and graph both temperature and humidity data. Then, you will also apply RESTful API concepts from Chapter 4Networking with MQTT, Python, and the Mosquitto MQTT Broker, and build an If-This-Then-That (IFTTT.com) workflow Applet that sends you an email whenever the temperature rises above or falls below a certain point.

Chapter 14, Tying It All Together – An IoT Christmas Tree, pulls together many of the themes and concepts you have learned about in earlier chapters with a multifaceted example centered around an internet-connected Christmas tree. From an electronics perspective, you will revisit the APA102 LED strip from Chapter 8Lights, Indicators, and Displaying Information (this will be the Christmas tree lights), and servos from Chapter 10Movement with Servos, Motors, and Steppers (this is used to provide a mechanism to shake or rock the tree). From a networking perspective, you will revisit dweet.io from Chapter 2, Getting Started with Python and IoT; RESTful-APIs from Chapter 3Networking with RESTful APIs and Web Sockets Using Flask; and MQTT from Chapter 4Networking with MQTT, Python, and the Mosquitto MQTT Broker, and learn how to combine techniques to achieve complex integrations that need to bridge different technologies. Finally, you will revisit IFTTT from Chapter 13IoT Visualization and Automation Platforms, and create two Applets that let you control your tree's lights and make the tree shake or rock over the internet. These three Applets include email control, and voice-activated control using Google Assistant.