Book Image

Getting Started with Python for the Internet of Things

By : Tim Cox, Steven Lawrence Fernandes, Sai Yamanoor, Srihari Yamanoor, Prof. Diwakar Vaish
Book Image

Getting Started with Python for the Internet of Things

By: Tim Cox, Steven Lawrence Fernandes, Sai Yamanoor, Srihari Yamanoor, Prof. Diwakar Vaish

Overview of this book

This Learning Path takes you on a journey in the world of robotics and teaches you all that you can achieve with Raspberry Pi and Python. It teaches you to harness the power of Python with the Raspberry Pi 3 and the Raspberry Pi zero to build superlative automation systems that can transform your business. You will learn to create text classifiers, predict sentiment in words, and develop applications with the Tkinter library. Things will get more interesting when you build a human face detection and recognition system and a home automation system in Python, where different appliances are controlled using the Raspberry Pi. With such diverse robotics projects, you'll grasp the basics of robotics and its functions, and understand the integration of robotics with the IoT environment. By the end of this Learning Path, you will have covered everything from configuring a robotic controller, to creating a self-driven robotic vehicle using Python. • Raspberry Pi 3 Cookbook for Python Programmers - Third Edition by Tim Cox, Dr. Steven Lawrence Fernandes • Python Programming with Raspberry Pi by Sai Yamanoor, Srihari Yamanoor • Python Robotics Projects by Prof. Diwakar Vaish
Table of Contents (37 chapters)
Title Page
Copyright and Credits
About Packt
Contributors
Preface
Index

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 starts 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 that provides options to install several of the available operating systems on to your SD card.

Getting ready

Since NOOBS creates a RECOVERY partition to keep the original installation images, an 8 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 macOS X, you can use the SD Association's formatter, as shown in the following screenshot (available at https://www.sdcard.org/downloads/formatter_4/):

Getting 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 just over 1 GB) contains Raspbian, the most popular Raspberry Pi operating system image 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 network connection on Raspberry Pi are 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 of 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.

  1. You can now put the card into your Raspberry Pi, connect it to a keyboard and display, and turn the power on. Refer to the Connecting to 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 (the offline version only includes Raspbian). There are many more distributions that are available, but only the selected ones are available directly through 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 you 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 20 to 40 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 Raspberry Pi will start to boot into Raspberry Pi Desktop.

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 Raspberry Pi powers up, it loads some special code contained within the GPU's internal memory (commonly referred to as binary blob by 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 desktop on the root partition. Refer to the following diagram:

NOOBS creates several partitions on the SD card to allow the 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 choose between them (with an option to set a default value in the event of a time-out period).

If you later add, remove, or re-install an operating system, ensure first that you make a copy of any files, including system settings you wish to keep, as NOOBS may overwrite everything on the SD card.

There's more...

When you power up Raspberry Pi for the first time directly, the desktop will be loaded. You can configure the system settings using the Raspberry Pi Configuration menu (under the Preferences menu on the Desktop or via the sudo raspi-config command). With this menu, you can make changes to your SD card or set up your general preferences:

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 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 provides 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 Raspberry Pi by issuing a shutdown command, as follows:

sudo shutdown -h now

Or, use this one:

sudo halt

You must wait until this command completes before you remove power from 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 Raspberry Pi. It can also provide easier access to startup and configuration files, 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 into 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 sometimes 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 an .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. Double-check that 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.

  1. Finally, click on the Write button and wait for the program to write the image to the SD card, as shown in the following screenshot:

Manually writing operating system images to the SD card, using Disk Imager

  1. Once completed, you can exit the program. Your SD card is ready.

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 in a suitable place.
  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
  1. If you haven't already done so, insert your SD card into your card reader and plug it into your computer.
  2. 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.
  3. Ensure that all the partitions on the SD card are unmounted using theumount /dev/sdXn command for each partition, where sdXn is the partition being unmounted.
  4. Write the image file to the SD card, with the following command:
sudo dd if=filename.img of=/dev/sdX bs=4M
  1. The process will take some time to write to the SD card, returning to the Terminal prompt when complete.
  2. 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
  1. If you haven't already done so, insert your SD card into your card reader and plug it into your computer.

 

  1. 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 uses a raw path and skips data buffering).

  1. Ensure that the SD card is unmounted using the unmountdisk /dev/diskX command, where diskX is the device being unmounted.
  2. Write the image file to the SD card, with the following command:
sudo dd if=filename.img of=/dev/diskX bs=1M
  1. The process will take some time to write to the SD card, returning to the Terminal prompt when complete.
  2. Unmount the SD card before removing it from the computer, using the following command:
unmountdisk /dev/diskX

Refer to the following diagram:

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 Raspberry Pi Configuration tool.

Select Expand Filesystem, as shown in the following screenshot:

Raspberry Pi Configuration tool

Accessing the RECOVERY/BOOT partition

Windows and macOS 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 read-only access to all of the partitions on the SD card.

Access the partitions from 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 shown in the following screenshot:

NOOBS installation and data partition

The following table shows the names of partitions and their meanings

Partition name

Meaning

mmcblk0p1

(VFAT) RECOVERY

mmcblk0p2

(Extended partition) contains (root, data, BOOT)

mmcblk0p5

(ext4) root

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.

  1. Create a folder and set it as the mount point for the partition; for the RECOVERYpartition, use the following command:
mkdir ~/recoverysudo mount -t vfat /dev/mmcblk0p1 ~/recovery

To ensure that 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.localsudo mount -t vfat /dev/mmcblk0p1 /home/pi/recovery
  1. 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 on to 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 to install additional operating systems on the same card, the partition identifiers shown here will be different.

Using the tools to back up 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.

To make a backup of your system, or to clone to another SD card using Raspberry Pi, use the SD Card Copier (available from the desktop menu via the Accessories | SD Card Copier).

Insert an SD card into a card reader into a spare USB port of Raspberry Pi and select the new storage device, as shown in the following screenshot:

SD Card Copier program

Before continuing, the SD Card Copier will confirm that you wish to format and overwrite the target device and, if there is sufficient space, make a clone of your system.

The dd command can similarly be used to back up the card, as follows:

  • For Linux, replacing sdX with your device ID, use this 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