Python Programming with Raspberry Pi

Book description

Become a master of Python programming using the small yet powerful Raspberry Pi Zero

About This Book

  • This is the first book on the market that teaches Python programming with Raspberry Pi Zero

  • Develop exciting applications such as a mobile robot and home automation controller using Python

  • This step-by-step guide helps you make the most out of Raspberry Pi Zero using Python programming

  • Who This Book Is For

    This book is aimed at hobbyists and programmers who want to learn Python programming and develop applications using the Pi Zero. They should have basic familiarity with electronics.

    What You Will Learn

  • Configure Raspberry Pi using Python

  • Control loops to blink an LED using simple arithmetic operations

  • Understand how interface sensors, actuators, and LED displays work

  • Get to grips with every aspect of Python programming using practical examples

  • Explore machine vision, data visualization, and scientific computations

  • Build a mobile robot using the Raspberry Pi as the controller

  • Build a voice-activated home automation controller

  • In Detail

    Raspberry Pi Zero is a super-small and super-affordable product from Raspberry Pi that is packed with a plethora of features and has grabbed the notice of programmers, especially those who use Python.

    This step-by-step guide will get you developing practical applications in Python using a Raspberry Pi Zero. It will become a valuable resource as you learn the essential details of interfacing sensors and actuators to a Raspberry Pi, as well as acquiring and displaying data.

    You will get started by writing a Python program that blinks an LED at 1-second intervals. Then you will learn to write simple logic to execute tasks based upon sensor data (for example, to control a motor) and retrieve data from the web (such as to check e-mails to provide a visual alert). Finally, you will learn to build a home automation system with Python where different appliances are controlled using the Raspberry Pi.

    The examples discussed in each chapter of this book culminate in a project that help improve the quality of people’s lives.

    Style and approach

    This will be a learning, step-by-step guide to teach Python programming using the famous Raspberry Pi Zero. The book is packed with practical examples at every step along with tips and tricks for the Raspberry Pi fans

    Table of contents

    1. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    2. Getting Started with Python and the Raspberry Pi Zero
      1. Let's get started!
        1. Things needed for this book
          1. Buying the Raspberry Pi Zero
      2. Introduction to the Raspberry Pi Zero
        1. The features of the Raspberry Pi Zero
      3. The setup of the Raspberry Pi Zero
        1. Soldering the GPIO headers
          1. Enclosure for the Raspberry Pi Zero
        2. OS setup for the Raspberry Pi
          1. micro SD card preparation
      4. Let's learn Python!
      5. The Hello World example
        1. Setting up your Raspberry Pi Zero for Python programming
          1. IDLE's interactive tool
          2. The text editor approach
          3. Launching the Python interpreter via the Linux Terminal
          4. Executing Python scripts using the Linux Terminal
          5. The print() function
          6. The help() function
      6. Summary
    3. Arithmetic Operations, Loops, and Blinky Lights
      1. Hardware required for this chapter
      2. Arithmetic operations
      3. Bitwise operators in Python
      4. Logical operators
      5. Data types and variables in Python
      6. Reading inputs from the user
        1. The formatted string output
          1. The str.format() method
          2. An exercise for the reader
          3. Another exercise for the reader
        2. Concatenating strings
      7. Loops in Python
        1. A for loop
          1. Indentation
            1. Nested loops
        2. A while loop
      8. Raspberry Pi's GPIO
        1. Blinky lights
          1. Code
        2. The applications of GPIO control
      9. Summary
    4. Conditional Statements, Functions, and Lists
      1. Conditional statements
        1. An if-else statement
        2. if-elif-else statement
          1. Breaking out of loops
        3. The applications of conditional statements: executing tasks using GPIO
          1. Breaking out a loop by counting button presses
      2. Functions in Python
        1. Passing arguments to a function:
        2. Returning values from a function
        3. The scope of variables in a function
        4. GPIO callback functions
          1. DC motor control in Python
      3. Some mini-project challenges for the reader
      4. Summary
    5. Communication Interfaces
      1. UART - serial port
        1. Raspberry Pi Zero's UART port
          1. Setting up the Raspberry Pi Zero serial port
        2. Example 1 - interfacing a carbon dioxide sensor to the Raspberry Pi
        3. Python code for serial port communication
        4. I2C communication
        5. Example 2 - PiGlow
        6. Installing libraries
          1. Example
        7. Example 3 - Sensorian add-on hardware for the Raspberry Pi
          1. I2C drivers for the lux sensor
            1. Challenge
      2. The SPI interface
        1. Example 4 - writing to external memory chip
          1. Challenge to the reader
      3. Summary
    6. Data Types and Object-Oriented Programming in Python
      1. Lists
        1. Operations that could be performed on a list
          1. Append element to list:
          2. Remove element from list:
          3. Retrieving the index of an element
          4. Popping an element from the list
          5. Counting the instances of an element:
          6. Inserting element at a specific position:
            1. Challenge to the reader
          7. Extending a list
          8. Clearing the elements of a list
          9. Sorting the elements of a list
          10. Reverse the order of elements in list
          11. Create copies of a list
          12. Accessing list elements
            1. Accessing a set of elements within a list
          13. List membership
        2. Let's build a simple game!
      2. Dictionaries
      3. Tuples
      4. Sets
      5. OOP in Python
        1. Revisiting the student ID card example
          1. Class
          2. Adding methods to a class
          3. Doc strings in Python
          4. self
      6. Speaker controller
        1. Light control daemon
      7. Summary
    7. File I/O and Python Utilities
      1. File I/O
        1. Reading from a file
          1. Reading lines
        2. Writing to a file
        3. Appending to a file
          1. seek
          2. Read n bytes
          3. r+
            1. Challenge to the reader
        4. The with keyword
        5. configparser
          1. Challenge to the reader
        6. Reading/writing to CSV files
          1. Writing to CSV files
          2. Reading from CSV files
      2. Python utilities
        1. The os module
          1. Checking a file's existence
          2. Checking for a folder's existence
          3. Deleting files
          4. Killing a process
          5. Monitoring a process
        2. The glob module
          1. Challenge to the reader
        3. The shutil module
        4. The subprocess module
        5. The sys module
      3. Summary
    8. Requests and Web Frameworks
      1. The try/except keywords
        1. try...except...else
        2. try...except...else...finally
      2. Connecting to the Internet - web requests
        1. The application of requests - retrieving weather information
        2. The application of requests - publishing events to the Internet
      3. Flask web framework
        1. Installing Flask
        2. Building our first example
        3. Controlling appliances using the Flask framework
      4. Summary
    9. Awesome Things You Could Develop Using Python
      1. Image processing using a Raspberry Pi Zero
        1. OpenCV
          1. The verification of the installation
            1. A challenge to the reader
        2. Installing the camera to the Raspberry Zero
      2. Speech recognition
      3. Automating routing tasks
      4. Improving daily commute
        1. A challenge to the reader
        2. Project challenge
      5. Improving your vocabulary
        1. A challenge to the reader
        2. Project challenge
      6. Logging
      7. Threading in Python
      8. PEP8 style guide for Python
        1. Verifying PEP8 guidelines
      9. Summary
    10. Lets Build a Robot!
      1. Components of the robot
      2. Setting up remote login
        1. Changing the password
        2. Enabling SSH access
      3. Chassis setup
        1. Motor driver and motor selection
      4. Preparing the motor driver circuit
      5. Raspberry Pi Zero and motor driver assembly
        1. Raspberry Pi Zero and motor driver assembly
        2. Robot Power supply setup
        3. Setting up the motor power supply
          1. Using the Raspberry Pi Zero's 5V power supply
          2. Using an external power supply
        4. Testing the motors
          1. Motor driver detection
          2. Detecting motor driver
        5. Motor test
          1. Dependencies
      6. Camera setup
        1. Verification of camera function
      7. The web interface
      8. Camera setup for the web interface
      9. Buttons for robot control
      10. Troubleshooting tips
      11. Project enhancements
      12. Summary
    11. Home Automation Using the Raspberry Pi Zero
      1. Voice activated personal assistant
        1. Installing requisite packages
        2. How does it work?
        3. Setting up the audio tools
          1. Connecting the speaker
        4. Connecting the microphone
        5. Houndify
        6. Building voice commands
          1. Adding a button
      2. Web framework based appliance control/dashboard
        1. Building the web dashboard
      3. Personal Health Improvement—Sitting is the new smoking
        1. Installing requisite software packages
          1. Getting access keys for Fitbit client
        2. Fitbit API Test
          1. Building the visual aid
      4. Smart lawn sprinkler
      5. Summary
    12. Tips and Tricks
      1. Change your Raspberry Pi's password
        1. Updating your OS
      2. Setting up your development environment
        1. SSH access via Windows
        2. SSH access via Linux/macOS
        3. Transferring files to/from your Pi
          1. WinSCP
          2. Mac/Linux environment
        4. Git
        5. Command-line text editors
        6. Graphical text editors
        7. SSH aliases (on Mac/Linux Terminals)
        8. Saving SSH sessions on PuTTY
      3. VNC access to Raspberry Pi
        1. The RUN switch of the Raspberry Pi Zero board
        2. GPIO pin mapping
          1. Stackable breadboard
      4. Summary

    Product information

    • Title: Python Programming with Raspberry Pi
    • Author(s): Sai Yamanoor, Srihari Yamanoor
    • Release date: April 2017
    • Publisher(s): Packt Publishing
    • ISBN: 9781786467577