Connecting Raspberry Pi Zero with Amazon Web Services IoT — Part II: Provisioning a Raspberry Pi

goMake
7 min readAug 3, 2017

--

This post is by Morris Singer, who provides cloud infrastructure architecture expertise to GoMake.

This tutorial is part of a three-part series on using a Raspberry Pi Zero with Amazon Web Services (AWS) IoT. In the first part, I walked through setting up Amazon IoT to communicate with the Pi. In this part, I will change gears and work through provisioning the Raspberry Pi Zero. In the final part, I will walk through installing the AWS IoT Device SDK and running one of the sample application on the Pi that will demonstrate communication with AWS.

Review: What is the Internet of Things

The Internet of Things (IoT) represents the growth of the Internet into a platform for providing connectivity between electronics, software, sensors, and actuators embedded into physical devices. IoT allows the physical world to be more directly integrated into the Internet, providing opportunities to improve efficiency, accuracy and control of our physical world via existing networks.

For a broader exploration of how IoT systems are structured, see the first part of this tutorial.

What is the Raspberry Pi

The Raspberry Pi is a series of small, single-board computers developed by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries. The original model became far more popular than anticipated, and the computers are now seen as credible offerings for use in IoT systems.

The Raspberry Pi runs special distributions of Linux that are ported to run on the ARM processors that Raspberry Pi products rely on for processing. They include a 40-pin general input/output (GPIO) header block for connecting the Pi with sensors, actuators, displays, and other peripherals that facilitate the integration of the Pi with physical devices.

Flash Raspbian Jessie Onto the SD Card

One of the most popular distributions of Linux that runs on the Raspberry Pi is Raspbian, though the Pi is capable of running many other distributions of Linux as well, such as CentOS, Fedora, Ubuntu, Arch, Android, SUSE, and others. It is even capable of running a special version of Windows 10 geared toward IoT devices.

In this tutorial, I will walk through setting up the Pi with Raspbian. If you have a Raspberry Pi already set up with Raspbian, you can skip this step.

If you have a blank SD card, you will need to flash an image of Raspbian Linux onto the SD card that you put into the Raspberry Pi.

To do this, you will need a few pieces of equipment. Namely, you’ll need a MicroSD card (with at least 8 GB), an SD card reader/writer, and (possibly) a MicroSD card adapter. If you use a MacBook Pro, like I do, the card reader will be built into the laptop, but will require a MicroSD adapter.

Download the Raspbian Image

The first thing to do is to download the Raspbian image of your choice. At the time of this writing, you can choose between Raspbian Jessie and Raspbian Jessie Lite. Both images are available for download here.

I’ve generally preferred the full image rather than the Lite one, because it makes development and prototyping easier. In a final revision of your work, however, you may find that Lite gives you everything you need or that you like the idea of installing additional software packages piecemeal to get a highly tailored distribution for your needs.

After you download, it is very important for the security of your system that you check the SHA to ensure that you have a correct and secure version of the image. For this, I use the shasum tool to generate my own hash of the zip file, and compare it with the hash provided on raspberrypi.org using the diff tool:

diff <(shasum raspbian-jessie.zip | awk ‘{print $1}’) <(echo “e1edd4d26090b3e67a66939fa77eeb656de8a2c5”) && echo “OK”

If you have a secure image, the result should be:

OK

Anything else indicates that you should download again and try again.

Extract the image from the zip file:

unzip raspbian-jessie.zip

Identify the Correct Device

Next, place the MicroSD card into the adapter, and ensure that any switch on the side of the adapter that controls whether the SD card is writable is flipped to the position that allows you to write to it.

Open a terminal and determine which device is the MicroSD cad:

diskutil list

You’ll see a bunch of information on physical and logical volumes that are included in your environment. One entry will correspond directly to the SD card. it may be a little tricky to figure it out, depending on your setup. In my case, I got the following output (which is slightly redacted):

/dev/disk0 (internal, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *500.3 GB disk0
1: EFI EFI 209.7 MB disk0s1
2: Apple_CoreStorage Macintosh HD 499.4 GB disk0s2
3: Apple_Boot Recovery HD 650.0 MB disk0s3
/dev/disk1 (internal, virtual):
#: TYPE NAME SIZE IDENTIFIER
0: Macintosh HD +499.0 GB disk1
Logical Volume on disk0s2
/dev/disk3 (internal, physical):
#: TYPE NAME SIZE IDENTIFIER
0: FDisk_partition_scheme *7.9 GB disk3
1: Windows_FAT_32 boot 43.7 MB disk3s1
2: Linux 7.9 GB disk3s2

Here, you can see that disk0 is my main SSD, and that disk1 is a logical volume on a partition in disk0. You can also see disk3, which is clearly different from the first two, and, not coincidentally, has the same capacity as my MicroSD card. So, you know that disk3 is the one to flash.

N.B.: Be very careful here. If you flash the wrong disk, you could destroy the data on that disk, and if this is your system disk, the result will not be pretty.

Flashing the Firmware

Once you have identified the correct disk to flash (in this case disk3), unmount it:

diskutil unmountDisk /dev/disk3

If this works, you will see the following message:

Unmount of all volumes on disk3 was successful

Next, you’ll need to run the Data Definition (dd) tool to copy the image onto the SD card:

sudo dd bs=1m if=IMAGE-FILE of=DISK conv=sync

N.B.: Be very sure to specify the correct disk, which you identified earlier. Specifying the incorrect disk here could result in destruction of your system, including its data.

The process can take a while to run, given that it is transferring potentially over 4GB (in the case of the full Raspbian Jessie). To check in on the progress, send a SIGINFO by pressing CTRL-T, as often as you’d like.

Setup Pi Zero for USB On-The-Go (OTG) and Ethernet Gadget

Once this is completed, there are a few steps to set up the Pi for your convenience during development. For example, while the Pi makes it perfectly possible to connect an HDMI display and USB keyboard, you may find it easier to use the Pi over SSH from your development laptop. In this approach, no USB keyboard or HDMI monitor is needed.

For this method, alongside your Pi Zero, MicroUSB cable and MicroSD card, only an additional computer is required, which can be running Windows (with Bonjour), OS X or Linux (with Avahi Daemon installed).

USB OTG allows the Pi to function as a USB host rather than as a gadget, which, in combination with Ethernet Gadget, is what allows you to connect with it via SSH over USB. For more information on this, the following resources may be illuminating:

Enable OTG

You can enable OTG by using the DWC2 USB driver. You will need to add the DWC2 device tree overlay to the Raspbian configuration. To do this, locate the config.txt file on the boot partition of the SD drive and add dtoverlay=dwc2 to the bottom of the file:

echo “dtoverlay=dwc2” >>> config.txt

For more information on device tree overlays, see Device Trees, Overlays, and Parameters.

Finally, you will need to pass an additional argument to the Linux kernel in order to actually load the dwc2 module (for OTG) and g_ether module (for Ethernet Gadget). You can do this by adding those arguments in the cmdline.txt file. For more information on this file, see RPi cmdline.txt — elinux.org

Open up the cmdline.txt. Insert modules-load=dwc2,g_ether after rootwait.

For full clarity, an example. Here is the before:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=bcc21716–02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

And here is the after:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=bcc21716–02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_ether quiet splash plymouth.ignore-serial-consoles

Enable SSH

If using a recent release of Jessie (Dec 2016 onwards), then create a new file simply called ssh in root of the boot partition of your SD card. By default SSH is now disabled so this is required to enable it.

touch ssh

Get the Pi on Your WiFi

At this point, you can eject the SD card (after unmounting it), install it into your Raspberry Pi, and connect a USB Micro cable between your development laptop and the Pi. Give the Pi about 90 seconds to boot, after which you can SSH into it:

ssh pi@raspberrypi.local

The default password in a Raspbian distro for the pi user is raspberry.

Now, you will need to make some modifications to your /etc/network/interfaces file in order to provide information on the WiFi access point and password so that the Pi can connect to Wifi.

The interfaces file defines how the Pi is networked. You should put the eth0 interface into Dynamic Host Configuration Protocol (DHCP) so that your router can provide it with an IP address (unless your needs are different), and ensure that the wlan0 interface includes information on your WiFi access point and password.

To edit the interfaces file, execute the following command:

sudo nano /etc/network/interfaces

Ensure that the file ends up looking as follows:

auto loiface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-ssid “YOUR SSID HERE”
wpa-psk “YOUR PASSWORD HERE”

This should work for most people, but your specific needs may be different. I recommend creating a backup of your interfaces file before you modify the interfaces file.

Next Steps

In the next section, I will go through how to get the AWS IoT Device SDK onto the Pi and how to build one of the sample applications so that you can see your Pi communicate with AWS IoT.

--

--

goMake

Through project-based learning, goMake empowers students to build, launch, recover, and analyze data from high-altitude balloons.