In this hands-on guide, we will install Node.js on a Raspberry Pi 3 Model B+ and then connect our device to the AWS IoT Core service. This service connects to the Amazon Web Services (AWS) platform by using the MQTT publish-subscribe-based messaging protocol and lets your device communicate with other services on the platform.
The physical environment for this guide consists of a headless Raspberry Pi 3 Model B+ and a computer. If you need to configure your Raspberry Pi, see Setting up your Raspberry Pi before proceeding.
Since we will need to access our headless Raspberry Pi 3 Model B+ remotely, let’s install the following applications on our computer:
We will also need an AWS account. You can create an account on the AWS registration page.
Installing Node.js and npm on the Raspberry Pi
Let’s install the Node.js package, which includes npm, on our Raspberry Pi. If you have already installed these two components, you can skip to Connecting Our Raspberry Pi to AWS IoT Core.
In a web browser, let’s visit the Node.js download page. Our Raspberry Pi 3 Model B+ is running armv71, so let’s right-click ARMv7, and then copy the link location.
We need to access our Raspberry Pi via a command terminal, so let’s open PuTTY. In the PuTTY Configuration window:
- Enter the IP address of our Raspberry Pi in the Host Name (or IP address)
- Enter 22 in the Port
- Select SSH as the Connection type, and then click Open.
When prompted, we need to log in with a valid username and password. Once we’ve logged in, we can download the Node.js package to our user directory:
wget <link location>
The Node.js download page provided us with an archive file, so we need to extract it to our user directory:
tar -xf node-v10.15.3-linux-armv7l.tar.xz
Once the contents have been extracted, let’s enter the Node.js directory and copy the contents to /usr/local:
sudo cp -R * /usr/local/
Before proceeding, we should confirm that Node.js and npm are installed properly:
Now let’s return to our user directory and use npm to install the AWS IoT Device SDK for Node.js:
npm install aws-iot-device-sdk
If we have UFW configured as our firewall, we need to allow TCP port 8883. Opening this port will allow the Raspberry Pi to communicate with AWS IoT while behind the firewall.
sudo ufw allow 8883
We have now installed Node.js and npm and configured the firewall to allow incoming and outgoing connections.
Connecting Our Raspberry Pi to AWS IoT Core
Now, let’s add our Raspberry Pi to AWS IoT Core. To do this, we need to access the AWS Management Console, transfer the connection kit, and access our Raspberry Pi.
Assuming we signed up for an AWS account before reaching this step, let’s sign in to the AWS Management Console. In the Find Services field, type IoT Core and then press Enter.
In the sidebar menu, let’s select Onboard and then Get started. Once we select Get started again on the next page, the How are you connecting to AWS IoT? page will provide us with some platform and SDK options. Since our Raspberry Pi is based on the Linux kernel, we need to select Linux/OSX as the platform. And since the purpose of this guide is to use Node.js, let’s select Node.js as the AWS IoT Device SDK. After selecting these two options, click Next to proceed.
Now we need to name our Raspberry Pi so that we can identify it within the AWS IoT Core dashboard. On the Register a thing page, we need to enter a name for our Raspberry Pi in the Name field. Then, select Next step.
On the Download a connection kit page, we can download the connection kit to our computer. Normally, we would right-click a button and copy the link into PuTTY to download the file directly to our Raspberry Pi. But in this case, AWS IoT Core will not create the contents of the download kit until we select Linux/OSX. Let’s select Linux/OSX and save the connection kit to our Downloads folder.
Now we need to transfer the connection kit to our Raspberry Pi. Let’s open FileZilla and connect to our Raspberry Pi. In the Local site pane, we should navigate to the Downloads folder on our computer. Then, in the Remote site pane, we need to transfer connect_device_package.zip to our user directory (for example, /home/pi) on our Raspberry Pi.
We need to extract the contents of this .zip file, so let’s open PuTTY. As described on the Configure and test your device page, run:
Running this command will extract a certificate, private key, public key, and start script to our user directory. We need to give execution permissions to the start script, so let’s run:
chmod +x start.sh
Now it’s time to test our connection. To establish a connection between our Raspberry Pi and AWS IoT Core, let’s run:
When our Raspberry Pi connects with AWS IoT Core, the following message appears:
Waiting for messages from your device
To close the connection, we need to select Ctrl+C.
We are now finished with configuring and testing our device. Let’s click Done to return to the AWS IoT Core dashboard.
When the confirmation page appears, let’s click Done. On the Monitor page, we see on the Successful connections graph that our Raspberry Pi successfully connected to AWS IoT Core.
We can also test the connection between our Raspberry Pi and AWS IoT Core from the dashboard. By selecting Test in the sidebar menu, AWS IoT Core will send a connection request by using the MQTT protocol to our Raspberry Pi. We can perform this test connection to confirm whether our Raspberry Pi can subscribe and publish messages.
To end the test connection, let’s select Connected as iotconsole-xxxxxxxxxxxxx-x and then choose Disconnect.
We now have a Raspberry Pi running Node.js that can connect to AWS IoT Core by using the MQTT protocol. From the AWS IoT Core dashboard, we can monitor and test the connection to our Raspberry Pi, view message data, and see other details.
In the future, we could integrate our Raspberry Pi with AWS Lambda and the Alexa Skills Kit to control our Raspberry Pi by using voice commands. A system like this could improve efficiencies in the organization and help offload resource-intensive tasks to AWS, thereby reducing carbon footprint and costs.