ebook img

Learn to Program on the World’s Most Popular Tiny Computer: Learn to Program on the World’s Most Popular Tiny Computer PDF

389 Pages·2018·8.98 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learn to Program on the World’s Most Popular Tiny Computer: Learn to Program on the World’s Most Popular Tiny Computer

TECHNOLOGY IN ACTION™ Learn Raspberry Pi Programming with Python Learn to Program on the World’s Most Popular Tiny Computer — Second Edition — Wolfram Donat Learn Raspberry Pi Programming with Python Learn to Program on the World’s Most Popular Tiny Computer Second Edition Wolfram Donat Learn Raspberry Pi Programming with Python: Learn to Program on the World’s Most Popular Tiny Computer Wolfram Donat Palmdale, California, USA ISBN-13 (pbk): 978-1-4842-3768-7 ISBN-13 (electronic): 978-1-4842-3769-4 https://doi.org/10.1007/978-1-4842-3769-4 Library of Congress Control Number: 2018951226 Copyright © 2018 by Wolfram Donat This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. 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. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Aaron Black Development Editor: James Markham Coordinating Editor: Jessica Vakili Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) 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 [email protected], 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. For information on translations, please email [email protected] or visit http://www.apress. com/rights-permissions. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/978-1-4842-3768-7. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper 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. Table of Contents About the Author ��������������������������������������������������������������������������������xv About the Technical Reviewer ����������������������������������������������������������xvii Acknowledgments �����������������������������������������������������������������������������xix Preface ����������������������������������������������������������������������������������������������xxi Introduction �������������������������������������������������������������������������������������xxiii Chapter 1: Introducing the Raspberry Pi ����������������������������������������������1 The History of the Raspberry Pi ����������������������������������������������������������������������������3 Exploring the Pi �����������������������������������������������������������������������������������������������������8 The GPIO Pins ��������������������������������������������������������������������������������������������������9 USB & Ethernet ����������������������������������������������������������������������������������������������10 Audio Jack �����������������������������������������������������������������������������������������������������10 Camera Connector �����������������������������������������������������������������������������������������10 HDMI ��������������������������������������������������������������������������������������������������������������11 Power ������������������������������������������������������������������������������������������������������������11 Display �����������������������������������������������������������������������������������������������������������11 The System on a Chip ������������������������������������������������������������������������������������12 SD Card ����������������������������������������������������������������������������������������������������������12 Not Visible ������������������������������������������������������������������������������������������������������13 The Pi Zero/Zero W ����������������������������������������������������������������������������������������������13 GPIO ���������������������������������������������������������������������������������������������������������������14 Camera Connector �����������������������������������������������������������������������������������������14 v TTaabbllee ooff CCoonnTTeennTTss Power ������������������������������������������������������������������������������������������������������������14 USB ����������������������������������������������������������������������������������������������������������������14 HDMI ��������������������������������������������������������������������������������������������������������������15 SD Card ����������������������������������������������������������������������������������������������������������15 System on a Chip �������������������������������������������������������������������������������������������15 Not Visible ������������������������������������������������������������������������������������������������������15 Comparing the Raspberry Pi to Similar Devices �������������������������������������������������16 Getting Started with the Pi ����������������������������������������������������������������������������������16 Hardware Requirements of the Pi �����������������������������������������������������������������������17 Connecting to Power �������������������������������������������������������������������������������������17 Adding a Monitor �������������������������������������������������������������������������������������������17 Adding a USB Hub �����������������������������������������������������������������������������������������18 The Pi Operating System �������������������������������������������������������������������������������������19 Formatting the Card ��������������������������������������������������������������������������������������20 Installing the OS ��������������������������������������������������������������������������������������������20 Connecting the Peripherals ���������������������������������������������������������������������������������21 Configuring the Pi �����������������������������������������������������������������������������������������������22 Shutting Down the Pi ������������������������������������������������������������������������������������������25 Summary�������������������������������������������������������������������������������������������������������������26 Chapter 2: Linux by the Seat of Your Pants ����������������������������������������27 Getting Started with Linux on the Pi �������������������������������������������������������������������30 Linux Files and the File System ���������������������������������������������������������������������31 Root User Versus sudo �����������������������������������������������������������������������������������32 Commands ����������������������������������������������������������������������������������������������������33 Exercise: Navigating in the Linux File System �����������������������������������������������39 vi TTaabbllee ooff CCoonnTTeennTTss Shells in Linux �����������������������������������������������������������������������������������������������42 Package Managers ����������������������������������������������������������������������������������������43 Text Editors ���������������������������������������������������������������������������������������������������������45 Vim Versus emacs Versus nano ���������������������������������������������������������������������46 Using Vim �������������������������������������������������������������������������������������������������������47 Using emacs ��������������������������������������������������������������������������������������������������49 Using nano �����������������������������������������������������������������������������������������������������51 Default Text Editor �����������������������������������������������������������������������������������������52 Summary�������������������������������������������������������������������������������������������������������������53 Chapter 3: Introducing Python ������������������������������������������������������������55 Scripting Versus a Programming Language ��������������������������������������������������������56 The Python Philosophy ����������������������������������������������������������������������������������������59 Getting Started with Python ��������������������������������������������������������������������������������61 Running Python Using IDLE ���������������������������������������������������������������������������61 Running Python Using the Terminal ���������������������������������������������������������������67 Running Python Using Scripts �����������������������������������������������������������������������67 Exploring Python Data Types �������������������������������������������������������������������������������69 Numbers ��������������������������������������������������������������������������������������������������������70 Strings �����������������������������������������������������������������������������������������������������������71 Lists ���������������������������������������������������������������������������������������������������������������73 Dictionaries ���������������������������������������������������������������������������������������������������76 Tuples and Files ���������������������������������������������������������������������������������������������77 Programming with Python ����������������������������������������������������������������������������������79 IF tests �����������������������������������������������������������������������������������������������������������80 Loops �������������������������������������������������������������������������������������������������������������83 vii TTaabbllee ooff CCoonnTTeennTTss Functions �������������������������������������������������������������������������������������������������������86 Objects and Object-Oriented Programming ���������������������������������������������������88 Summary�������������������������������������������������������������������������������������������������������������89 Chapter 4: Electronics at 100 MPH �����������������������������������������������������91 Basic Electricity Concepts �����������������������������������������������������������������������������������93 Required Tools for Robotics ��������������������������������������������������������������������������������96 Screwdrivers �������������������������������������������������������������������������������������������������96 Pliers and Wire Strippers �������������������������������������������������������������������������������96 Wire Cutters ���������������������������������������������������������������������������������������������������99 Files �������������������������������������������������������������������������������������������������������������100 Magnifying Light ������������������������������������������������������������������������������������������101 Hot-Glue Gun �����������������������������������������������������������������������������������������������101 Assorted Glues���������������������������������������������������������������������������������������������102 Multimeter ���������������������������������������������������������������������������������������������������102 Power Supplies ��������������������������������������������������������������������������������������������104 Breadboard ��������������������������������������������������������������������������������������������������105 Power Strip ��������������������������������������������������������������������������������������������������107 Soldering Iron ����������������������������������������������������������������������������������������������108 General Safety Rules �����������������������������������������������������������������������������������������109 Working with Heat ���������������������������������������������������������������������������������������110 Working with Sharp Objects ������������������������������������������������������������������������110 Wear Safety Glasses ������������������������������������������������������������������������������������111 Fire Extinguishers at the Ready �������������������������������������������������������������������111 Keep a First-Aid Kit Handy ���������������������������������������������������������������������������112 Work in a Ventilated Area�����������������������������������������������������������������������������112 viii TTaabbllee ooff CCoonnTTeennTTss Organizing Your Workplace ��������������������������������������������������������������������������113 Bonus: Soldering Techniques ����������������������������������������������������������������������������115 Summary�����������������������������������������������������������������������������������������������������������118 Chapter 5: The Web Bot ��������������������������������������������������������������������119 Bot Etiquette �����������������������������������������������������������������������������������������������������121 The Connections of the Web �����������������������������������������������������������������������������123 Web Communications Protocols ������������������������������������������������������������������123 Web Page Formats ��������������������������������������������������������������������������������������124 A Request Example ��������������������������������������������������������������������������������������125 Our Web Bot Concept ����������������������������������������������������������������������������������������126 Parsing Web Pages �������������������������������������������������������������������������������������������126 Coding with Python Modules ����������������������������������������������������������������������������129 Using the Mechanize Module ����������������������������������������������������������������������130 Parsing with Beautiful Soup ������������������������������������������������������������������������131 Downloading with the urllib Library ������������������������������������������������������������131 Deciding What to Download ������������������������������������������������������������������������������132 Choosing a Starting Point ����������������������������������������������������������������������������133 Storing Your Files �����������������������������������������������������������������������������������������133 Writing the Python Bot ��������������������������������������������������������������������������������������135 Reading a String and Extracting All the Links ���������������������������������������������135 Looking For and Downloading Files ������������������������������������������������������������136 Testing the Bot ���������������������������������������������������������������������������������������������138 Creating Directories and Instantiating a List �����������������������������������������������139 The Final Code ��������������������������������������������������������������������������������������������������142 Summary�����������������������������������������������������������������������������������������������������������145 ix TTaabbllee ooff CCoonnTTeennTTss Chapter 6: The Weather Station ��������������������������������������������������������147 A Shopping List of Parts ������������������������������������������������������������������������������������149 Using the I2C Protocol ���������������������������������������������������������������������������������������150 Using an Anemometer ���������������������������������������������������������������������������������������151 Building the Anemometer ����������������������������������������������������������������������������152 Connecting the Anemometer to the Pi ���������������������������������������������������������158 Correlating Revolutions per Second with Wind Speed ��������������������������������159 Connecting the Digital Compass �����������������������������������������������������������������������162 Connecting the Temperature/Humidity Sensor �������������������������������������������������166 Connecting the Barometer ��������������������������������������������������������������������������������168 Connecting the Bits �������������������������������������������������������������������������������������������171 The Final Code ��������������������������������������������������������������������������������������������������175 Summary�����������������������������������������������������������������������������������������������������������178 Chapter 7: The Media Server ������������������������������������������������������������179 A Shopping List of Parts ������������������������������������������������������������������������������������180 Using an NTFS Drive �����������������������������������������������������������������������������������������180 Installing Samba �����������������������������������������������������������������������������������������������184 Configuring Samba �������������������������������������������������������������������������������������������186 Setting Linux Permissions ��������������������������������������������������������������������������������188 Restarting the Samba Service ���������������������������������������������������������������������189 Connecting with Linux/OS X ������������������������������������������������������������������������������191 Kodi and Plex ����������������������������������������������������������������������������������������������������193 Where’s Python? �����������������������������������������������������������������������������������������������196 Summary�����������������������������������������������������������������������������������������������������������197 x

Description:
Learn how to program your nifty new $35 computer to make a web spider, a weather station, a media server, and more. This book explores how to make a variety of fun and even useful projects, from a web bot to search and download files to a toy to drive your pets insane. Even if you're completely new
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.