# Raspberry Pi Cookbook for Python Programmers

4.5 (8 reviews total)
By Tim Cox
• $27.99 eBook •$46.99 Print + eBook
• $12.99 eBook + Subscription What do you get with a Packt Subscription? • Instant access to this title and 7,500+ eBooks & Videos • Constantly updated with 100+ new titles each month • Breadth and depth in over 1,000+ technologies About this book The Raspberry Pi foundation has been selling their computers since 2012 with the aim of inspiring programmers and engineers. Raspberry Pi Cookbook for Python Programmers is a practical guide for getting the most out of this little computer. It will guide you through the many uses of the Raspberry Pi and allow you to showcase the best it has to offer. Discover what the Raspberry Pi has to offer using detailed Python 3 examples that you can adapt and extend; see where your creativity takes you! "Raspberry Pi Cookbook for Python Programmers" 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 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. The "Raspberry Pi Cookbook for Python Programmers" will allow you to discover the vast range of abilities this micro-computer has. By following the clear examples you can pick up many new skills and arm yourself with the tools to create your own exciting and interesting projects. Publication date: April 2014 Publisher Packt Pages 402 ISBN 9781849696623 ## Chapter 1. Getting Started with a Raspberry Pi Computer In this chapter, we will cover the following topics: • Connecting the Raspberry Pi • Using NOOBS to set up your Raspberry Pi SD card • Networking and connecting your Raspberry Pi to the Internet via the LAN connector • Configuring your network manually • Networking directly to a laptop or computer • Networking and connecting your Raspberry Pi to the Internet via a USB Wi-Fi dongle • Connecting to the Internet through a proxy server • Connecting remotely to the Raspberry Pi over the network using VNC • Connecting remotely to the Raspberry Pi over the network using SSH (and X11 Forwarding) • Sharing the home folder of the Raspberry Pi with SMB • Keeping the Raspberry Pi up to date ## Introduction This chapter introduces the Raspberry Pi and the process to set it up for the first time. We will connect the Raspberry Pi to a suitable display, power, and peripherals. We shall install an operating system on an SD card. This is required for the system to boot. Next, we will ensure that we can connect successfully to the Internet through a local network. Finally, we will make use of the network to provide ways to remotely connect to and/or control the Raspberry Pi from other computers and devices as well as to ensure that the system is kept up to date. Once you have completed the steps within this chapter, your Raspberry Pi will be ready for you to use for programming. If you already have your Raspberry Pi set up and running, ensure that you take a look through the following sections as there are many helpful tips. ### Introducing the Raspberry Pi The Raspberry Pi is a single-board computer created by the Raspberry Pi Foundation, a charity formed with the primary purpose of reintroducing low-level computer skills to children in the UK. The aim was to rekindle the microcomputer revolution from the 1980s, which produced a whole generation of skilled programmers. Even before the computer was released at the end of February 2012, it was clear that the Raspberry Pi had gained a huge following worldwide and has now sold over 2 million units. The following image represents a Raspberry Pi Model B: A Raspberry Pi Model B (revision 2.0) ### What is with the name? The name, Raspberry Pi, was the combination of the desire to create an alternative fruit-based computer (such as Apple, BlackBerry, and Apricot) and a nod to the original concept of a simple computer that can be programmed using Python (shortened to Pi). Within this book, we will take this little computer, find out how to set it up, and then explore its capabilities chapter by chapter using the Python programming language. ### Why Python? It is often asked, "Why has Python been selected as the language to use on the Raspberry Pi?". The fact is that Python is just one of the many programming languages that can be used on the Raspberry Pi. There are many programming languages that you can choose, from high-level graphical block programming, such as Scratch, to traditional C, right down to BASIC, and even raw Machine Code Assembler. A good programmer often has to be code multilingual to be able to play to the strengths and weaknesses of each language in order to best meet the needs of their desired application. It is useful to understand how different languages (and programming techniques) try to overcome the challenge of converting "what you want" into "what you get" as this is what you are trying to do as well while you program. Python has been selected as a good place to start when learning about programming by providing a rich set of coding tools while still allowing simple programs to be written without fuss. This allows beginners to gradually be introduced to the concepts and methods on which modern programming languages are based without requiring them to know it all from the start. It is very modular with lots of additional libraries that can be imported to quickly extend the functionality. You will find that over time, this encourages you to do the same, and you will want to create your own modules that you can plug in to your own programs, thus taking your first steps into structured programming. Like all programming languages, Python isn't perfect; things such as adding a space at the start of a line will often break your code (indents matter a lot in Python; they define how blocks of code are grouped together). Generally, Python is slow; since it is interpreted, it takes time to create a module while it is running the program. This can be a problem if you need to respond to time critical events. However, you can precompile Python or use modules written in other languages to overcome this. It hides the detail; this is both an advantage and disadvantage. It is excellent for beginners but can be difficult when you have to second-guess aspects such as data-types, but this in turn forces you to consider all the possibilities, which can be a good thing. ### Python 2 and Python 3 A massive source of confusion for beginners is that there are two versions of Python on the Raspberry Pi (Version 2.7 and Version 3.2), which are not compatible with one another, so code written for Python 2.7 may not run with Python 3.2 (and vice versa). The Python Software Foundation is continuously working to improve and move forward with the language, which sometimes means they have to sacrifice backward compatibility in order to embrace new improvements (and importantly, remove redundant and legacy ways of doing things). ### Note Supporting both Python 2 or Python 3 There are many tools that will ease the transition from Python 2 to Python 3, including converters such as 2to3, which will parse and update your code to use Python 3 methods. This process is not perfect, and in some cases, you'll need to manually rewrite sections and fully retest everything. You can write the code and libraries that will support both. The import __future__ statement allows you to import the friendly methods of Python 3 and run them using Python 2.7. ### Which version of Python should you use? Essentially, the selection of which version to use will depend on what you intend to do. For instance, you may require Python 2.7 libraries, which are not yet available for Python 3.2. Python 3 has been available since 2008, so these tend to be older or larger libraries that have not been translated. In many cases, there are new alternatives to legacy libraries; however, their support can vary. In this book, we have used Python 3.2, which is also compatible with Python 3.3. ## Connecting the Raspberry Pi There are many ways to wire up the Raspberry Pi and use the various interfaces to view and control content. For typical use, most users will require power, display (with audio), and a method of input such as a keyboard and mouse. To access the Internet, refer to the Networking and connecting your Raspberry Pi to the Internet via the LAN connector recipe. ### Getting ready Before you can use your Raspberry Pi, you will need an SD card with an operating system installed or with the New Out Of Box System (NOOBS) on it, as discussed in the Using NOOBS to set up your Raspberry Pi SD card recipe. The following section will detail the types of devices you can connect to the Raspberry Pi and importantly, how and where to plug them in. As you will discover later, once you have your Raspberry Pi set up, you may decide to connect remotely and use it through a network link, in which case you only need power and a network connection. Refer to the following sections: Connecting remotely to the Raspberry Pi over the network using VNC and Connecting remotely to the Raspberry Pi over the network using SSH (and X11 Forwarding). ### How to do it… The layout of the Raspberry Pi is shown in the following figure: The Raspberry Pi connection layout (Model B revision 2.0) The description of the preceding figure is explained as follows: • Display: The Raspberry Pi supports the following three main display connections; if both HDMI and Composite video are connected, it shall default to the HDMI only. • HDMI For best results, use a TV or monitor that has an HDMI connection, thus allowing the best resolution display (1080p) and also digital audio output. If your display has a DVI connection, you may be able to use an adapter to connect through the HDMI. There are several types of DVI connections; some support analogue (DVI-A), some digital (DVI-D), and some both (DVI-I). the Raspberry Pi is only able to provide a digital signal through the HDMI, so an HDMI to DVI-D adapter is recommended (shown with a tick mark in the following screenshot). This lacks the four extra analogue pins (shown with a cross mark in the following screenshot), thus allowing it to fit into both DVI-D and DVI-I type sockets: HDMI to DVI connection (DVI-D adaptor) If you wish to use an older monitor (with a VGA connection), an additional HDMI to VGA converter is required. • Analogue An alternative display method is to use the analogue composite video connection (via the RCA socket); this can also be attached to an S-Video or European SCART adapter. However, the analogue video output has a maximum resolution of 640 x 480 pixels, so it is not ideal for general use. When using the RCA connection or a DVI input, audio has to be provided separately by the analogue audio connection. • Direct Display DSI A touch display produced by the Raspberry Pi Foundation will connect directly into the DSI socket. This can be connected and used at the same time as the HDMI or Analogue video output to create a dual display setup. • Stereo Analogue Audio: This provides an analogue audio output for headphones or amplified speakers. The audio can be switched between analogue (Stereo Socket) and digital (HDMI) using amixer or alsamixer. ### Note To find out more information about a particular command in the terminal, you can use the following man command before the terminal reads the manual (most commands should have one): man amixer  Some commands also support the --help option for more concise help, shown as follows: amixer --help  • Network (Model B Only): The network connection is discussed in the Networking and connecting your Raspberry Pi to the Internet via the LAN connector recipe later in this chapter. If we use the Model A Raspberry Pi, it is possible to add a USB network adapter to add wired or even wireless networking (refer to the Networking and connecting your Raspberry Pi to the Internet via a USB Wi-Fi dongle recipe). • USB (x1 Model A, x2 Model B) – Using a keyboard and mouse: The Raspberry Pi should work with most USB keyboards and mice available. However, you may encounter issues if your device draws more than 140mA, in which case a powered USB hub is recommended. You can also use wireless mice and keyboards, which use RF Dongles. However, additional configuration is required for items that use the Bluetooth dongles. If there is a lack of power supplied by your power supply or the devices are drawing too much current, you may experience the keyboard keys appearing to stick, and in severe cases, corruption of the SD card. ### Note USB power can be more of an issue with the early Model B revision 1 boards that were available prior to October 2012. They included additional Polyfuses on the USB output and tripped if an excess of 140mA was drawn. The Polyfuses can take several hours or days to recover completely, thus causing unpredictable behavior to remain even when the power is improved. You can identify a revision 1 board as it lacks the two mounting holes that are present on the revision 2 board. Debian Linux (upon which Raspbian is based) supports many common USB devices, such as flash storage drives, hard disk drives (external power may be required), cameras, printers, Bluetooth, and Wi-Fi adapters. Some devices will be detected automatically while others will require drivers to be installed. • Micro USB Power: The Raspberry Pi requires a 5V power supply, which can comfortably supply at least 700mA (1A or more recommended) with a micro USB connection. It is possible to power the unit using portable battery packs, such as the ones suitable for powering or recharging tablets. Again, ensure that they can supply 5V at 700mA or over. You should aim to make all other connections to the Raspberry Pi before connecting the power. However, USB devices, audio, and network may be connected and removed while it is running without problems. ### There's more… In addition to the standard primary connections you would expect to see on a computer, the Raspberry Pi also has a number of additional connections. #### Secondary hardware connections Each of the following connections provide additional interfaces for the Raspberry Pi: • P1 13 x 2 GPIO pin header: This is the main GPIO header of the Raspberry Pi used for interfacing directly with hardware components. We use this connection in Chapter 6, Using Python to Drive Hardware, Chapter 7, Sense and Display Real World Data, Chapter 9, Building Robots, and Chapter 10, Interfacing with Technology. • P5 8 x 2 GPIO pin header: This is present on board revision 2.0 only (no pins fitted). • P6 reset: This is present on board revision 2.0 only (no pins fitted). A reset is triggered when P6-Pin1 (reset) and P6-Pin2 (GND) are connected together. • P5 and P6 pin header ### Tip We use P5 and P6 in the A controlled shutdown button recipe in Chapter 6, Using Python to Drive Hardware. • P2/P3 GPU/LAN JTAG: The Joint Test Action Group (JTAG) is a programming and debugging interface used to configure and test processors. A specialist JTAG device is required to use this interface. • S5 Direct Camera CSI: This connection supports the Raspberry Pi Camera module (as used in Chapter 8, Creating Projects with the Raspberry Pi Camera Module). • S2 Direct Display DSI: This connection is intended to support a display (an add-on released by the Raspberry Pi Foundation). ## Using NOOBS to set up your Raspberry Pi SD card The Raspberry Pi requires the operating system to be loaded onto an SD card before it will start up. The easiest way to set up the SD card is to use NOOBS; you may find that you can buy an SD card with NOOBS already loaded on it. NOOBS provides an initial start menu providing options to install several of the available operating systems onto your SD card. ### Getting ready Since NOOBS creates a RECOVERY Partition to keep the original installation images, a 4 GB SD card or larger is recommended. You will also need an SD card reader (experience has shown that some built-in card readers can cause issues, so an external USB type reader is recommended). If you are using an SD card that you have used previously, you may need to reformat it to remove any previous partitions and data. NOOBS expects the SD card to consist of a single FAT32 partition. If using Windows or Mac OS X, you can use SD association's formatter, as shown in the following screenshot (available at https://www.sdcard.org/downloads/formatter_4/): Get rid of any partitions on the SD card using SD formatter From the Option Setting dialog box, set Format Size Adjustment. This will remove all the SD card partitions that were created previously. If using Linux, you can use gparted to clear any previous partitions and reformat it as a FAT32 partition. The full NOOBS package (typically around 1.3 GB) contains a selection of the most popular Raspberry Pi operating system's images built in. A lite version of NOOBS is also available that has no preloaded operating systems (although, a smaller initial download of 20 MB and a wired network connection on the Raspberry Pi is required to directly download the operating system you intend to use). NOOBS is available at http://www.raspberrypi.org/downloads with the documentation available at https://github.com/raspberrypi/noobs. ### How to do it… By performing the following steps, we will prepare the SD card to run NOOBS. This will then allow us to select and install the operating system we want to use: 1. Get your SD card ready. 2. On a freshly formatted or new SD card, copy the contents of the NOOBS_vX.zip file. When it has finished copying, you should end up with something like the following screenshot on the SD card: NOOBS files extracted onto the SD card ### Note The files may vary slightly with different versions of NOOBS, and the icons displayed may be different on your computer. 3. You can now place the card into your Raspberry Pi, connect it to a keyboard and display, and turn the power on. (refer to the Connecting Up the Raspberry Pi recipe for details on what you need and how to do this). By default, NOOBS will display via the HDMI connection. If you have another type of screen (or you don't see anything), you will need to manually select the output type by pressing 1, 2, 3, or 4 according to the following functions: • Key 1 stands for the Standard HDMI mode (the default mode) • Key 2 stands for the Safe HDMI mode (alternative HDMI settings if the output has not been detected) • Key 3 stands for Composite PAL (for connections made via the RCA analogue video connection) • Key 4 stands for Composite NTSC (again, for connections via the RCA connector) This display setting will also be set for the installed operating system. After a short while, you will see the NOOBS selection screen that lists the available distributions that have been included. There are many more distributions that are available, but these are the ones the Raspberry Pi Foundation has selected for the NOOBS system. Click on Raspbian as this is the operating system being used in this book. Press Enter or click on Install OS, and confirm that we wish to overwrite all the data on the card. This will overwrite any distributions previously installed using NOOBS but will not remove the NOOBS system; you can return to it at any time by pressing Shift when you turn the power on. It will take around 10 to 30 minutes to write the data to the card depending on its speed. When it completes and the Image Applied Successfully message appears, click on OK and the Raspberry Pi will start to boot into raspi-config. ### How it works… The purpose of writing the image file to the SD card in this manner is to ensure that the SD card is formatted with the expected filesystem partitions and files required to correctly boot the operating system. When the Raspberry Pi powers up, it loads some special code contained within the GPU's internal memory (commonly referred to as the binary blob by the Raspberry Pi Foundation). The binary blob provides the instructions required to read the BOOT Partition on the SD card, which (in the case of a NOOBS install) will load NOOBS from the RECOVERY partition. If, at this point, Shift is pressed, NOOBS will load the recovery and installation menu. Otherwise, NOOBS will begin loading the OS as specified by the preferences stored in the SETTINGS Partition. When loading the operating system, it will boot via the BOOT partition using the settings defined in config.txt and options in cmdline.txt to finally load to the terminal or desktop on the root Partition, depending on what you have set up in raspi-config. Refer to the following diagram: NOOBS creates several partitions on the SD card to allow installation of multiple operating systems and to provide recovery NOOBS allows the user to optionally install multiple operating systems on the same card and provides a boot menu to select between them (with an option to set a default value in the event of a time-out period). You can also choose to create an optional data Partition that allows you to keep your datafiles separate to the operating system. This makes it easier to share files between multiple systems and allows you to keep backups of just your user data. If you later add, remove, or reinstall an operating system, ensure that you make a copy of any files, including system settings you wish to keep first as NOOBS may overwrite everything on the SD card. ### There's more… When you power up the Raspberry Pi for the first time, it will start directly into raspi-config (this only occurs for the first boot of a new install), which will allow you to perform changes to your SD card and set up your general preferences. Use the sudo raspi-config command to run it another time. When you exit this program, it will load directly by default to the terminal interface, which is the command line of the Raspberry Pi. To start a desktop session, such as Windows or OS X, use the startx command, which will load the Raspbian desktop. #### Changing the default user password Ensure that you change the default password for the pi user account once you have logged in as the default password is well known. This is particularly important if you connect to the public networks. You can do this with the passwd command, as shown in the following screenshot: Setting a new password for the pi user This gives greater confidence because if you later connect to another network, only you will be able to access your files and take control of your Raspberry Pi. #### Ensuring that you shut down safely To avoid any data corruption, you must ensure that you correctly shut down the Raspberry Pi by issuing a shutdown command as follows: sudo shutdown –h now  You must wait until this command completes before you remove power from the Raspberry Pi (wait for at least 10 seconds after the SD card access light has stopped flashing). You can also restart the system with the reboot command as follows: sudo reboot  #### Preparing an SD card manually An alternative to using NOOBS is to manually write the operating system image to the SD card. While this was originally the only way to install the operating system, some users still prefer it. It allows the SD cards to be prepared before they are used in the Raspberry Pi. It can also provide easier access to startup and configuration files (refer to the Networking directly to a laptop or computer recipe), and it leaves more space available for the user (unlike NOOBS, a RECOVERY partition isn't included). The default Raspbian image actually consists of two partitions, BOOT and SYSTEM, which will fit onto a 2 GB SD card (4 GB or more is recommended). You need a computer running Windows/Mac OS X/Linux (although it is possible to use another Raspberry Pi to write your card, be prepared for a very long wait). Download the latest version of the operating system you wish to use. For the purpose of this book, it is assumed you are using the latest version of Raspbian available at http://www.raspberrypi.org/downloads. Perform the following steps depending on the type of computer you plan to use to write to the SD card (the .img file you need is usually compressed, so before you start, you will need to extract the file). The following steps are for Windows: 1. Ensure that you have downloaded the Raspbian image, as previously detailed, and extracted it to a convenient folder to obtain a .img file. 2. Obtain the Win32DiskImager.exe file available at http://www.sourceforge.net/projects/win32diskimager. 3. Run Win32DiskImager.exe from your downloaded location. 4. Click on the folder icon and navigate to the location of the .img file and click on Save. 5. If you haven't already done so, insert your SD card into your card reader and plug it into your computer. 6. Select the Device drive letter that corresponds to your SD card from the small drop-down box. Ensure you double-check this is the correct device (as the program will overwrite whatever is on the device when you write the image). ### Note The drive letter may not be listed until you select a source image file. 7. Finally, click on the Write button and wait for the program to write the image to the SD card. 8. Once completed, you can exit the program. Your SD card is ready! Refer to the following screenshot: Manually write operating system images to the SD card using Disk Imager The following steps should work for the most common Linux distributions, such as Ubuntu and Debian: 1. Using your preferred web browser, download the Raspbian image and save it somewhere suitable. 2. Extract the file from the file manager or locate the folder in the terminal and unzip the .img file with the following command: unzip filename.zip  3. If you haven't already done so, insert your SD card into your card reader and plug it into your computer. 4. Use the df –h command and identify the sdX identifier for the SD card. Each partition will be displayed as sdX1, sdX2, and so on, where X will be a, b, c, d, and so on for the device ID. 5. Ensure that all the partitions on the SD card are unmounted using the umount /dev/sdXn command for each partition, where sdXn is the partition being unmounted. 6. Write the image file to the SD card with the following command: sudo dd if=filename.img of=/dev/sdX bs=4M  7. The process will take some time to write to the SD card, returning to the terminal prompt when complete. 8. Unmount the SD card before removing it from the computer using the following command: umount /dev/sdX1  The following steps should work for most of the versions of OS X: 1. Using your preferred web browser, download the Raspbian image and save it somewhere suitable. 2. Extract the file from the file manager or locate the folder in the terminal and unzip the .img file with the following command: unzip filename.zip  3. If you haven't already done so, insert your SD card into your card reader and plug it into your computer. 4. Use the diskutil list command and identify the disk# identifier for the SD card. Each partition will be displayed as disk#s1, disk#s2, and so on, where # will be 1, 2, 3, 4 and so on for the device ID. ### Note If rdisk# is listed, use this for faster writing (this is a raw path and skips data buffering). 5. Ensure that the SD card is unmounted using the unmountdisk /dev/diskX command, where diskX is the device being unmounted. 6. Write the image file to the SD card with following command: sudo dd if=filename.img of=/dev/diskX bs=1M  7. The process will take some time to write to the SD card, returning to the terminal prompt when complete. 8. Unmount the SD card before removing it from the computer using the following command: unmountdisk /dev/diskX  Refer to the following screenshot: The boot process of a manually installed OS image #### Expanding the system to fit in your SD card A manually written image will be of a fixed size (usually made to fit the smallest sized SD card possible). To make full use of the SD card, you will need to expand the system partition to fill the remainder of the SD card. This can be achieved using the raspi-config tool. Start the raspi-config tool with the following command: sudo raspi-config  Select the menu item, 1 Expand Filesystem Ensures that all of the SD card storage is available to the OS, as shown in the following screenshot:. The raspi-config menu #### Accessing the Data/RECOVERY/BOOT partition Windows and Mac OS X do not support the ext4 format, so when you read the SD card, only the File Allocation Table (FAT) partitions will be accessible. In addition, Windows only supports the first partition on an SD card, so if you've installed NOOBS, only the RECOVERY partition will be visible. If you've written your card manually, you will be able to access the BOOT partition. The data partition (if you installed one via NOOBS) and the root partition are in ext4 format and won't usually be visible on non-Linux systems. ### Note If you do need to read files from the SD card using Windows, a freeware program, Linux Reader (available at www.diskinternals.com/linux-reader) can provide a read-only access to all of the partitions on the SD card. Access the partitions from the Raspberry Pi. To view the currently mounted partitions, use df, as shown in the following screenshot: The result of the df command To access the BOOT partition from within Raspbian, use the following command: cd /boot/  To access the RECOVERY or data partition, we have to mount it, by performing the following steps: 1. Determine the name of the partition as the system refers to it by listing all the partitions, even the unmounted ones. The sudo fdisk -l command lists the partitions, as show in the following screenshot: The partition table of a NOOBS install of Raspbian and data partition  mmcblk0p1 (vfat) RECOVERY mmcblk0p2 (Extended partition) contains (root, data, BOOT) mmcblk0p3 (ext4) root mmcblk0p5 (ext4) data mmcblk0p6 (vfat) BOOT mmcblk0p7 (ext4) SETTINGS If you have installed additional operating systems on the same card, the partition identifiers shown in the preceding table will be different. 2. Create a folder and set it as the mount point for the partition as follows: • For the RECOVERY partition, use the following command: mkdir ~/recovery sudo mount –t vfat /dev/mmcblk0p1 ~/recovery  • For the data partition, use the following command: mkdir ~/userdata sudo mount –t ext4 /dev/mmcblk0p5 ~/userdata  To ensure they are mounted each time the system is started, perform the following steps: 1. Add the sudo mount commands to /etc/rc.local before exit 0. If you have a different username, you will need to change pi to match. sudo nano /etc/rc.local sudo mount -t vfat /dev/mmcbblk0p1 /home/pi/recovery sudo mount -t ext4 /dev/mmcbblk0p5 /home/pi/userdata  2. Save and exit by pressing Ctrl + X, Y, and Enter. ### Note Commands added to /etc/rc.local will be run for any user who logs onto the Raspberry Pi. If you only want the drive to be mounted for the current user, the commands can be added to .bash_profile instead. If you have install additional operating systems on the same card, the partition identifiers shown here will be different. #### Using the tools to backup your SD card in case of failure You can use Win32 Disk Imager to make a full backup image of your SD card by inserting your SD card into your reader, starting the program, and creating a filename to store the image in. Simply click on the Read button instead to read the image from the SD card and write it to a new image file. The dd command can similarly be used to back up the card as follows: • For Linux, replacing sdX with your device ID, use the following command: sudo dd if=/dev/sdX of=image.img.gz bs=1M  • For OS X, replacing diskX with your device ID, use the following command: sudo dd if=/dev/diskX of=image.img.gz bs=1M  • You can also use gzip and split to compress the contents of the card and split them into multiple files if required for easy archiving as follows: sudo dd if=/dev/sdX bs=1M | gzip –c | split –d –b 2000m – image.img.gz  • To restore the split image, use the following command: sudo cat image.img.gz* | gzip –dc | dd of=/dev/sdX bs= 1M  ## Networking and connecting your Raspberry Pi to the Internet via the LAN connector The simplest way to connect the Raspberry Pi to the Internet is using the built-in LAN connection on the Model B. If you are using a Model A Raspberry Pi, a USB-to-LAN adapter can be used (refer to the There's more… section of the Networking and connecting your Raspberry Pi to the Internet via a USB Wi-Fi dongle recipe for details on how to configure this). ### Getting ready You will need an access to a suitable wired network, which will be connected to the Internet and a standard network cable (Cat5e or a similar one with a RJ45 type connector for connecting to the Raspberry Pi). ### How to do it… Many networks connect and configure themselves automatically using Dynamic Host Configuration Protocol (DHCP), which is controlled by the router or switch. If this is the case, simply plug the network cable into a spare network port on your router or network switch (or wall network socket if applicable). Alternatively, if a DHCP server is not available, you shall have to configure the settings manually (refer to the There's more… section for details). You can confirm this is functioning successfully with the following steps: 1. Ensure that the three LEDs on the Raspberry Pi marked FDX, LNK, and 100, light up (the 100 LED may not light up if connected to a 10 Mbps device rather than the more common 100 Mbps device), and in some cases, start to flash.This will indicate that there is a physical connection to the router and the equipment is powered and functioning. 2. Test the link to your local network using the ping command. First, find out the IP address of another computer on the network (or the address of your router perhaps, often 192.168.0.1 or 192.168.1.254). Now, on the Raspberry Pi terminal, use the ping command (the parameter -c 4 is used to send just four messages; otherwise, press Ctrl + C to stop) to ping the IP address as follows: ping 192.168.1.254 -c 4  3. Test the link to the Internet (this will fail if you usually connect to the Internet though a proxy server) as follows: ping www.raspberrypi.org -c 4  4. Finally, you can test the link back to the Raspberry Pi by discovering the IP address using hostname -I on the Raspberry Pi. You can then use the ping command on another computer on the network to ensure it is accessible (using the Raspberry Pi's IP address in place of www.raspberrypi.org). The Windows version of the ping command will perform five pings and stop automatically and will not need the –c 4 option). If the above tests fail, you will need to check your connections and then confirm the correct configuration for your network. ### There's more… If you find yourself using your Raspberry Pi regularly on the network and if you need to find out the IP address, you may find it helpful to fix the IP address to a known value by manually setting the IP address. However, remember to switch it back to use DHCP when connecting on another network. Some routers will also have an option to set a Static IP DHCP address, so the same address is always given to the Raspberry Pi (how this is set will vary on the router itself). Knowing your Raspberry Pi's IP address when it is on the network is particularly useful if you intend to use one of the remote access solutions described later on, which avoids the need for a display. On some networks, you may be able to use the Raspberry Pi's hostname instead of its IP address (the default is raspberrypi), but not all networks will support this without additional software such as Bonjour (built in to OS X and available for Windows). ## Configuring your network manually If your network does not include a DHCP server or it is disabled (typically, these are built in to most modern ADSL/cable modems or routers), you may need to configure your network settings manually. ### Getting ready Before you start, you will need to determine the network settings for your network. You will need to find out the following information from your router's settings or another computer connected to the network: • IPv4 address: This address will need to be selected to be similar to other computers on the network (typically, the first three numbers should match, that is, 192.168.1.X if netmask is 255.255.255.0), but it should not already be used by another computer. However, avoid x.x.x.255 as the last address since this is reserved as a broadcast address. • Subnet mask: This number determines the range of addresses the computer will respond to (for a home network, it is typically 255.255.255.0, which allows up to 254 addresses). This is also sometimes referred to as the netmask. • Default gateway address: This address is usually your router's IP address, through which the computers connect to the Internet. • DNS servers: The DNS server (Domain Name Service) converts names into IP addresses by looking them up. Usually, they will already be configured on your router, in which case you can use your router's address. Alternatively, your Internet Service Provider (ISP) may provide some addresses, or you can use Google's public DNS servers at the addresses 8.8.8.8 and 8.8.4.4. These are also called nameservers in some systems. For Windows, you can obtain this information by connecting to the Internet and running the following command: ipconfig /all  Locate the active connection (usually called Local Area Connection 1 or similar if you are using a wired connection or if you are using Wi-Fi, it is called wireless network connection) and find the information required as follows: The ipconfig/all command shows useful information about your network settings For Linux and Mac OS X, you can obtain the required information with the following command (note that it is ifconfig rather than ipconfig): ifconfig  The DNS servers are called nameservers and are usually listed in the resolv.conf file. You can use the less command as follows to view its contents (press Q to quit when you have finished viewing it): less /etc/resolv.conf  ### How to do it… To set the network interface settings, edit /etc/network/interfaces using the following code: sudo nano /etc/network/interfaces  Now, perform the following steps: 1. We can add the details for our particular network, the IP address number we want to allocate to it, the netmask address of the network, and the gateway address as follows: iface eth0 inet static address 192.168.1.10 netmask 255.255.255.0 gateway 192.168.1.254  2. Save and exit by pressing Ctrl + X, Y, and Enter. 3. To set the nameservers for DNS, edit /etc/resolv.conf using the following code: sudo nano /etc/resolv.conf  4. Add the addresses for your DNS servers as follows: nameserver 8.8.8.8 nameserver 8.8.4.4  5. Save and exit by pressing Ctrl + X, Y, and Enter. ### There's more… You can configure the network settings by editing cmdline.txt in the BOOT partition and adding settings to the startup command line with ip. The ip option takes the following form: ip=client-ip:nfsserver-ip:gw-ip:netmask:hostname:device:autoconf  • The client-ip option is the IP address you want to allocate to the Raspberry Pi. • The gw-ip option will set the gateway server address if you need to set it manually. • The netmask option will directly set the netmask of the network. • The hostname option will allow you to change the default raspberrypi hostname. • The device option allows you to specify a default network device if more than one network device is present. • The autoconf option allows the automatic configuration to be switched on or off. ## Networking directly to a laptop or computer It is possible to connect the Raspberry Pi LAN port directly to a laptop or computer using a single network cable. This will create a local network link between the computers, allowing all the things you can do if connected to a normal network without the need for a hub or router, including the connection to the Internet, if Internet Connection Sharing (ICS) is used as follows: ICS allows the Raspberry Pi to connect to the Internet through another computer. However, some additional configuration is required for the computers in order to communicate across the link, as the Raspberry Pi does not automatically allocate its own IP address. We will use the ICS to share a connection from another network link, such as a built-in Wi-Fi on a laptop. Alternatively, we can use a direct network link (refer to the Direct network link section under the There's more… section) if the Internet is not required or if the computer only has a single network adapter. ### Note Although this setup should work for most of the computers, some setups are more difficult than the others. For additional information, see www.pihardware.com/guides/direct-network-connection. It is likely that this functionality will be included as a part of the NOOBS/Raspbian images eventually. ### Getting ready You will need the Raspberry Pi with power and a standard network cable. ### Note The Raspberry Pi Model B LAN chip includes Auto-MDIX (Automatic Medium-Dependent Interface Crossover). Removing the need to use a special crossover cable (a special network cable wired so the transmit lines connect to receive lines for direct network links), the chip will decide and change the setup as required automatically. It may also be helpful to have a keyboard and monitor available to perform additional testing, particularly, if this is the first time you have tried this. To ensure that you can restore your network settings back to their original values, you should check whether it has a fixed IP address or the network is configured automatically. To check the network settings on Windows 7 and Vista, perform the following steps: 1. Open Network and Sharing Center from the Control Panel and click on Change adapter settings on the left-hand side. 2. To check the network settings on Windows XP, open Network Connections from the Control Panel. 3. Find the item that relates to your wired network adapter (by default, this is usually called Local Area Connection as shown in the following screenshot): Locating your wired network connection 4. Right-click on its icon and click on Properties. A dialog-box will appear as shown in the following screenshot: Selecting the TCP/IP properties and checking the settings 5. Select the item called Internet Protocol (TCP/IP) or Internet Protocol Version 4 (TCP/IPv4) if there are two versions (the other is Version 6), and click on the Properties button. 6. You can confirm if your network is set using automatic settings or by using a specific IP address (if so, take note of this address and the remaining details as you may want to revert the settings at a later point). To check the network settings on Linux, perform the following steps: 1. Open up the Network Settings dialog box, and select Configure Interface. Refer to the following screenshot: Linux Network Settings dialog box 2. Ensure that if any settings are manually set, you take note of them so that you can restore them later if you want. To check the network settings on Mac OS X, perform the following steps: 1. Open System Preferences and click on Networks. You can then confirm if the IP address is allocated automatically (using DHCP) or not. 2. Ensure that if any settings are manually set you take note of them so you can restore them later if you want to. Refer to the following screenshot: OS X Network Settings dialog box If you just need to access or control the Raspberry Pi without an Internet connection, refer to the Direct network link section on the There's more…section. ### How to do it… First, we need to enable ICS on our network devices. In this case, we will be sharing the Internet, which is available on Wireless Network Connection through Local Area Connection to the Raspberry Pi. For Windows, perform the following steps: 1. Return to the list of network adapters, right-click on the connection that links to the Internet (in this case, the Wireless Network Connection option) and click on Properties as shown in the following screenshot: Locating your wired network connection 2. At the top of the window, select the second tab (in Windows XP, it is called Advanced; in Windows 7, it is called Sharing) as shown in the following screenshot: Selecting the TCP/IP properties and noting the allocated IP address 3. In the Internet Connection Sharing section, check the box for Allow other network users to connect through this computer's Internet connection and use the drop-down box to select the Home networking connection: option as Local Area Connection. Click on OK and confirm if you previously had a fixed IP address set for Local Area Connection. 4. Go back into the Properties dialog box of Local Area Connection and view the Internet Protocol settings as before. 5. Take note of the new IP address that has been allocated to the adapter by ICS. For Mac OS X to enable the ICS, perform the following steps: 1. Click on System Preferences and click on Sharing. 2. Click on Internet Sharing and select the connection from which we want to share the Internet (in this case, it will be the Wi-Fi AirPort). Then select the connection that we will connect the Raspberry Pi to (in this case, Ethernet). 3. Take note of the new IP address that has been allocated to the adapter by ICS. For Linux to enable the ICS, perform the following steps: 1. From the System menu, click on Preferences and then Network Connections. Select the connection we want to share (in this case, Wireless) and click on Edit or Configure. In the IPv4 Settings tab, change the Method option to Shared to other computers. 2. Take note of the new IP address that has been allocated to the adapter by ICS. The IP address of the network adapter will be the Gateway IP address to be used on the Raspberry Pi, and we must also provide the Raspberry Pi with an IP address that matches the IP address (except the last number). For instance, if the computer's wired connection now has 192.168.137.1, we can set the Raspberry Pi's IP address to 192.168.137.10 and set the Gateway IP to 192.168.137.1. Next, we will set the required settings on the Raspberry Pi. We need to edit the cmdline.txt file, which we can do directly on the Raspberry Pi or on the computer (this is useful if there isn't a monitor or keyboard attached to the Raspberry Pi). To edit directly on the Raspberry Pi, perform the following steps: 1. Use the following command to edit /boot/cmdline.txt: sudo nano /boot/cmdline.txt  2. When the changes have been made, save and exit by pressing Ctrl + X, Y, and Enter. To edit on another computer, perform the following steps: 1. Shut down the Raspberry Pi before removing the SD card. 2. Take the SD card out of the Raspberry Pi and insert into an SD card reader. ### Note If you have used NOOBS to set up your SD card, you will be unable to access the BOOT partition directly on a Windows computer. Windows is only able to access the first partition on the SD card (which in the case of NOOBS is the RECOVERY partition). Therefore, in order to edit the cmdline.txt file on a Windows computer, it is necessary to use a Linux Live CD or create a Linux Virtual Machine. If the SD card has been prepared manually (refer to the Preparing an SD card manually section), you will be able to access and edit it directly from Windows. Mac OS X should allow you to access both the BOOT and RECOVERY partitions. 3. Locate the SD card in the file manager, find the cmdline.txt file, and open it to edit (on Linux, it will be in the first partition; on Windows, it will be the only visible partition on the device). 4. Ensure that you safely eject the SD card, selecting the drive and ejecting or unmounting it when you have made the change. The file will contain something similar to the following command line (all on a single line): dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait  To set the Raspberry Pi's IP address (for example, 192.168.137.10) and the Gateway IP address (for example, 192.168.137.1) when it next powers up, we add the following command line to the end of the same line using ip= option: dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait ip=192.168.137.10::192.168.137.1  We are now ready to test the new connection as follows: 1. Connect the network cable to the Raspberry Pi and the computer's network port, and then power up the Raspberry Pi, ensuring that you have reinserted the SD card if you previously removed it. To reboot the Raspberry Pi if you edited the file on there, use sudo reboot to restart it. 2. Allow a minute or two for the Raspberry Pi to fully power up. We can now test the connection. 3. From the connected laptop or computer, test the connection by pinging with the IP address you have set for the Raspberry Pi, for example, 192.168.137.10, as shown in the following command (on Linux or OS X, add -c 4 to limit to four messages or press Ctrl + C to exit): ping 192.168.137.10  Hopefully, you will find you have a working connection and receive replies from the Raspberry Pi. If you have a keyboard and screen connected to the Raspberry Pi, you can perform the following steps: 1. You can ping the computer in return (for example, 192.168.137.1) from the Raspberry Pi terminal as follows: ping 192.168.137.1 -c 4  2. You can test the link to the Internet by using ping to connect to a well-known website as follows, assuming you do not access the Internet through a proxy server: ping www.raspberrypi.org -c 4  If all goes well, you will have full Internet available through your computer to the Raspberry Pi, allowing you to browse the web as well as update and install new software. If the connection fails, perform the following steps: 1. Repeat the process, ensuring that the first three sets of numbers match with the Raspberry Pi and the network adapter IP addresses. 2. You can also check that when the Raspberry Pi powers up, the correct IP address is being set using the following command: hostname -I  3. This should be the address that was set in the cmdline.txt file. 4. If not, check the file; it should not have odd characters or symbols and should contain only one line. This is particularly important if you edited the file using Windows, as Linux files use a slightly different format. ### Note If you end up using Windows to edit lots of files from the Raspberry Pi, it is worth installing an editor such as Notepad++ (http://notepad-plus-plus.org), which supports these differences. 5. Check your firewall settings to ensure it is not blocking internal network connections. Just remember that this address may be different on different networks and computers, so if you have problems, confirm that the IP address has not changed on the shared adapter. ### How it works… When we enable ICS on the primary computer, the operating system will automatically allocate a new IP address to the computer. Once we determine what the new IP address is, we can ensure that the Raspberry Pi is set to a compatible IP address and the primary computer IP address is used as the Gateway IP address. Finally, we check whether the computer can communicate over the direct network link to the Raspberry Pi, back the other way, and also through to the Internet. ### There's more… If you do not require the Internet on the Raspberry Pi, or your computer only has a single network adapter, we can still connect the computers together through a direct network link. The advantage of this method is that the same IP address should work on most of the computers as long as the adapter you connect to is set to automatic. Refer to the following diagram: Connecting and using the Raspberry Pi with just a network cable, a standard imaged SD card, and power #### Direct network link For a network link to work between two computers, they need to be using the same address range. The allowable address range is determined by the subnet mask (for example, 255.255.0.0 or 255.255.255.0 would mean all IP addresses should be the same except for the last two or just the last number in the IP address; otherwise, they will be filtered). To use a direct link without enabling ICS, check the IP settings of the adapter you are going to connect to and determine if it is automatically allocated or fixed to a specific IP address. Most PCs connected directly to another computer will allocate an IP address in the range 169.254.X.X (with a subnet mask of 255.255.0.0). For the Raspberry Pi to be able to communicate through the direct link, it needs to have a fixed IP address in the same address range 169.254.X.X. Again, we can set this in the cmdline.txt file (for example, 169.254.1.10) as follows: dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait ip=169.254.1.10  The 169.254.X.X address is particularly useful, since this setting should work on most computers where the adapter settings are set to automatic. If you use this often, you can make a copy of cmdline.txt and swap them over when you wish to change the settings. ### Note In the later releases of the Raspberry Pi distributions, it is expected that the Raspberry Pi will detect this situation and like other computers, automatically allocate itself an IP address in the 169.254.X.X range. This would avoid the need to change any settings, and the direct network link will be available automatically, assuming both sides are set to automatic DHCP settings. If set to a fixed IP address (for example, 192.168.1.50), simply pick one that matches except the last number and set the ip= setting command in cmdline.txt accordingly (for example, 192.168.1.10, as shown in the following command line). This time we don't need to set the Gateway IP address: dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait ip=192.168.1.10  ### See also If you don't have a keyboard or screen connected to the Raspberry Pi, you can use this network link to remotely access the Raspberry Pi just as you would on a normal network (just use the new IP address you have set for the connection). Refer to the Connecting remotely to the Raspberry Pi over the network using VNC and Connecting remotely to the Raspberry Pi over the network using SSH (and X11 Forwarding) recipes. ## Networking and connecting your Raspberry Pi to the Internet via a USB Wi-Fi dongle Many home networks provide a wireless network over Wi-Fi. By adding a USB Wi-Fi dongle to the Raspberry Pi's USB port, it can connect to and use the Wi-Fi network. ### Getting ready You shall need to obtain a suitable USB Wi-Fi dongle; and in some cases, you may require a powered USB hub (this will depend on the hardware version of the Raspberry Pi you have and the quality of your power supply). General suitability of USB Wi-Fi dongles will vary depending on the chipset that is used inside and the level of Linux support available. You may find that some USB Wi-Fi dongles will work without installing additional drivers (in which case you can jump to configuring it for the wireless network) A list of supported Wi-Fi adapters is available at http://elinux.org/RPi_VerifiedPeripherals#USB_WiFi_Adapters. You will need to ensure that your Wi-Fi adapter is also compatible with your intended network; for example, supporting the same types of signals 802.11bgn and the encryptions WEP, WPA, and WPA2 (although most networks are backward compatible). You will also need the following details of your network: • Service set identifier (SSID): This is the name of your Wi-Fi network and should be visible if you use the following command: sudo iwlist scan | grep SSID  • Encryption type and key: This value will be None, WEP, WPA, or WPA2, and the key will be the code you normally enter when you connect your phone or laptop to the wireless network (sometimes, it is printed on the router). You will require a working Internet connection (that is, wired Ethernet) in order to download the required drivers. Otherwise, you may be able to locate the required firmware files (they will be the .deb files), and copy them to the Raspberry Pi (that is, via a USB flash drive; the drive should be automatically mounted if you are running in the desktop mode). Copy the file to a suitable location and install it with the following command: sudo apt-get install firmware_file.deb  ### How to do it… This task has two stages; first, we would identify and install firmware for the Wi-Fi adapter, and then we would need to configure it for the wireless network. We will try to identify the chipset of your Wi-Fi adapter (the part that handles the connection); this may not match the actual manufacturer of the device. An approximate list of supported firmware can be found with the following command: sudo apt-cache search wireless firmware  This will produce results similar to the following output (disregarding any results without firmware in the package title): atmel-firmware - Firmware for Atmel at76c50x wireless networking chips. firmware-atheros - Binary firmware for Atheros wireless cards firmware-brcm80211 - Binary firmware for Broadcom 802.11 wireless cards firmware-ipw2x00 - Binary firmware for Intel Pro Wireless 2100, 2200 and 2915 firmware-iwlwifi - Binary firmware for Intel PRO/Wireless 3945 and 802.11n cards firmware-libertas - Binary firmware for Marvell Libertas 8xxx wireless cards firmware-ralink - Binary firmware for Ralink wireless cards firmware-realtek - Binary firmware for Realtek wired and wireless network adapters libertas-firmware - Firmware for Marvell's libertas wireless chip series (dummy package) zd1211-firmware - Firmware images for the zd1211rw wireless driver  To find out the chipset of your wireless adapter, plug the Wi-Fi-adapter into Raspberry Pi, and from the terminal, run the following command: dmesg | grep 'Product:\|Manufacturer:'  ### Note This command stitches together two commands into one. First, dmesg displays the message buffer of the kernel (this is an internal record of system events that have occurred since power on, such as detected USB devices). You can try the command on its own to observe the complete output. The | (pipe) sends the output to the grep command, grep 'Product:\|Manuf' checks it and only returns lines that contain Product or Manuf (so we should get a summary of any items, which are listed as Product and Manufacturer). If you don't find anything or want to see all your USB devices, try grep 'usb' instead. This should return something similar to the following output (in this case, I've got a ZyXEL device, which has a ZyDAS chipset (a quick Google search reveals zd1211-firmware is for ZyDAS devices): [ 1.893367] usb usb1: Product: DWC OTG Controller [ 1.900217] usb usb1: Manufacturer: Linux 3.6.11+ dwc_otg_hcd [ 3.348259] usb 1-1.2: Product: ZyXEL G-202 [ 3.355062] usb 1-1.2: Manufacturer: ZyDAS  Once you have identified your device and the correct firmware, you can install it, as you would for any other package available through apt-get (where zd1211-firmware can be replaced with your required firmware as) shown in the following command: sudo apt-get install zd1211-firmware  Remove and reinsert the USB Wi-Fi dongle to allow it to be detected and the drivers loaded. We can now test if the new adapter is correctly installed with ifconfig. The output is shown as follows: wlan0 IEEE 802.11bg ESSID:off/any Mode:Managed Access Point: Not-Associated Tx-Power=20 dBm Retry long limit:7 RTS thr:off Fragment thr:off Power Management:off  The command will show the network adapters present on the system. For Wi-Fi, this is usually as wlan0, or wlan1, or so on if you have installed more than one. If not, double-check the selected firmware; and perhaps, try an alternative or check on the site for troubleshooting tips. Once we have the firmware installed for the Wi-Fi adapter, we will need to configure it for the network we wish to connect. Perform the following steps: 1. We will need to add the wireless adapter to the list of network interfaces, which is set in /etc/network/interfaces as follows: sudo nano -c /etc/network/interfaces  Using the previous wlan# value, add the following command: auto wlan0 iface wlan0 inet dhcp wpa-conf /etc/wpa.conf  When the changes have been made, save and exit by pressing Ctrl + X, Y, and Enter. 2. We will now store the Wi-Fi network settings of our network in the wpa.conf file (don't worry if your network doesn't use the wpa encryption; it is just the default name for the file) as follows: sudo nano -c /etc/wpa.conf  The following information (that is, if the SSID is set as theSSID): • If no encryption is used, use the following code: network={ ssid="theSSID" key_mgmt=NONE } • With the WEP encryption (that is, if the WEP key is set as theWEPkey), use the following code: network={ ssid="theSSID" key_mgmt=NONE wep_key0="theWEPkey" } • Or for the WPA or WPA2 encryption (that is, if the WPA key is set as theWPAkey), use the following code: network={ ssid="theSSID" key_mgmt=WPA-PSK psk="theWPAkey" } 3. You can enable the adapter with the following command (again, replace wlan0 if required): sudo ifup wlan0  Use the following command to list the wireless network connections: iwconfig  You should see your wireless network connected with your SSID listed as follows: wlan0 IEEE 802.11bg ESSID:"theSSID" Mode:Managed Frequency:2.442 GHz Access Point: 00:24:BB:FF:FF:FF Bit Rate=48 Mb/s Tx-Power=20 dBm Retry long limit:7 RTS thr:off Fragment thr:off Power Management:off Link Quality=32/100 Signal level=32/100 Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0 Tx excessive retries:0 Invalid misc:15 Missed beacon:0  If not, adjust your settings and use sudo ifdown wlan0 to switch off the network interface, and then sudo ifup wlan0 to switch it back on. This will confirm that you have successfully connected to your Wi-Fi network. 4. Finally, we will need to check whether we have access to the Internet. Here, we have assumed that the network is automatically configured with DHCP and no proxy server is used. If not, refer to the Connecting to the Internet through a proxy server recipe. Unplug the wired network cable, if still connected, and see if we can ping the Raspberry Pi website as follows: ping www.raspberrypi.org  ### Tip If you want to quickly know the IP address currently in use by the Raspberry Pi, you can use hostname -I or to find out which adapter is connected to which IP address, use ifconfig. ### There's more… The Model A version of the Raspberry Pi does not have a built-in network port; so in order to get a network connection, a USB network adapter will have to be added (either a Wi-Fi dongle, as explained in the preceding section, or a LAN-to-USB adapter, as described in the following section. #### Using USB wired network adapters Just like the USB Wi-Fi, the adapter support will depend on the chipset used and the drivers available. Unless the device comes with the Linux drivers, you may have to search the Internet to obtain the suitable Debian Linux drivers. If you find a suitable .deb file, you can install it with the following command: sudo apt-get install firmware_file.deb  Ensure that you also check using ifconfig, as some devices will be supported automatically and will appear as eth1 (or eth0 on Model A), and be ready to use immediately. ## Connecting to the Internet through a proxy server Some networks such as workplaces or schools often require you to connect to the Internet through a proxy server. ### Getting ready You will need the address of the proxy server you are trying to connect to, including the username and password if one is required. You should confirm that the Raspberry Pi is already connected to the network and that you can access the proxy server. Use the ping command to check this as follows: ping proxy.address.com -c 4  If this fails (you get no responses), you will need to ensure your network settings are correct before continuing. ### How to do it… Create a new file using nano as follows (if there is content in the file already, you can add the code at the end): sudo nano -c ~/.bash_profile  To allow the basic web browsing through programs such as midori while using a proxy server, you can use the following script: function proxyenable { # Define proxy settings PROXY_ADDR="proxy.address.com:port" # Login name (leave blank if not required): LOGIN_USER="login_name" # Login Password (leave blank to prompt): LOGIN_PWD= #If login specified - check for password if [[ -z$LOGIN_USER ]]; then
PROXY_FULL=$PROXY_ADDR else #Login needed for proxy Prompt for password -s option hides input if [[ -z$LOGIN_PWD ]]; then
echo
fi
PROXY_FULL=$LOGIN_USER:$LOGIN_PWD@$PROXY_ADDR fi #Web Proxy Enable: http_proxy or HTTP_PROXY environment variables export http_proxy="http://$PROXY_FULL/"
export HTTP_PROXY=$http_proxy export https_proxy="https://$PROXY_FULL/"
export HTTPS_PROXY=$https_proxy export ftp_proxy="ftp://$PROXY_FULL/"
export FTP_PROXY=$ftp_proxy #Set proxy for apt-get sudo cat <<EOF | sudo tee /etc/apt/apt.conf.d/80proxy > /dev/null Acquire::http::proxy "http://$PROXY_FULL/";
Acquire::ftp::proxy "ftp://$PROXY_FULL/"; Acquire::https::proxy "https://$PROXY_FULL/";
EOF
#Remove info no longer needed from environment
echo Proxy Enabled
}

function proxydisable {
#Disable proxy values, apt-get and git settings
unset http_proxy HTTP_PROXY https_proxy HTTPS_PROXY
unset ftp_proxy FTP_PROXY
sudo rm /etc/apt/apt.conf.d/80proxy
echo Proxy Disabled
}

Once done, save and exit by pressing Ctrl + X, Y, and Enter.

### Note

The script is added to the user's own .bash_profile file, which is run when that particular user logs in. This will ensure that the proxy settings are kept separately for each user. If you want all users to use the same settings, you can add the code to /etc/rc.local instead (this file must have exit 0 at the end).

### How it works…

Many programs that make use of the Internet will check for the http_proxy or HTTP_PROXY environment variables before connecting. If they are present, they will use the proxy settings to connect through. Some programs may also use the HTTPS and FTP protocols, so we can set the proxy setting for them here too.

### Note

If a username is required for the proxy server, a password will be prompted for. It is generally not recommended to store your passwords inside scripts unless you are confident no one else will have access to your device (either physically or through the Internet).

The last part allows any programs that execute using the sudo command to use the proxy environment variables while acting as the super user (most programs will try accessing the network using normal privileges first, even if running as a super user, so it isn't always needed).

### There's more…

We also need to allow the proxy settings to be used by some programs, which use super user permissions while accessing the network (this will depend on the program; most don't need this). We need to add the commands into a file stored in /etc/sudoers.d/ by performing the following steps:

### Note

It is important to use visudo here, as it ensures the permissions of the file are created correctly for the sudoers directory (read only by the root user).

1. Use the following command to open a new sudoer file:

sudo visudo -f /etc/sudoers.d/proxy

2. Enter the following text in the file (on a single line):

Defaults env_keep += "http_proxy HTTP_PROXY https_proxy HTTPS_PROXY ftp_proxy FTP_PROXY"

3. Once done, save and exit by pressing Ctrl + X, Y, and Enter; don't change the proxy.tmp filename (this is normal for visudo; it will change it to proxy when finished).

4. If prompted What now?, there is an error in the command. Press X to exit without saving and retype the command!

5. After a reboot (using sudo reboot), you will be able to use the following commands to enable and disable the proxy respectively:

proxyenable
proxydisable


## Connecting remotely to the Raspberry Pi over the network using VNC

Often, it is preferable to remotely connect to and control the Raspberry Pi across the network. For instance, using a laptop or desktop computer as a screen and keyboard, or while the Raspberry Pi is connected elsewhere, perhaps even connected to some hardware to which it needs to be near.

VNC is just one way in which you can remotely connect to the Raspberry Pi. It will create a new desktop session that will be controlled and accessed remotely. The VNC session here is separate to the one that may be active on the Raspberry Pi's display.

Ensure that your Raspberry Pi is powered up and connected to the Internet. We will use the Internet connection to install a program using apt-get. This is a program that allows us to find and install applications directly from the official repositories.

### How to do it…

First, we need to install the TightVNC server on the Raspberry Pi with the following commands. It is advisable to run an update command first to get the latest version of the package you want to install as follows:

sudo apt-get update
sudo apt-get install tightvncserver


Accept the prompt to install and wait until it completes. To start a session, use the following command to start a session:

vncserver :1


The first time you run this, it will ask you to enter a password (of no more than eight characters) to access the desktop (you will use this when you connect from your computer).

The following message should confirm that a new desktop session has been started:

New 'X' desktop is raspberrypi:1


If you do not already know the IP address of the Raspberry Pi, use hostname –I and take note of it.

Next, we need to run a VNC client, VNC Viewer is suitable program, which is available at http://www.realvnc.com/ and should work on Windows, Linux, and OS X.

When you run VNC Viewer, you will be prompted for the Server address and Encryption type. Use the IP address of your Raspberry Pi with :1. That is, for the IP address 192.168.1.69, use the 192.168.1.69:1 address.

You can leave the Encryption type as Off or Automatic.

Depending on your network, you may be able to use the hostname; the default is raspberrypi, that is raspberrypi:1.

You may have a warning about not having connected to the computer before or having no encryption. You should enable encryption if you are using a public network or if you are performing connections over the Internet (to stop others from being able to intercept your data).

### There's more…

You can add options to the command line to specify the resolution and also the color depth of the display. The higher the resolution and color depth (can be adjusted to use 8 to 32 bits per pixel to provide low or high color detail), the more data has to be transferred through the network link. If you find the refresh rate a little slow, try reducing these numbers as follows:

vncserver :1 –geometry 1280x780 –depth 24


To allow the VNC server to start automatically when you switch on, you can add the vncserver command to .bash_profile (this is executed each time the Raspberry Pi starts).

Use the nano editor as follows (the -c option allows the line numbers to be displayed):

sudo nano -c ~/.bash_profile


Add the following line to the end of the file:

vncserver :1


The next time you power up, you should be able to remotely connect using VNC from another computer.

## Connecting remotely to the Raspberry Pi over the network using SSH (and X11 Forwarding)

An SSH (Secure SHell) is often the preferred method for making remote connections, as it allows only the terminal connections and typically requires fewer resources.

An extra feature of SSH is the ability to transfer the X11 data to an X Windows server running on your machine. This allows you to start programs that would normally run on the Raspberry Pi desktop and they will appear in their own Windows on the local computer as follows:

X Forwarding can be used to display applications, which are running on the Raspberry Pi, on a Windows computer.

If you are running the latest version of Raspbian, SSH and X11 Forwarding will be enabled by default (otherwise, double-check the settings explained in the How it works… section).

### How to do it…

Linux and OS X have built-in support for X11 Forwarding; but if you are using Windows, you will need to install and run the X Windows server on your computer.

Download and run xming from the Xming site (http://sourceforge.net/projects/xming/).

Install xming, following the installation steps, including the installation of PuTTY if you don't have it already. You can also download PuTTY separately from http://www.putty.org/.

Next, we need to ensure the SSH program we use has X11 enabled when we connect.

For Windows, we shall use PuTTY to connect to the Raspberry Pi.

In the PuTTY Configuration dialog box, navigate to Connection | SSH | X11 and tick in the checkbox for X11 Forwarding. If you leave the X display location option blank, it will assume the default Server 0:0 as follows (you can confirm the server number by moving your mouse over the Xming icon in the system tray when it is running):

Enabling X11 Forwarding within the PuTTY configuration

Enter the IP address of the Raspberry Pi in the Session settings (you may also find that you can use the Raspberry Pi's hostname here instead; the default hostname is raspberrypi).

Save the setting using a suitable name, RaspberryPi, and click on Open to connect to your Raspberry Pi.

You are likely to see a warning message pop up stating you haven't connected to the computer before (this allows you to check you have everything right before continuing).

Opening an SSH connection to the Raspberry Pi using PuTTY

For OS X or Linux, click on Terminal to open a connection to the Raspberry Pi.

To connect with the default pi username, with an IP address of 192.168.1.69, use the following command; the -X option enables X11 Forwarding:

ssh -X pi@192.168.1.69


All being well, you should be greeted with a prompt for your password (remember the default value for the pi user is raspberry).

Ensure that you have Xming running, bystarting the Xming program from your computer's Start menu. Then, in the terminal window, type a program that normally runs within the Raspberry Pi desktop, such as leafpad or scratch. Wait a little while and the program should appear on your computer's desktop (if you get an error, you have probably forgotten to start Xming, so run it and try again).

### How it works…

X Windows and X11 is what provides the method by which the Raspberry Pi (and many other Linux-based computers) can display and control graphical Windows as part of a desktop.

For X11 Forwarding to work over a network connection, we need both SSH and X11 Forwarding enabled on the Raspberry Pi. Perform the following steps:

1. To switch on (or off) SSH, you can access raspi config (just type sudo raspi-config from the terminal) and click on SSH within the Advanced Options menu, as shown in the following screenshot (SSH is often enabled by default for most of the distributions to help allow remote connections without needing a monitor to configure it):

2. Ensure that X11 Forwarding is enabled on the Raspberry Pi (again most of the distributions now have this enabled by default).

3. Use nano with the following command:

sudo nano /etc/ssh/sshd_config

4. Look for a line in the /etc/ssh/sshd_config file, which controls X11 Forwarding and ensure that it says yes (with no # sign before it) as follows:

X11Forwarding yes

5. Save if required, by pressing Ctrl + X, Y, and Enter and reboot (if you need to change it) as follows:

sudo reboot


### There's more…

SSH and X 11 Forwarding is a convenient way to control the Raspberry Pi remotely; we will explore some additional tips on how to use it effectively in the following sections.

#### Running multiple programs with X11 Forwarding

If you want to run an X program, but still be able to use the same terminal console for other stuff, you can run the command in the background with & as follows:

leafpad &


Just remember that the more programs you run, the slower everything will get. You can switch to the background program by typing fg, check for the background tasks with bg.

#### Running as a desktop with X11 Forwarding

You can even run a complete desktop session through X11, although it isn't particularly user-friendly and VNC will produce better results. To achieve this, you have to use lxsession instead of startx (in the way you would normally start the desktop from the terminal).

An alternative is to use lxpanel, which provides the program menu bar from which you can start and run programs from the menu as you would on the desktop.

#### Running PyGame and Tkinter with X11 Forwarding

You can get the following error (or similar) when running the PyGame or Tkinter scripts:

_tkinter.TclError: couldn't connect to display "localhost:10.0"


In this case, use the following command to fix the error:

sudo cp ~/.Xauthority ~root/


## Sharing the home folder of the Raspberry Pi with SMB

When you have the Raspberry Pi connected to your network, you can access the home folder by setting up file sharing; this makes it much easier to transfer files and provides a quick and easy way to back up your data. Server Message Block (SMB) is a protocol that is compatible with Windows file sharing, OS X, and Linux.

Ensure that you have the Raspberry Pi powered and running with a working connection to the Internet.

You will also need another computer on the same local network to test the new share.

### How to do it…

First, we need to install samba, a software that handles folder sharing in a format that is compatible with Windows sharing methods.

Ensure that you use update as follows to obtain the latest list of available packages:

sudo apt-get update
sudo apt-get install samba


The install will require around 20 MB of space and take a few minutes.

Once the install has completed, we can make a copy of the configuration file as follows to allow us to restore to defaults if needed:

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.backup
sudo nano /etc/samba/smb.conf


Scroll down and find the section named Authentication; change the # security = user line to security = user.

As described in the file, this setting ensures that you have to enter your username and password for the Raspberry Pi in order to access the files (this is important for shared networks).

Find the section called Share Definitions and [homes], and change the read only = yes line to the read only = no line.

This will allow us to view and also write files to the shared home folder. Once done, save and exit by pressing Ctrl + X, Y, and Enter.

### Note

If you have changed the default user from pi to something else, substitute it in the following instructions.

Now, we can add pi (the default user) to use samba as follows:

sudo pdbedit -a -u pi


Now, enter a password (you can use the same password as your login or select a different one, but avoid using the default Raspberry password, which would be very easy for someone to guess). Restart samba to use the new configuration file as follows:

sudo /etc/init.d/samba restart
[ ok ] Stopping Samba daemons: nmbd smbd.
[ ok ] Starting Samba daemons: nmbd smbd.


In order to test, you will need to know either the Raspberry Pi's hostname (the default hostname is raspberrypi) or its IP address. You can find both of these with the following command:

hostname


For the IP address, add -I as follows:

hostname –I


On another computer on the network, enter the \\raspberrypi\pi address in the explorer path.

Depending on your network, the computer should locate the Raspberry Pi on the network and prompt for a username and password. If it can't find the share using the hostname, you can use the IP address directly, where 192.168.1.69 should be changed to match the IP address, \\192.168.1.69\pi.

## Keeping the Raspberry Pi up to date

The Linux image used by the Raspberry Pi is often updated to include enhancements, fixes, and improvements to the system, as well as adding support for new hardware, or changes made to the latest board. Many of the packages that you install, can be updated too.

This is particularly important if you plan on using the same system image on another Raspberry Pi board (particularly, a newer one) as older images will lack support for any wiring changes or alternative RAM chips. New firmware should work on older Raspberry Pi boards, but older firmware may not be compatible with the latest hardware.

Fortunately, you need not reflash your SD card every time there is a new release, since you can update it instead.

You will need to be connected to the Internet in order to update your system. It is always advisable to make a backup of your image first (and at a minimum, take a copy of your important files).

You can check your current version of firmware with the uname -a command as follows:

Linux raspberrypi 3.1.9+ #168 PREEMPT Sat Jul 14 18:56:31 BST 2012 armv6l GNU/Linux


The GPU firmware can be checked using the /opt/vc/bin/vcgencmd version command as follows:

   Jul 14 2012 13:14:40
version 325444 (release)


This is important if you are using an older version of firmware (pre-November 2012) on a newer board since the Model B board was only 254 MB RAM. Upgrading allows the firmware to make use of the extra memory if available.

The free -h command will detail the RAM available to the main processor (the total RAM is split between the GPU and ARM cores) and will give the following output:

             total       used       free     shared    buffers     cached
Mem:          183M       125M        58M         0B       9.1M        96M
-/+ buffers/cache:        19M       164M
Swap:          99M         0B        99M


You can then recheck the preceding output following a reboot to confirm if they have been updated (although they may have already been the latest).

### How to do it…

Before running any upgrades or installing any packages, it is worth ensuring you have the latest list of packages in the repository. The update command gets the latest list of available software and versions:

sudo apt-get update


If you just want to obtain an upgrade of your current packages, upgrade will bring them all up to date:

sudo apt-get upgrade


To ensure that you are running the latest release of Raspbian, you can run dist-upgrade (be warned; this can take an hour or so, depending on the amount that needs to be upgraded). This will perform all the updates that upgrade will perform but will also remove redundant packages and clean up:

sudo apt-get dist-upgrade


Both methods will upgrade the software, including the firmware used at boot and startup (bootcode.bin and start.elf).