Teach Yourself VISUALLY™ Raspberry Pi®

Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256

www.wiley.com

Published simultaneously in Canada

Copyright © 2014 by John Wiley & Sons, Inc., Indianapolis, Indiana

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400, fax 978-646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, 201-748-6011, fax 201-748-6008, or online at www.wiley.com/go/permissions.

Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.

Library of Congress Control Number: 2013954228

ISBN: 978-1-118-76819-8

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

Trademark Acknowledgments

Wiley, Visual, the Visual logo, Teach Yourself VISUALLY, Read Less - Learn More and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates. Raspberry Pi is a registered trademark of the Raspberry Pi Foundation in the United Kingdom and other countries. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.

LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.

FOR PURPOSES OF ILLUSTRATING THE CONCEPTS AND TECHNIQUES DESCRIBED IN THIS BOOK, THE AUTHOR HAS CREATED VARIOUS NAMES, COMPANY NAMES, MAILING, E-MAIL AND INTERNET ADDRESSES, PHONE AND FAX NUMBERS AND SIMILAR INFORMATION, ALL OF WHICH ARE FICTITIOUS. ANY RESEMBLANCE OF THESE FICTITIOUS NAMES, ADDRESSES, PHONE AND FAX NUMBERS AND SIMILAR INFORMATION TO ANY ACTUAL PERSON, COMPANY AND/OR ORGANIZATION IS UNINTENTIONAL AND PURELY COINCIDENTAL.

Contact Us

For general information on our other products and services please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993 or fax 317-572-4002.

For technical support please visit www.wiley.com/techsupport.

Credits

Acquisitions Editor

Aaron Black

Sr. Project Editor

Sarah Hellert

Technical Editor

Paul Hallett

Copy Editor

Kim Heusel

Director, Content Development & Assembly

Robyn Siesky

Vice President and Executive Group Publisher

Richard Swadley

About the Author

Richard Wentk has been building, working with, and writing about technology since the 1980s. He has had a lot of fun installing Raspberry Pi boards around his home for music storage, remote heating control and efficiency, and security. He is also an app developer and the author of a number of books for developers and Mac users. For the latest news and information, visit his site at www.zettaboom.com.

Author’s Acknowledgments

All books are a collaboration, and this one is no exception. I’d like to thank Aaron Black for giving the green light to this project, Paul Hallet for checking code and facts, and Sarah Hellert for making the transition from copy to print as smooth as possible.

Extra special thanks are due to Annette Saunders for cake.

How to Use This Book

Who This Book Is For

This book is for the reader who has never used this particular technology or software application. It is also for readers who want to expand their knowledge.

The Conventions in This Book

Steps

This book uses a step-by-step format to guide you easily through each task. Numbered steps are actions you must do; bulleted steps clarify a point, step, or optional feature; and indented steps give you the result.

Notes

Notes give additional information — special conditions that may occur during an operation, a situation that you want to avoid, or a cross-reference to a related area of the book.

Icons and Buttons

Icons and buttons show you exactly what you need to click to perform a step.

Tips

Tips offer additional information, including warnings and shortcuts.

Bold

Bold type shows command names, options, and text or numbers you must type.

Italics

Italic type introduces and defines a new term.

Teach Yourself VISUALLY™ Raspberry Pi®

Table of Contents

Chapter 1: Getting Started with Raspberry Pi

Introducing Raspberry Pi

Take a Tour of Raspberry Pi

Choose Accessories

Choose and Use a Case

Choose and Use a Case

Connect a Monitor

Connect a Monitor

Connect USB Accessories

Connect USB Accessories

Connect Power and Boot

Connect Power and Boot

Chapter 2: Selecting an Operating System

Understanding OS Options

Prepare an SD Card

Prepare an SD Card

Copy NOOBS to an SD card

Copy NOOBS to an SD Card

Select and Install Raspbian Wheezy

Select and Install Raspbian Wheezy

Back Up an SD Card

Back Up an SD Card

Get Started with BerryBoot

Get Started with BerryBoot

Chapter 3: Setting Up Raspbian

Set a Password

Select a Password

Select a Keyboard Layout

Select a Keyboard Layout

Select a Time Zone

Select a Time Zone

Manage Memory and SD Card Options

Manage Memory and SD Card Options

Set Up Overclocking

Set Up Overclocking

Connect to the Internet

Connect to the Internet

Revise the Configuration

Revise the Configuration

Update the Operating System

Update the Operating System

Launch the Desktop

Launch the Desktop

Shut Down the Pi

Shut Down the Pi

Chapter 4: Working with Applications

Introducing Raspberry Pi Apps

Set Up Wi-Fi

Set Up Wi-Fi

View Web Pages

View Web Pages

Work with Files in File Manager

Work with Files in File Manager

Edit Text with Leafpad

Edit Text with Leafpad

Configure the Desktop

Configure the Desktop

Configure the Launch Panel

Configure the Launch Panel

Chapter 5: Working with the Command Line

Introducing Linux

Using the Command Line

Using the Command Line

Become the Superuser

Become the Superuser

Understanding the File System

Understanding Linux Files

Navigate and List Directories

Navigate and List Directories

Create a Directory

Create a Directory

Delete Files and Directories

Delete Files and Directories

View a File

View a File

Find a File or Command

Find a File or Command

Copy, Move, and Rename Files

Copy, Move, and Rename Files

Work with File Permissions

Work with File Permissions

Using the Command Line History

Using the Command Line History

Chapter 6: Understanding Advanced Commands

Understanding Advanced Linux

Using the Nano Editor

Using the Nano Editor

Set Up Autologin

Set Up Autologin

Download and Install Applications

Download and Install Applications

Configure an Application

Configure an Application

Redirect Screen Output to a File

Redirect Screen Output to a File

Combine Commands with a Pipe

Combine Commands with a Pipe

Process Text with grep and sed

Process Text with grep and sed

Create a Simple Script

Create a Simple Script

Run a Command at Set Times

Run a Command at Set Times

Chapter 7: Networking Raspberry Pi

Understanding Networking

Set a Static IP Address

Set a Static IP Address

Set Up ssh

Set Up ssh

Using ssh for Remote Command Line Access

Using ssh for Remote Command Line Access

Set Up Remote Access with VNC

Set Up Remote Access with VNC

Share Files with Samba

Share Files with Samba

Set Up a Simple Web Server

Set Up a Simple Web Server

Create a Simple Web Page

Create a Simple Web Page

Install PHP

Install PHP

Create a Smart Web Page

Create a Smart Web Page

Send E-Mails

Send E-Mails

Get Started with curl and wget

Get Started with curl and wget

Put Your Pi on the Internet

Put Your Pi on the Internet

Chapter 8: Getting Started with Sound and Video

Understanding Media on the Pi

Set Monitor Resolution and Overscan

Set Monitor Resolution and Overscan

Set Up Audio

Set Up Audio

Play Video with omxplayer

Play Video with omxplayer

License Extra Video Formats

License Extra Video Formats

Connect a USB Media Drive

Connect a USB Media Drive

Chapter 9: Programming with Scratch

Understanding Scratch

Import a Costume

Import a Costume

Switch a Costume with Blocks

Switch a Costume with Blocks

Create a Loop

Create a Loop

Move a Sprite

Move a Sprite

Add a Bounce

Add a Bounce

Check for Collisions

Check for Collisions

Respond to the Mouse and Keyboard

Respond to the Mouse and Keyboard

Edit a Costume

Edit a Costume

Change the Stage Background

Change the Stage Background

Play Sounds

Play Sounds

Work with Variables

Work with Variables

Using the Pen

Using the Pen

Chapter 10: Getting Started with Python

Introducing Python

Launch IDLE

Launch IDLE

Work with Numbers

Work with Numbers

Create Variables

Create Variables

Get Started with Strings

Get Started with Strings

Split Strings

Split Strings

Pick Words from Strings

Pick Words from Strings

Convert Strings and Numbers

Convert Strings and Numbers

Work with Files

Work with Files

Create and Run a Python Script

Create and Run a Python Script

Chapter 11: Organizing Information with Python

Get Started with Lists

Get Started with Lists

Using List Methods

Using List Methods

Explore Tuples

Explore Tuples

Work with Dictionaries

Work with Dictionaries

Understanding Repeats and Decisions

Make Decisions

Make Decisions

Work with Loops and Repeats

Work with Loops and Repeats

Understanding Functions and Objects

Create a Function

Create a Function

Define a Class

Define a Class

Using a Class

Using a Class

Load Modules

Load Modules

Work with pickle

Work with pickle

Using the Debugger

Using the Debugger

Chapter 12: Getting Started with Pygame

Introducing Pygame

Create a Window

Create a Window

Close a Window

Close a Window

Load a Background Image

Load a Background Image

Read the Mouse

Read the Mouse

Respond to Keyboard Events

Respond to Keyboard Events

Scan the Keyboard

Scan the Keyboard

Create a Timed Loop

Create a Timed Loop

Chapter 13: Creating Graphics with Pygame

Understanding Graphics and Animation

Understanding Sprites and Images

Get Started with Surfaces

Get Started with Surfaces

Define Colors

Define Colors

Draw Shapes

Draw Shapes

Animate an Object

Animate an Object

Bounce a Shape

Bounce a Shape

Draw Text with Fonts

Draw Text with Fonts

Load and Show an Image

Load and Show an Image

Create a Sprite Class

Create a Sprite Class

Check for Sprite Collisions

Check for Sprite Collisions

Create a Breakout Game

Create a Breakout Game

Create a Breakout Game (continued)

Create a Breakout Game (continued)

Share Your Games

Share Your Games

Chapter 14: Adding Custom Hardware

Understanding Electronics

Using a Digital Multimeter

Using a Digital Multimeter

Set Up GPIO Control Software

Set Up GPIO Control Software

Control an LED with a Button

Control an LED with a Button

Connect a Digital Temperature Sensor

Connect a Digital Temperature Sensor

Log Sensor Readings to a File

Log Sensor Readings to a File

Graph Readings on a Web Page

Graph Readings on a Web Page

Connect a Real-Time Clock

Connect a Real-Time Clock

Create a Python Webcam

Create a Python Webcam

Control a Relay

Control a Relay

Learn to Solder

Learn to Solder

Create a Circuit on Stripboard

Create a Circuit on Stripboard

Understanding Further Options

Chapter 1

Getting Started with Raspberry Pi

To set up your Raspberry Pi, you must choose essential extras and connect them to your Pi. You can then power up your Pi for the first time and begin setting it up.

Introducing Raspberry Pi

Take a Tour of Raspberry Pi

Choose Accessories

Choose and Use a Case

Connect a Monitor

Connect USB Accessories

Connect Power and Boot

Introducing Raspberry Pi

The Raspberry Pi is a tiny computer designed in the United Kingdom and sold around the world at a very low price. The Pi ships as a bare circuit board with standard connections for a keyboard, mouse, monitor, and power supply. You must add these extras yourself. To keep costs down, the Pi uses an SD card, a small memory card, as a hard drive.

The Pi also includes extra connections you can use in your own electronic projects, and two software applications, Python and Scratch, for learning programming.

Compare the Pi and a PC/Mac

The Pi is much less powerful than a PC or Mac. You cannot run Microsoft Office on it. However, you can use it as a small media center and for programming games. You can also create network-based projects such as a web server, file server, or even a home automation system.

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!