Internet of Things with Python
上QQ阅读APP看书,第一时间看更新

Setting up the board to work with Python as the programming language

There is some work to be done in order to start working with Python as the main programming language to control this board. We need the following additional elements that aren't included in the board's box:

  • A microSD card of at least 4 GB with a maximum supported capacity of 32 GB. It is convenient to use a speed class 4 or a faster microSD card. Note that you will lose all the contents of the microSD card.
  • A microSD to SD memory card adapter. The adapter is usually included within a microSD card's package.
  • A computer with an SD memory card reader. Most modern laptops and desktop computers include SD mermory card readers. However, in case you don't have one, you can buy a USB SD memory card reader and plug it to a free USB port in your computer. SD memory card readers are in fact read/write devices, and therefore, we can use them to write to a microSD card via the microSD to SD memory card adapter.
  • An Ethernet cable.
  • An Ethernet switch or a WiFi router with a free Ethernet port. You will connect the Intel Galileo Gen 2 board to your LAN.

Tip

In case you do not have access to your LAN's switch, you will have to ask your network administrator for advice.

The next picture shows an 8 GB speed class 4 microSD card labeled SDC4/8GB (left) and a microSD to SD memory card adapter (right).

Setting up the board to work with Python as the programming language

We have to download the latest version of the Yocto Linux meta distribution boot image from the Intel IoT Development Kit Images Repository website. Open http://iotdk.intel.com/images/ in your Web browser and download the iot-devkit-latest-mmcblkp0.direct.bz2 compressed file with the boot image listed on the Web page. You can also download it by entering the full URL in your Web browser: http://iotdk.intel.com/images/iot-devkit-latest-mmcblkp0.direct.bz2.

Tip

We will use the devkit-latest-mmcblkp0.direct.bz2 file, last modified on July 2, 2015. Make sure that you don't download any version releases sooner than this date because there are many differences in the package names used in previous releases that aren't compatible with the instructions provided later on in this chapter.

Once you have downloaded the file, it is necessary to decompress the downloaded image file and write the extracted image to the microSD card. The procedure is different in Windows and Mac OS X.

In Windows, you can use 7-Zip to extract the contents from the downloaded .bz2 file. 7-Zip is a free and open source software that you can download from http://www.7-zip.org.

Once you extract the Yocto Linux meta distribution boot image iot-devkit-latest-mmcblkp0.direct from the .bz2 file, you have to write this image to the microSD card. Insert the microSD card into the microSD to SD memory card adapter and insert the adapter into the computer's SD memory card reader.

The Win32 Disk Imager tool is an image writer for Windows that allows us to write images to USB sticks or SD/CF cards. You can use this free software to write the image to the microSD card. You can download it from http://sourceforge.net/projects/win32diskimager/files/Archive. The installer for the latest version is the Win32DiskImager-0.9.5-install.exe file. Once you install the software, take into account that you must execute the application as an administrator in Windows. You can right-click on the application's icon and select Run as administrator.

Click on the icon on the right-hand side of the Image File textbox and change the files filter from Disk Images (*.img *.IMG) to *.* so that you can select the Yocto Linux boot image with a direct extension.

Select the drive letter that Windows assigned to the microSD card in the Device dropdown.

Tip

Make sure that you select the right drive letter because all the contents for the drive will be erased and overwritten with the boot image. If you select the incorrect drive letter, you will lose the contents of the entire drive.

Click on Write and then click on Yes in the confirm overwrite dialog box. Now, wait until the tool finishes writing the contents to the microSD card. The following screenshot shows the Win32 Disk Imager tool displaying the progress while it writes the image to the microSD card in Windows 10.

Setting up the board to work with Python as the programming language

It will take a few minutes until the tool finishes writing the image to the microSD card. Once the writing process has finished, the tool will display a Complete dialog box with a Write successful message. Click on OK to close the dialog box and close the Win32 Disk Imager window.

Eject the microSD card in Windows and then remove the SD memory card adapter from the SD card reader.

In Mac OS X and Linux, you can use bunzip2 to extract the contents from the downloaded bz2 file, diskutil to unmount the microSD card, and dd to write the image to the microSD card. It is also possible to open a Terminal and unzip the downloadeded bz2 file by running the following command in the folder in which you downloaded the file:

bunzip -c iot-devkit-latest-mmcblkp0.direct

Tip

You need to be very careful with the commands to avoid erasing a wrong device such as a partition of your hard drive.

It is also possible to unzip the downloaded bz2 file by double-clicking on it on Finder. However, we will be running more commands in the Terminal window, and therefore, it is easier to start unzipping the file with a command.

Once you extract the Yocto Linux boot image iot-devkit-latest-mmcblkp0.direct from the bz2 file, you have to write this image to the microSD card. Insert the microSD card into the microSD to SD memory card adapter and then insert the adapter into the computer's SD memory card reader. Launch the Disk Utility application and check the details for the media connected to the card reader. For example, in any MacBook laptop, you will find the info by clicking on APPLE SD Card Reader Media and then on the Info button. Check the name listed in Device name or BSD device node. We will use this name in a command that will write the boot image to the microSD card. The following picture shows the Disk Utility application and the information for a microSD card whose device name is disk2. We just need to add /dev/ as a prefix to the gathered device name, and therefore, in this sample case, the complete name is /dev/disk2.

Setting up the board to work with Python as the programming language

It is also possible to gather the information by running the diskutil command to list all the devices and find out the device name assigned to the microSD card. However, the information provided by this command is a bit difficult to read and the Disk Utility application makes it easy to understand which is the device name for the memory card reader. The following command lists all the devices:

diskutil list

The following is the sample output generated by this command. The highlighted lines show the device name for the microSD card: /dev/disk2.

/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *121.3 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:          Apple_CoreStorage Macintosh HD            120.5 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1 (internal, virtual):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                  Apple_HFS Macintosh HD           +120.1 GB   disk1
                                 Logical Volume on disk0s2
                                 4BADDDC3-442C-4E75-B8DC-82E38D8909AD
                                 Unencrypted
/dev/disk2 (internal, physical):
 #: TYPE NAME SIZE IDENTIFIER
 0: FDisk_partition_scheme *7.7 GB disk2
 1: Linux 53.5 MB disk2s1
 2: Linux 1.4 GB disk2s2

Tip

Make sure that you take note of the right device name because all the contents for the drive will be erased and overwritten with the boot image. If you specify a wrong device name, you will lose the contents of the entire drive.

Unmount the microSD card with the following command. You need to replace /dev/devicename with /dev/disk2 in case the device name you gathered was disk2. If not, replace it with the appropriate device name.

sudo diskutil unmountDisk /dev/devicename

The Terminal will ask for your password and will unmount the microSD card. Run the following dd command to write the image in the input file named iot-devkit-latest-mmcblkp0.direct to the microSD card in the device name you gathered in the previous step. You need to replace of=/dev/devicename with of=/dev/disk2 in case the device name you gathered was disk2. If not, replace it with the appropriate device name. The command doesn't include a device name so that you don't overwrite any of your disks by accident.

sudo dd if=iot-devkit-latest-mmcblkp0.direct of=/dev/devicename bs=8m

Then, it will take some time to write the image to the microSD card. Wait until the command finishes and the Terminal displays the prompt again. Notice that it usually takes a few minutes and there is no output with any progress indication until the write process finishes. You will see the following output after the command finishes:

169+1 records in
169+1 records out
1417675776 bytes transferred in 1175.097452 secs (1206433 bytes/sec)

Now, unmount the microSD card with the following command. You need to replace /dev/devicename with /dev/disk2 in case the device name you gathered was disk2. If not, replace it with the appropriate device name.

sudo diskutil unmountDisk /dev/devicename

Close the terminal window and then remove the SD memory card adapter from the SD card reader.

Now, we have a microSD card with a Yocto Linux distribution that includes Python 2.7.3 and many useful libraries and utilities. It is time to make the Intel Galileo Gen 2 board boot from the Yocto image written to the microSD card.

Make sure that the board is unplugged and place the microSD card with the Yocto image in the microSD card slot on the board, labeled SDIO. The following picture shows a microSD card inserted in the slot on the board.

Setting up the board to work with Python as the programming language

Then, connect the board to your LAN with the Ethernet cable and plug the board's power supply to turn on the board and start it up. You will notice that the rectangular onboard LED labeled SD indicates that there is activity with the microSD card. Wait for approximately 30 seconds to make sure that the board finishes the boot process. You will notice that the LED labeled SD stops blinking after the boot process finishes.