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

To get the most out of this book

The following headings provide an overview of the hardware, software, electronics, and peripherals you will require to successfully work through and complete the exercises found in this book.

  • Hardware and software: All of the exercises and code in this book were built and tested on the following hardware and software versions:
    • Raspberry Pi 4 Model B
    • Raspberry Pi OS Buster (with desktop and recommended software)
    • Python version 3.5

It will be my assumption that you will be using an equivalent setup; however, it is 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 or Raspberry Pi OS as long as your Python version is 3.5 or higher.

If you are not too sure about your Python version, don't worry. One of our first tasks in Chapter 1, Setting Up Your Development Environment, will be understanding Python on your Raspberry Pi and working out which versions are available.

  • Electronic Parts and Equipment: We will be using many electronic parts throughout this book. At the start of each chapter, I list the specific parts and quantities you will require for the chapter's examples. In addition to the parts listed, an electronic breadboard and a mixture of jumper/dupont cables will also be required.

For your convenience, a table cataloging all the electronic parts used throughout the book, the chapters where they are used, plus the minimum quantities you will require follows. If you are new to buying electronic parts, you'll also find tips to help get you started after the table:

Part Name Minimum Quantity Description / Notes Used in Chapter(s)
Red LED 2 * 5mm red LED. Different-colored LEDs can have different electrical characters. Most of our examples in the book will assume a red LED. 2, 3, 4, 5, 6, 7, 9, 12, 13
15Ω Resistor * Color bands (4-band resistor) will be brown, green, black, silver/gold 8
200Ω Resistor * Color bands (4-band resistor) will be red, black brown, silver/gold 2, 3, 4, 5, 6, 8, 9, 12, 13
1kΩ Resistor 2 * Color bands (4-band resistor) will be brown, brown, red, silver/gold 6, 7, 9, 8, 11
2kΩ Resistor 2 * Color bands (4-band resistor) will be red, black, red, silver/gold 6, 11
10kΩ Resistor 1 * Color bands (4-band resistor) will be brown, black, orange, silver/gold 9, 13
51kΩ Resistor 1 *

Color bands (4-band resistor) will be green, brown, orange, silver/gold

6
100kΩ Resistor 1 * Color bands (4-band resistor) will be brown, black, yellow, silver/gold 7, 8, 9
Momentary Push-Button Switch 1 To source a push-button switch that is breadboard friendly, try searching for a large tactile switch. 1, 6, 12
10kΩ Linear Potentiometers 2 Larger potentiometers that you can adjust with your fingers will be easier to work with in the book's examples than small potentiometers that will require a screwdriver to adjust. Make sure you have linear potentiometers (not logarithmic). 5, 6, 12
2N7000 MOSFET 1 * This is a logic-level compatible MOSFET transistor. 7, 8
FQP30N06L Power MOSFET 1 * Optional. When purchasing, make sure the part number ends with L, indicating that it is a logic-level compatible MOSFET (otherwise, it will not reliably work your Raspberry Pi). 7
PC817 Opto-Coupler 1 * Also known as an opto-isolator. 7
SDR-5VDC-SL-C Relay 1 These relays are very popular and easy to come by; however, they are not breadboard friendly. You will need to solder terminals or wires to them so you can plug them into your breadboard. 7
1N4001 Diode 1 * We will be using a diode in the role of a fly-back suppression diode to protect other electrical components from voltage spikes. 7, 8
Size R130  5-volt DC Hobby Motor 2 Size R130 is just a suggestion. What we need are 5-volt compatible DC motors with a stall current (ideally) less than 800 mA. While these motors are easy to come by on auction sites, their current and operating currents can be poorly documented so it can be a gamble as to what you get. Chapter 7Turning Things On and Off, will take you through an exercise to measure the operating currents of your motors. 7, 10
RGBLED, Common Cathode type 1 * This is an LED that is capable of making different colors. 8
Passive Buzzer 1 A passive buzzer that will work with 5 volts. 8
SSD1306 OLED Display 1 This is a small monochrome pixel-based display. 8
APA102 RGBLED Strip 1 This is a strip of addressable APA102 RGBLEDs. You will just need the LED strip, not a power supply or a remote control for our exercises. Be careful to make sure it is the APA102 LEDs that you are purchasing as there are different (and incompatible) types of addressable LEDs available. 8, 14
DHT11 or DHT22 Temperature/Humidity Sensor 1 The DHT11 and DHT22 are interchangeable. The DHT22 is slightly more expensive but offers more accuracy and can measure sub-zero temperatures. 9, 13
LDR 1 * Light-Dependent-Resistor 9
MG90S Hobby Servo 1 This is a suggestion. Any 5-volt hobby servo with 3 wires (+, GND, Signal) should be suitable. 10, 14
L293D H-Bridge IC 1 * Make sure the part number you purchase ends in D, meaning the IC includes embedded fly-back suppression diodes. 10
28BYJ-48 Stepper Motor 1 Make sure you purchase the 5-volt stepper motor variety, with a 1:64 gearing ratio. 10
HC-SR501 PIR Sensor 1 A PIR sensor detects movement. It works on heat, so it can detect the presence of people and animals. 11
HC-SR04 Ultrasonic Distance Sensor 1 An Ultrasonic Distance Sensor estimates distances using sound waves. 11
A3144 Hall-Effect Sensor 1 * This is a non-latching switch-type Hall-effect sensor that turns on in the presence of a magnetic field. 11
AH3503 Hall-Effect Sensor 1 * This is a ratiometric-type Hall-effect sensor that can detect how close (relatively) it is to a magnetic field. 11
Magnet 1 A small magnet is required for use with the Hall-effect sensors. 11
ADS1115 Analog-to-Digital (ADC) Converter Breakout Module 1 This module will allow us to interface analog components with our Raspberry Pi. 5, 9, 12
Logic Level Shifter/Converter Breakout Module 1 This module will allow us to interface 5-volt electrical components with our Raspberry Pi. Search for a Logic Level Shifter/Converter Breakout Module and look for a bi-directional (preferred) module when 4 or 8 channels. 6, 8, 14
Breadboard 1 All our electronic examples will be built on a breadboard. I recommend purchasing two full-size breadboards and joining them together – more breadboard working areas will make building circuits easier. 2 - 14
Dupont / Jumper Cables 3 sets * These cables are used to wire components together on your breadboard. I recommend purchasing sets of male-to-male, male-to-female, and female-to-female types. 2 - 14
Raspberry Pi GPIO Breadboard Breakout 1 This is optional, however, it will make it easier to interface your Raspberry Pi GPIO pins with your breadboard. 2 - 14
Digital Multimeter 1 As a guide, a digital multimeter in the price range of $30-50 USD should be more than suitable. Avoid the very-low and cheapest multi-meters. 6, 7
External Power Supply 2 Some of the circuits in this book will require more power than we can expect our Raspberry Pi to provide. As a minimum source, a 3.3/5-volt breadboard-compatible power supply capable of outputting 1 amp will be suitable. You might also like to research lab power supplies as a more capable and general alternative. 7, 8, 9, 10, 14
Soldering Iron and Solder 1 There will be cases where you need to solder wires and terminals onto components – for example, it is highly likely that you will need to solder terminal legs on to the ADS1115 and logic level converter/shifter modules that you purchase. You will also need to solder terminals or wires onto your SDR-5VDC-SL-C relay so you can plug it into your breadboard.

*  Spares recommended. These are components that can be damaged if incorrectly connected or powered or can physically break with use (for example, legs breaking off).

These parts have been selected due to their low price points, and their general availability on websites such as eBay.com, Bangood.com, AliExpress.com, and electronics retailers.

Before making your purchases, please consider the following:

  • The Minimum Quantity column is what you will need for the exercises in this book, however, it's highly recommended that you purchase spares, especially of LEDs, resistors, and MOSFETs as these components are easily damaged.
  • You will find that many components will need to be purchased in bulk lots.
  • Search around for Electronic Component Starter Kits and compare what they include against the parts listed in the table. You may be able to purchase many of the parts together in a single (and discounted) transaction.
  • The many available plug-and-play Sensor Module Starter Kits that are available will, for the most part, not be compatible with the circuit and code exercises presented throughout this book. The depth of our electronic and code examples means we will need to work with core electrical components. After completing this book, however, you will be in a great position to understand how these plug-and-play sensor modules are built and work!

If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.