Wolfram Donat
Learn Raspberry Pi Programming with PythonLearn to Program on the World’s Most Popular Tiny Computer2nd ed.
Wolfram Donat
Palmdale, California, USA
ISBN 978-1-4842-3768-7e-ISBN 978-1-4842-3769-4
Library of Congress Control Number: 2018951226
© Wolfram Donat 2018
Apress standard
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, email orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

To Becky and Reed

Thank you for your patience and support when I disappear for hours, days, and weeks at a time to build all manner of off-the-wall things and then write about them.

Preface

It is difficult to believe that it’s been four years since I wrote the first edition of this book. In 2014, there was one version of the Raspberry Pi, a comparatively underpowered board with only one core in its ARM processor and only twenty GPIO pins to play around with. I was excited to order my first Pi, and I actually had to get on a waiting list to be on the second shipment list.

In addition, it seemed that every time you turned around, someone else was introducing a single-board computer (SBC) that was trying to appeal to the same niche that the Pi did—mainly hobbyists and makers who were ready to step up from the Arduino to something a bit more powerful.

The Pi resisted all attacks on its throne, however (not that it was ever interested in competing), and thrived. There are now seven models of the Pi: the model 1, the model 2, the model 2B, the model 3, the model 3B, the Pi Zero, and the Zero W. The Pi 3 is a computing powerhouse compared to the original model; its quad-core architecture lets it perform tasks like computer vision and machine learning, and overclocking it can give you speeds up to 1.5GHz, compared to the original’s 700MHz. Meanwhile, the Zero and the Zero W have such a low price point ($5US and $10US, respectively) that I often have to field questions from readers like “Why should I use an Arduino? The Pi Zero is cheaper!”

And the Pi is not the only game in town. Depending on how much you’re willing to spend, there are quite a few other SBCs that can be used for whatever project you’ve got in mind, ranging from the $30 BeagleBoard to the $550 NVidia Jetson TX2. I still like the Pi, however; it’s the board that first got me started playing around with embedded computers and the hobby projects that you can do with them. It’s inexpensive, so when I burn it up or brick it (as I’ve done quite a few times) I can replace it without breaking the bank. And it’s still powerful enough for quite a lot of things.

Thanks for reading this new book with me. If you’re a fan of my original book, thanks for sticking with me and putting up with the several mistakes that made it through to publication, and if you’re a new reader and a new Pi user, welcome! I hope to use the following pages to introduce you to an exciting new world of projects and computing.

Introduction

In 2006, when Eben Upton and the other founders of the Raspberry Pi Foundation looked at the state of computer science (CS) programs at universities, they were dismayed. Such programs were being reduced to “CS 101: How to Operate Microsoft Word” and “CS 203: Optimize Your Facebook Page.” Nobody, they realized, was learning how to program anymore, least of all before they entered college. So they hatched a plan—create a small, cheap computer that kids could learn to program on, like the Amigas, Spectrums, and Commodore 64s of yesteryear. They put an ARM processor on a board, gave it (eventually) 512MB of RAM and a VideoCore GPU, and allowed users to interface with it using a USB keyboard and mouse and an HDMI output. To make it easy to program, they designed it with Python in mind—a powerful, easy-to-learn language. And thus the Raspberry Pi was born.

I wrote my first program in BASIC on a Commodore VIC20, longer ago than I care to admit. At 5KB of RAM, it had less processing power than many of today’s microcontrollers, but I was still able to write a simple maze game on it, saving my progress as I went along to a cassette-tape drive. In the years since, I’ve traversed my way through the different computing platforms, from Windows 3.1 to Macintosh OS 8 to Linux, my OS of choice. It had been a long time since I was truly excited by a computer; the Pi was a breath of fresh air in a stale computing environment. Not only was it small and cheap, but it was also easy to get it to interact with the physical world—a real boon for anybody like me who was interested in designing and building physical systems. So, when I heard about its release, I signed up for the shipment like about a trillion other hobbyists/hackers/engineers and waited impatiently for mine to be delivered. Then, I started building stuff with it and writing about it, and I never looked back.

If you’ve bought (or were gifted) a Pi, but aren’t sure how to get started with it, this book is for you.

If you’ve got a Pi but aren’t sure about what you can or want to do with it, this book is for you.

If you’re even considering buying a Pi, for yourself or someone else, but haven’t yet because you keep wondering “What’s it good for?” or “Why not buy an Arduino?” then this book is definitely for you.

This book isn’t meant to be a textbook on Python, nor is it an exhaustive exploration of the Raspberry Pi and everything it can do. But it is meant to be a fun getting-started guide to this nifty little computer, in all of its possible permutations. I hope that after working through this book you’ll have an understanding of everything you can do with this little board when you mix it with some ingenuity and creativity.

If you want to work through the projects in order, feel free. If you’d rather skip around, doing only those projects that interest you, do that. Along the way, I hope you develop a familiarity with Python and Linux and the Pi that will enable you to continue on, building projects as you go, and perhaps inspiring others the way I hope to inspire you. Above all, I hope you enjoy the book and its projects. It was truly a blast to write. I always love hearing about your projects; you can reach me through the publisher or via Twitter: @wolfram_donat .

Happy computing!

Acknowledgments

Even though writing is a solitary activity, no author writes in a void, and I would like to acknowledge those who have helped this book become a reality. Rebecca and Reed, your support—as always—is invaluable. Oliver makes sure the door to the office works. Chloe ensures that all objects coming out of the workshop or garage have evasive-maneuver capabilities. Smudge gives and receives emotional support. Doofus and Pericles supervise.

Couldn’t do it without you guys.

Table of Contents

Index365

About the Author and About the Technical Reviewer

About the Author

Wolfram Donat

is a writer, engineer, and maker who has been futzing with computers and electronics for longer than he cares to admit. He firmly believes that if something is worth doing, it’s worth overdoing; everything needs a self-destruct button; and digital watches are still a pretty neat idea. He has a degree in computer engineering from the University of Alaska, and—despite several warnings—currently lives in Southern California with his wife, son, and a small menagerie.

 

About the Technical Reviewer

Massimo Nardone

has more than 24 years of experience in security, web/mobile development, cloud, and IT architecture. His true IT passions are security and Android.

He has been programming and teaching how to program with Android, Perl, PHP, Java, VB, Python, C/C++, and MySQL for more than 20 years.

He holds a Master of Science degree in Computing Science from the University of Salerno, Italy.

He has worked as a project manager, software engineer, research engineer, chief security architect, information security manager, PCI/SCADA auditor, and senior lead IT security/cloud/SCADA architect over the years.

Technical skills include security, Android, cloud, Java, MySQL, Drupal, Cobol, Perl, web and mobile development, MongoDB, D3, Joomla, Couchbase, C/C++, WebGL, Python, Pro Rails, Django CMS, Jekyll, Scratch, and more.

He has previously worked as visiting lecturer and supervisor for exercises at the Networking Laboratory of the Helsinki University of Technology (Aalto University). He holds four international patents (PKI, SIP, SAML, and Proxy areas).

He currently works as Chief Information Security Office (CISO) for Cargotec Oyj and is a member of the ISACA Finland Chapter Board.