Connecting Raspberry Pi Zero with Amazon Web Services IoT — Part III: Installing the AWS IoT Device SDK and Running a Sample Application
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 the second part, I changed gears and worked through provisioning the Raspberry Pi Zero. In this 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 AWS IoT Device SDK
The AWS IoT Device SDK helps you to connect devices to AWS IoT. It provides features so that devices can seamlessly and securely work with the Device Gateway and Device Shadow provided by AWS IoT.
For a broader exploration of AWS IoT, see the first part of this tutorial.
Installing the AWS IoT Device SDK and Building a Sample Application
Installing the AWS IoT Device SDK and building a sample application consists of a few steps. One must:
- Upload Certificates and a key pair to the device.
- Download the AWS IoT SDK onto the device and install it.
- Configure the sample application with the needed AWS IoT variables.
- Compile and run the sample application.
I will discuss each of these steps, individually, below.
Upload the Certificates and Keys to Your Device
Use Secure Copy (scp
) to move the keys and certificates to your device:
scp privkey.pem pi@raspberrypi.local:/home/pi
scp pubkey.pem pi@raspberrypi.local:/home/pi
scp crt.crt pi@raspberrypi.local:/home/pi
scp ca.pem pi@raspberrypi.local:/home/pi
The default password for the default pi
user in Raspbian is raspberry
.
Download the AWS IoT Device SDK onto the Raspberry Pi
Open a Secure Shell (ssh
) with the Raspberry Pi:
ssh pi@raspberrypi.local
The default password for the default pi
user in Raspbian is raspberry
.
Next, in the SSH shell, download the AWS IoT Device SDK for C in a tarball (linux_mqtt_openssl-latest.tar). Save it in your deviceSDK directory.
wget https://s3.amazonaws.com/aws-iot-device-sdk-embedded-c/linux_mqtt_openssl-latest.tar
Move the tarbell into /opt/devicesdk
and extract it:
mkdir /opt/devicesdk
mv linux_mqtt_openssl-latest.tar /opt/devicesdk
cd /opt/devicesdk
tar -xvf linux_mqtt_openssl-latest.tar
Move the certificates and key pair into `/opt/devicesdk/certs`:
mv /home/pi/privkey.pem /opt/devicesdk
mv /home/pi/pubkey.pem /opt/devicesdk
mv /home/pi/crt.crt /opt/devicesdk
mv /home/pi/ca.pem /opt/devicesdk
Before you can use the AWS IoT Embedded C SDK, you must install the OpenSSL library on Raspberry Pi:
sudo apt-get install -y libssl-dev
Supply the AWS IoT Variables to the Application
Navigate to /opt/devicesdk/sample_apps/shadow_sample
and edit the aws_iot_config.h
, using an editor that is installed on Raspbian by default, such as nano
:
cd /opt/devicesdk/sample_apps/shadow_sample
nano aws_iot_config.h
Providing the following information:
#define AWS_IOT_MQTT_HOST “ENDPOINT.iot.us-east-1.amazonaws.com”
#define AWS_IOT_MQTT_PORT 8883
#define AWS_IOT_MQTT_CLIENT_ID “THING NAME”
#define AWS_IOT_MY_THING_NAME “THING NAME”
#define AWS_IOT_ROOT_CA_FILENAME “ca.pem”
#define AWS_IOT_CERTIFICATE_FILENAME “cert.crt”
#define AWS_IOT_PRIVATE_KEY_FILENAME “privkey.pem”
Supply the endpoint you gathered in the first part of this tutorial, when interacting with AWS IoT via the AWS CLI. If you need to get it again, use the AWS CLI:
aws iot describe_endpoint
The file names for AWS_IOT_ROOT_CA_FILENAME
, AWS_IOT_CERTIFICATE_FILENAME
, and AWS_IOT_PRIVATE_KEY_FILENAME
should not contain paths, but should be bare file names instead. The specific values to provide may be different from this example.
Save the file and exit the editor.
Compile and Run the Sample Application
Compile the example and run:
make -f Makefile
This will produce an executable called shadow_sample
, which you can run:
./shadow_sample
You should be able to log into the AWS management console, open the IoT service and see entries being written by your device. It should look something like this:
shadow_sample
executable.Congratulations! You have connected your device to AWS IoT!
Calls to Action
Now that you have a Raspberry Pi provisioned for AWS IoT and have the Pi set up to manipulate a device shadow, I encourage you to explore some of the other services that AWS has to offer that plug into AWS IoT, and to build out more sophisticated applications on the Pi.