Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

You're reading from   Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications) Learn to design and implement reliable Python applications on the Raspberry Pi using a range of external libraries, the Raspberry Pis GPIO port, and the camera module

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher
ISBN-13 9781783551590
Length 200 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Dan Nixon Dan Nixon
Author Profile Icon Dan Nixon
Dan Nixon
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Your First Steps with Python on the Pi FREE CHAPTER 2. Understanding Control Flow and Data Types 3. Working with Data Structures and I/O 4. Understanding Object-oriented Programming and Threading 5. Packaging Code with setuptools 6. Accessing the GPIO Pins 7. Using the Camera Module 8. Extracting Data from the Internet 9. Creating Command-line Interfaces 10. Debugging Applications with PDB and Log Files 11. Designing Your GUI with Qt Index

Installing and setting up Raspbian

The first thing we need to do is head to the Raspberry Pi downloads page at https://www.raspberrypi.org/downloads/ and download the latest version of Raspbian. This is a version of the Debian Linux distribution, specifically designed for the Raspberry Pi.

  1. On the downloads page select the Download ZIP option under RASPBIAN.
    Installing and setting up Raspbian
  2. Once the file has downloaded, extract the Zip archive using the default tool on your OS. You should now have a single file ending with the file extension .img.

Writing to the SD card

The next step is to write the just downloaded operating system image to the SD card so that it can be used with the Pi. The way this is done varies depending on the operating system you use on your main PC.

Windows

On Windows, we will use a tool called Win32 Disk Imager to write the OS image to the SD card. This tool can be downloaded from the SourceForge page at sourceforge.net/projects/win32diskimager.

  1. Once downloaded and installed, insert your SD card and open Win32 Disk Imager. You should see a window similar to the following screenshot:
    Windows

    The important thing to check is that a drive letter appears in the Device drop down list. If this does not happen then Win32 Disk Imager has failed to recognize your SD card. In such a case, try it in a different SD card reader. If it still does not work then it could indicate that the card has failed.

  2. Next, browse to select the .img file you had previously extracted from the downloaded Zip archive and click the Write button as shown in the following screenshot, after first making sure that the correct device is selected in the Device drop down list:
    Windows
  3. You will then see a confirmation dialog similar to the one shown in the next screenshot, asking you to confirm that the image and device are correct. Assuming they are, click on Yes.
    Windows
  4. Win32 Disk Imager will now write the image file to the SD card. This can take a few minutes. Once complete, you will see a confirmation dialog box as seen in the following screenshot:
    Windows

You now have Raspbian loaded on the SD card and can now move on to the Boot Pi for the first time. This will be covered in the following sections.

Linux and Mac

On Linux and Mac, the dd command line utility can be used to write the operating system image to the SD card.

  1. First, we need to determine the path to the storage device you want to write to. On Linux, the easiest way to do this is by using the udev management tool to monitor the udev logs. This is done by using the following command:
    udevadm monitor --udev
    
  2. Now insert the SD card and you should see a series of log messages printed to the console, similar to those shown in the following image. The last few should contain the paths to the partitions already on the drive (in my case, /dev/sdb1 and /dev/sdb2; from this we can deduce that the path to the SD card is /dev/sdb).
    Linux and Mac
  3. Next, we need to ensure that none of the existing partitions are mounted before we try to write to the SD card. This can be done by running the following command for every partition discovered using udevadm:
    umount PATH
    

    Here PATH is the path to the partition. This should give an output similar to the following image if the partition was not mounted; otherwise the command will exit without printing any output:

    Linux and Mac
  4. At this point, the SD card is ready to be written to. For this we will use the following command:
    sudo dd if=[path to .img] of=[path to SD]
    

    Here [path to .img] is the path to the .img file extracted from the Zip archive downloaded earlier and [path to SD] is the path to the SD card we just discovered.

    This process will take some time (up to 20 minutes) and is complete when the command exits and you see the next shell prompt as shown in the following screenshot. If the writing fails then an error message will be printed to the terminal.

    Linux and Mac

Booting the Pi for the first time

Now that you have an SD card with Raspbian installed on it, you are ready to boot the Pi for the first time and perform the first time configuration steps required to get the Pi up and running.

Note that to fully setup the Pi, you will need to have a way to connect it to the internet in order to install and update the software packages. This can either be wired (using an Ethernet cable) or wireless (using a USB WiFi adapter).

  1. Firstly, connect the mouse, keyboard, monitor, and either the WiFi adapter or the Ethernet cable to the Pi. Insert the SD card and connect a USB power source. You should see the red PWR LED (Light Emitting Diode) light up and shortly after that, the green ACT LED would start to blink.

    Note that the USB power source should be able to supply at least 1.5A to ensure reliable operation of the Pi. Usually, the USB chargers supplied with the tablets are a good choice of power supply.

  2. Once the Pi has booted, you will see the configuration utility as shown in the following screenshot. The first thing we need to do here is to expand the root partition on the SD card to fill the entire SD card. This ensures that we have the maximum space available once we start using the Pi. This is done by selecting the Expand Filesystem option at the top of the list and pressing Enter.
    Booting the Pi for the first time
  3. Once the filesystem has been modified, you will see a message similar to the one shown next. Press Enter to return to the main menu.
    Booting the Pi for the first time
  4. Next, we will change the password for the default Pi user. This is done by selecting the second option on the main menu, Change User Password, and pressing Enter.
  5. You will now see a message box similar to the one shown next, with instructions on entering a new password. Press Enter to continue.
    Booting the Pi for the first time
  6. You will now be required to enter a new password. Press Enter when finished. Once you have done this, you will be asked to enter the password again to confirm.
    Booting the Pi for the first time

    Note

    Note that when entering a password you will not see any characters appear on the screen.

  7. Now that the password has been changed, we need to set the default boot action to start LXDE, the desktop manager used on Raspbian. Select Enable Boot to Desktop/Scratch and press Enter.
    Booting the Pi for the first time
  8. Now select the second Desktop option and press Enter.
    Booting the Pi for the first time
  9. You may also wish to change the default locale using the Internationalisation Options menu option. By default, the Pi is configured for the UK.
  10. Once you are ready to reboot the Pi to apply all of the new settings. This is done by selecting the Finish option and pressing Enter.
  11. You will be asked for a confirmation that you want to reboot. Select Yes and press Enter.
    Booting the Pi for the first time
  12. If you are using Ethernet to connect to your network then you can skip this step. Otherwise, we will now setup the WiFi adapter and connect to a wireless network.
    1. Open wpa_gui by choosing the WiFi Configuration utility in the Preferences submenu from the main menu in Raspbian.
      Booting the Pi for the first time
    2. Click on Scan to search for wireless networks in range. When complete, you should see a list similar to the one in the following screenshot:
      Booting the Pi for the first time
    3. When the scan completes, double click on the WiFi network you wish to connect to and you will be shown a window similar to the one in the following image, with some of the details of the network filled in:
      Booting the Pi for the first time
    4. Here all that is usually needed to be done is to enter the WiFi password in the PSK field and click on Add.
    5. When done, the network should be selected in the Network drop down box automatically. Now click on Connect to connect to the network. Assuming all went well, you should see the Status of the connection show Connected, as shown in this next screenshot:
    Booting the Pi for the first time
  13. Now that we have an internet connection on the Pi, the final setup is to update the software packages already installed on the Pi. This can be done by opening a terminal, by clicking on the black monitor in the top left corner of the screen and typing the following commands:
    sudo apt-get update
    sudo apt-get upgrade
    

    Each of these commands will take a few minutes to execute. The first updates the list of the available packages and the second updates each of the installed packages to the latest version.

Now that we have Raspbian setup on the Pi, we can move on to having a look at some of the tools we can use to write and execute Python scripts on the Pi.

You have been reading a chapter from
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)
Published in: Sep 2015
Publisher:
ISBN-13: 9781783551590
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime