Connect a Raspberry Pi or other device
In this section, we'll configure a Raspberry Pi for use with AWS IoT. If you have another device that you'd like to connect, the instructions for the Raspberry Pi include references that can help you adapt these instructions to your device.
This normally takes about 20 minutes, but it can take longer if you have many system software upgrades to install.
In this tutorial, you'll:
Important
Adapting these instructions to other devices and operating systems can be challenging. You'll need to understand your device well enough to be able to interpret these instructions and apply them to your device.
If you encounter difficulties while configuring your device for AWS IoT, you might try one of the other device options as an alternative, such as Create a virtual device with Amazon EC2 or Use your Windows or Linux PC or Mac as an AWS IoT device.
Set up your device
The goal of this step is to collect what you'll need to configure your device so that it can start the operating system (OS), connect to the internet, and allow you to interact with it at a command line interface.
To complete this tutorial, you need the following:
-
An AWS account. If you don't have one, complete the steps described in Set up AWS account before you continue.
-
A Raspberry Pi 3 Model B
or more recent model. This might work on earlier versions of the Raspberry Pi, but they have not been tested. -
Raspberry Pi OS (32-bit)
or later. We recommend using the latest version of the Raspberry Pi OS. Earlier versions of the OS might work, but they have not been tested. To run this example, you do not need to install the desktop with the graphical user interface (GUI); however, if you're new to the Raspberry Pi and your Raspberry Pi hardware supports it, using the desktop with the GUI might be easier.
-
An Ethernet or WiFi connection.
-
Keyboard, mouse, monitor, cables, power supplies, and other hardware required by your device.
Important
Before you continue to the next step, your device must have its operating system installed, configured, and running. The device must be connected to the internet and you will need to be able to access the device by using its command line interface. Command line access can be through a directly-connected keyboard, mouse, and monitor, or by using an SSH terminal remote interface.
If you are running an operating system on your Raspberry Pi that has a graphical user interface (GUI), open a terminal window on the device and perform the following instructions in that window. Otherwise, if you are connecting to your device by using a remote terminal, such as PuTTY, open a remote terminal to your device and use that.
Install the required tools and libraries for the AWS IoT Device SDK
Before you install the AWS IoT Device SDK and sample code, make sure your system is up to date and has the required tools and libraries to install the SDKs.
-
Update the operating system and install required libraries
Before you install an AWS IoT Device SDK, run these commands in a terminal window on your device to update the operating system and install the required libraries.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install cmake
sudo apt-get install libssl-dev
-
Install Git
If your device's operating system doesn't come with Git installed, you must install it to install the AWS IoT Device SDK for JavaScript.
-
Test to see if Git is already installed by running this command.
git --version
-
If the previous command returns the Git version, Git is already installed and you can skip to Step 3.
-
If an error is displayed when you run the git command, install Git by running this command.
sudo apt-get install git
-
Test again to see if Git is installed by running this command.
git --version
-
If Git is installed, continue to the next section. If not, troubleshoot and correct the error before continuing. You need Git to install the AWS IoT Device SDK for JavaScript.
-
Install AWS IoT Device SDK
Install the AWS IoT Device SDK.
Install and run the sample app
In this section, you'll install and run the pubsub
sample app
found in the AWS IoT Device SDK. This app shows how your device uses the MQTT
library to publish and subscribe to MQTT messages. The sample app subscribes
to a topic, topic_1
, publishes 10 messages to that topic, and
displays the messages as they're received from the message broker.
Install the certificate files
The sample app requires the certificate files that authenticate the device to be installed on the device.
To install the device certificate files for the sample app
-
Create a
certs
subdirectory in yourhome
directory by running these commands.cd ~ mkdir certs
-
Into the
~/certs
directory, copy the private key, device certificate, and root CA certificate that you created earlier in Create AWS IoT resources.How you copy the certificate files to your device depends on the device and operating system and isn't described here. However, if your device supports a graphical user interface (GUI) and has a web browser, you can perform the procedure described in Create AWS IoT resources from your device's web browser to download the resulting files directly to your device.
The commands in the next section assume that your key and certificate files are stored on the device as shown in this table.
Certificate file names File
File path
Root CA certificate
~/certs/Amazon-root-CA-1.pem
Device certificate
~/certs/device.pem.crt
Private key
~/certs/private.pem.key
To run the sample app, you need the following information:
Parameter |
Where to find the value |
---|---|
your-iot-endpoint |
In the AWS IoT console On the Settings page in the AWS IoT menu. Your endpoint is displayed in the Device data endpoint section. |
The your-iot-endpoint
value has a format of:
,
for example,
endpoint_id
-ats.iot.region
.amazonaws.coma3qj468EXAMPLE-ats.iot.us-west-2.amazonaws.com
.
View messages from the sample app in the AWS IoT console
You can see the sample app's messages as they pass through the message broker by using the MQTT test client in the AWS IoT console.
To view the MQTT messages published by the sample app
-
Review View MQTT messages with the AWS IoT MQTT client. This helps you learn how to use the MQTT test client in the AWS IoT console to view MQTT messages as they pass through the message broker.
-
Open the MQTT test client in the AWS IoT console.
-
Subscribe to the topic, topic_1.
-
In your command line window, run the sample app again and watch the messages in the MQTT client in the AWS IoT console.