You are previewing Raspberry Pi for Python Programmers Cookbook - Second Edition.
O'Reilly logo
Raspberry Pi for Python Programmers Cookbook - Second Edition

Book Description

Over 60 recipes that harness the power of the Raspberry Pi together with Python programming and create enthralling and captivating projects

About This Book

  • Install your first operating system, share files over the network, and run programs remotely

  • Construct robots and interface with your own circuits and purpose built add-ons, as well as adapt off-the-shelf household devices using this pragmatic guide

  • Packed with clear, step-by-step recipes to walk you through the capabilities of Raspberry Pi

  • Who This Book Is For

    Readers are expected to be familiar with programming concepts and Python (where possible Python 3 is used), although beginners should manage with the help of a good Python reference book and background reading. No prior knowledge of the Raspberry Pi or electronics is required; however, for the hardware sections you will need some basic electronic components/household tools to build some of the projects.

    What You Will Learn

  • Get the Raspberry Pi set up and running for the first time

  • Remotely connect to the Raspberry Pi and use your PC/laptop instead of a separate screen/keyboard

  • Get to grips with text, files and creating quick menus using Python

  • Develop desktop applications; handle images and process files with ease

  • Make use of graphics and user control to develop your own exciting games

  • Use the Raspberry Pi’s powerful GPU to create 3D worlds

  • Take control of the real world and interface with physical hardware, combining hardware and software for your own needs

  • Measure and control processes, respond to real events and monitor through the Internet

  • Learn about the Raspberry Pi hardware inputs/outputs, starting with the basics and beyond

  • Expand the capabilities of the Raspberry Pi with hardware expansion / add-on modules (use analogue inputs, drive servos and motors, and use SPI/I2C)

  • Create your own Pi-Rover or Pi-Hexpod driven by the Raspberry Pi

  • Make use of existing hardware by modifying and interfacing with it using the Raspberry Pi

  • In Detail

    Raspberry Pi cookbook for Python Programmers is a practical guide for getting the most out of this little computer. This book begins by guiding you through setting up the Raspberry Pi, performing tasks using Python 3 and introduces the first steps to interface with electronics. As you work through each chapter you will build up your skills and knowledge and apply them as you progress throughout the book, delving further and further into the unique abilities and features of the Raspberry Pi.

    Later, you will learn how to automate tasks by accessing files, build applications using the popular Tkinter library and create games by controlling graphics on screen. You will harness the power of the built-in graphics processor by using Pi3D to generate your own high quality 3D graphics and environments. Connect directly to the Raspberry Pi’s hardware pins to control electronics from switching on LEDs and responding to push buttons right through to driving motors and servos. Learn how to monitor sensors to gather real life data and to use it to control other devices, and view the results over the Internet. Apply what you have learnt by creating your own Pi-Rover or Pi-Hexipod robots. Finally, we will explore using many of the purpose built add-ons available for the Raspberry Pi, as well as interfacing with common household devices in new ways.

    Style and approach

    Written in a cookbook style, the book contains a series of recipes on various topics, ranging from simple to complex. It is an easy-to-follow and step-by-step guide with examples of various feature integration suitable for any search application.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Raspberry Pi for Python Programmers Cookbook - Second Edition
      1. Table of Contents
      2. Raspberry Pi for Python Programmers Cookbook - Second Edition
      3. Credits
      4. About the Author
      5. About the Reviewer
      6. www.PacktPub.com
        1. eBooks, discount offers, and more
          1. Why Subscribe?
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Safety and using electronics
        5. Sections
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        6. Conventions
        7. Reader feedback
        8. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started with a Raspberry Pi Computer
        1. Introduction
          1. Introducing the Raspberry Pi
          2. What is with the name?
          3. Why Python?
          4. Python 2 and Python 3
          5. Which version of Python should you use?
          6. The Raspberry Pi family – a brief history of Pi
          7. Which Pi to choose?
        2. Connecting the Raspberry Pi
          1. Getting ready
          2. How to do it…
          3. There's more…
            1. Secondary hardware connections
        3. Using NOOBS to set up your Raspberry Pi SD card
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Changing the default user password
            2. Ensuring that you shut down safely
            3. Preparing an SD card manually
            4. Expanding the system to fit in your SD card
            5. Accessing the RECOVERY/BOOT partition
            6. Using the tools to back up your SD card in case of failure
        4. Networking and connecting your Raspberry Pi to the Internet via the LAN connector
          1. Getting ready
          2. How to do it…
          3. There's more…
        5. Using built-in Wi-Fi and Bluetooth on the Raspberry Pi
          1. Getting ready
          2. How to do it…
            1. Connecting to your Wi-Fi network
            2. Connecting to Bluetooth devices
        6. Configuring your network manually
          1. Getting ready
          2. How to do it…
          3. There's more…
        7. Networking directly to a laptop or computer
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Direct network link
          5. See also
        8. Networking and connecting your Raspberry Pi to the Internet via a USB Wi-Fi dongle
          1. Getting ready
          2. How to do it…
          3. There's more…
            1. Using USB wired network adapters
        9. Connecting to the Internet through a proxy server
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        10. Connecting remotely to the Raspberry Pi over the network using VNC
          1. Getting ready
          2. How to do it…
          3. There's more…
        11. Connecting remotely to the Raspberry Pi over the network using SSH (and X11 Forwarding)
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Running multiple programs with X11 Forwarding
            2. Running as a desktop with X11 Forwarding
            3. Running PyGame and Tkinter with X11 Forwarding
        12. Sharing the home folder of the Raspberry Pi with SMB
          1. Getting ready
          2. How to do it…
        13. Keeping the Raspberry Pi up to date
          1. Getting ready
          2. How to do it…
          3. There's more…
      9. 2. Starting with Python Strings, Files, and Menus
        1. Introduction
        2. Working with text and strings
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Using files and handling errors
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Creating a boot-up menu
          1. How to do it…
          2. How it works…
          3. There's more…
        5. Creating a self-defining menu
          1. How to do it…
          2. How it works…
          3. There's more…
            1. Alternative script locations
            2. Adding scripts to PATH
      10. 3. Using Python for Automation and Productivity
        1. Introduction
        2. Using Tkinter to create graphical user interfaces
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Creating a graphical application – Start menu
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Displaying photo information in an application
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Organizing your photos automatically
          1. Getting ready
          2. How to do it…
          3. How it works…
      11. 4. Creating Games and Graphics
        1. Introduction
        2. Using IDLE3 to debug your programs
          1. How to do it…
          2. How it works…
        3. Drawing lines using a mouse on Tkinter Canvas
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Creating a bat and ball game
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Creating an overhead scrolling game
          1. Getting ready
          2. How to do it…
          3. How it works…
      12. 5. Creating 3D Graphics
        1. Introduction
        2. Starting with 3D coordinates and vertices
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Camera
            2. Shaders
            3. Lights
            4. Textures
        3. Creating and importing 3D models
          1. Getting ready
          2. How to do it…
          3. How it works...
          4. There's more…
            1. Creating or loading your own objects
            2. Changing the object's textures and .mtl files
            3. Taking screenshots
        4. Creating a 3D world to roam in
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Building 3D maps and mazes
          1. Getting ready
          2. How to do it…
          3. How it works...
          4. There's more...
            1. The Building module
            2. Using SolidObjects to detect collisions
      13. 6. Using Python to Drive Hardware
        1. Introduction
        2. Controlling an LED
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Controlling the GPIO current
        3. Responding to a button
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Safe voltages
            2. Pull-up and pull-down resistor circuits
            3. Protection resistors
        4. A controlled shutdown button
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Resetting and rebooting Raspberry Pi
            2. Adding extra functions
        5. The GPIO keypad input
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Generating other key combinations
            2. Emulating mouse events
        6. Multiplexed color LEDs
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Hardware multiplexing
            2. Displaying random patterns
            3. Mixing multiple colors
        7. Writing messages using Persistence of Vision
          1. Getting ready
          2. How to do it…
          3. How it works…
      14. 7. Sense and Display Real-World Data
        1. Introduction
        2. Using devices with the I2C bus
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using multiple I2C devices
            2. I2C bus and level shifting
            3. Using just the PCF8591 chip or adding alternative sensors
        3. Reading analog data using an analog-to-digital converter
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Gathering analog data without hardware
        4. Logging and plotting data
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Plotting live data
            2. Scaling and calibrating data
        5. Extending the Raspberry Pi GPIO with an I/O expander
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
            1. I/O expander voltages and limits
            2. Using your own I/O expander module
            3. Directly controlling an LCD alphanumeric display
        6. Capturing data in an SQLite database
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
            1. CREATE
            2. INSERT
            3. SELECT
            4. WHERE
            5. UPDATE
            6. DELETE
            7. DROP
        7. Viewing data from your own webserver
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
            1. Security
            2. Using MySQL instead
        8. Sensing and sending data to online services
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      15. 8. Creating Projects with the Raspberry Pi Camera Module
        1. Introduction
        2. Getting started with the Raspberry Pi camera module
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Using the camera with Python
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Class member and static functions
            2. Using a USB webcam instead
            3. Additional drivers for the Raspberry Pi camera
          5. See also
        4. Generating a time-lapse video
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Class inheritance and function overriding
            2. Disabling the camera LED
            3. Pi NoIR – taking night shots
        5. Creating a stop frame animation
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Improving the focus
            2. Creating a hardware shutter
        6. Making a QR code reader
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Generating QR codes
          5. See also
        7. Discover and experiment with OpenCV
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Color detection with OpenCV
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        9. Performing motion tracking with OpenCV
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      16. 9. Building Robots
        1. Introduction
        2. Building a Rover-Pi robot with forward driving motors
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Darlington array circuits
            2. Transistor and relay circuits
            3. Tethered or untethered robots
            4. Rover kits
        3. Using advanced motor control
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Motor speed control using PWM control
            2. Using I/O expanders
        4. Building a six-legged Pi-Bug robot
          1. Getting ready
          2. How to do it…
          3. How it works…
            1. Controlling the servos
            2. The servo class
            3. Learning to walk
            4. The Pi-Bug code for walking
        5. Controlling servos directly with Servoblaster
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Using an Infra-Red Remote Control with your Raspberry Pi
          1. Getting Ready
          2. How to do it…
          3. There's more…
        7. Avoiding objects and obstacles
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Ultrasonic reversing sensors
        8. Getting a sense of direction
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Calibrating the compass
            2. Calculating the compass bearing
            3. Saving the calibration
            4. Driving the robot using the compass
      17. 10. Interfacing with Technology
        1. Introduction
        2. Automating your home with remote sockets
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Sending RF control signals directly
            2. Extending the range of the RF Transmitter
            3. Determining the structure of the remote control codes
          5. Using SPI to control an LED matrix
            1. Getting ready
            2. How to do it…
            3. How it works...
            4. There's more...
              1. Daisy-chain SPI configuration
          6. Communicating using a serial interface
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
              1. Configuring a USB to RS232 device for the Raspberry Pi
              2. RS232 signals and connections
              3. Using the GPIO built-in serial pins
              4. The RS232 loopback
          7. Controlling the Raspberry Pi over Bluetooth
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
              1. Configuring Bluetooth module settings
          8. Controlling USB devices
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
              1. Controlling similar missile-type devices
              2. Robot arm
              3. Taking USB control further
      18. A. Hardware and Software List
      19. Index