Book Image

Python Programming with Raspberry Pi

By : Sai Yamanoor, Srihari Yamanoor
Book Image

Python Programming with Raspberry Pi

By: Sai Yamanoor, Srihari Yamanoor

Overview of this book

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.
Table of Contents (17 chapters)
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Preface

The Raspberry Pi represents the best in innovation in computer science, education, entertainment, hobby hacking, and several other categories that you can classify the device family into. Even as this book is entering publication, the Raspberry Pi family of products have become the third best selling computers of all time. It is anyone’s guess that with the continuing innovation coming out of the Raspberry Pi Foundation and the thousands of people across the planet constantly demonstrating newer and better examples innovative solutions with the various flavors of Raspberry Pi, what new heights this product line might reach!

One of the main goals of the Raspberry Pi is affordability. And the purpose of this book is to allow the beginner to learn Programming in Python, as well as manipulating hardware. The reader may have worked a little bit on hardware, and a little bit on programming, and want to strengthen skills in either area. The reader may also just be interested in doing more projects with the Pi Zero in Python, and of course, some of the projects in this book, briefly highlighted below might be of interest.

The book starts off with some warm up examples, helping develop a familiarity with the Raspberry Pi environment, and the projects increase in variety and complexity as the book progresses. While, readers who have advanced a bit before approaching the book can skip a few chapters, we recommend beginners progress through all chapters, since the concepts build on top of each other.

What this book covers

Chapter 1, Getting Started with Python and the Raspberry Pi Zero, introduces the Raspberry Pi Zero and the Python programming language, its history, and its features. We will set up the Raspberry Pi for Python development and write the first program.

Chapter 2, Arithmetic Operations, Loops, and Blinky Lights, walks through the arithmetic operations in Python and loops in Python. In the second half of the chapter, we will discuss the Raspberry Pi Zero’s GPIO interface and then learn to blink an LED using a GPIO pin.

Chapter 3, Conditional Statements, Functions, and Lists, discusses the types of conditional statements, variables, and logical operators in Python. We will also discuss functions in Python. Then, we will learn to write a function that is used to control DC motors using the Raspberry Pi Zero.

Chapter 4, Communication Interfaces, covers all the communication interfaces available on the Raspberry Pi Zero. This includes the I2C, UART, and the SPI interface. These communication interfaces are widely used to interface sensors. Hence, we will demonstrate the operation of each interface using a sensor as an example.

Chapter 5, Data Types and Object-Oriented Programming in Python, discusses object-oriented programming in Python and the advantages of object-oriented programming. We will discuss this using a practical example.

Chapter 6, File I/O and Python Utilities, discusses reading and writing to files. We discuss creating and updating config files. We will also discuss some utilities available in Python.

Chapter 7, Requests and Web Frameworks, discusses libraries and frameworks that enable retrieving data from the Web. We will discuss an example, fetching local weather information. We will also discuss running a web server on the Raspberry Pi Zero.

Chapter 8, Awesome Things You Could Develop Using Python, discusses libraries and frameworks that enable retrieving data from the web. We will discuss examples such as fetching the local weather information. We will also discuss running a web server on the Raspberry Pi Zero.

Chapter 9, Let's Build a Robot!, shows how we built an indoor robot using the Raspberry Pi Zero as the controller and documented our experience as a step-by-step guide. We wanted to demonstrate the awesomeness of the combination of Python and the Raspberry Pi Zero’s peripherals.

Chapter 10, Home Automation Using The Raspberry Pi Zero, discusses four projects, a voice-activated personal assistant, a web framework-based appliance control, a physical activity motivation tool, and a smart lawn sprinkler. Through these projects we provide more examples of the new hardware and programming implementations.

Chapter 11, Tips and Tricks, concludes the book with useful hardware and software tips and shortcuts that will help you as you step beyond the concepts and exercises in this book to implement your own projects and solutions, or simply explore the areas of programming and hardware hacking as a hobby and a source of entertainment.

What you need for this book

The following hardware is recommended:

  • A laptop computer, with any OS
  • Raspberry Pi Zero
  • A microSD card, either 8 GB or 16 GB
  • A USB keyboard
  • A USB mouse
  • A display with HDMI input
  • A USB Wi-Fi card
  • Power supply, minimum 500 mA.
  • Display cables
  • Other accessories, as required to complete the various projects in the book

Who this book is for

This book is primarily aimed at hobbyists and makers. So, some basic exposure to programming, hardware and the Linux OS is assumed. Even without exposure to these areas, it is possible to follow along and benefit from the book. Wherever possible, we have tried our best to point you to free, open and/or cost effective resources to follow along with the projects in the book.

.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The remove() method finds the first instance of the element (passed an argument) and removes it from the list."

A block of code is set as follows:

      try: 
         input_value = int(value) 
      except ValueError as error:  
         print("The value is invalid %s" % error)

Any command-line input or output is written as follows:

    sudo pip3 install schedule

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Select the A8 Serial option from the drop-down menu."

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book 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 files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

You can download the latest code samples from the code repository belonging to this book from the author's code repository at https://github.com/sai-y/pywpi. You can find additional resources including bonus projects at http://pywithpi.com.

The code bundle for the book is also hosted on Packt's GitHub repository at https://github.com/PacktPublishing/Python-Programming-with-Raspberry-Pi-Zero. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/PythonProgrammingwithRaspberryPiZero_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.