Tutorial: Getting Started with Amazon AWS IoT, MQTT Protocol, and Node JS.
Update: This article is part of a series. Check out the series: Part 2.
0 — Introduction
In this article, we will walk you through the steps involved in getting started with AWS IoT Core connecting the popular source — Raspberry Pi. It will guide you to set up an AWS IoT Core instance and write a simple IoT application that helps us to understand the basics of peer-to-peer communication using a Node Js program and MQTT protocol.
You will be writing a Node.js client app to post data from your Raspberry Pi.
1 — Message Queue Telemetry Transport (MQTT)
The Internet of Things is about connecting everything. From tiny sensors to devices that control large systems. You can connect anything to the Internet, but the problem is connecting all this using a standardized protocol. Many protocols have been tried and tested. Some of the most common include HTTP, TCP/IP, and HTTPS. However, these protocols are not optimized for efficiency. Also, the house of IoT is built on a foundation of low power and battery efficiency. This means that IoT devices need to have a small footprint.
2 — AWS IoT Core
AWS IoT Core is a managed cloud platform that allows connected devices to easily and securely interact with cloud applications and other devices. AWS IoT Coore can support billions of transactions per day from thousands of devices, streamlining communication between the growing number of internet-connected things (including sensors, actuators, embedded devices, edge servers, gateways, mobile devices, and wearables) and the cloud.
Steps:
- 2.1 Register new thing (device)
- 2.2 Create a certificate
- 2.3 Create and associate policies
- 2.4 Create Amazon Cognito
2.1 — Register New Thing (device)
The first step will be to register the IoT device on AWS IoT Core; in this case, it will be a Raspberry Pi 4.
A thing resource is a digital representation of a physical device or logical entity in AWS IoT Core.
In order to register a new IoT device go to the AWS IoT Core dashboard, access “Manage”, and click on “Things”.
Click “Create thing” which will ask how many devices you want to add. Chose “create a single thing” and click. “Next”.
Now we will be asked to name your new thing (my-raspberry-01
), so we choose a name for it.
Leave the other parameters with the default configuration and click on “Next”.
2.2 — Create a certificate
A device requires a certificate to connect to AWS IoT Core. Select Auto-generate a new certificate
. This will generate a certificate, a public key, and a private key using AWS IoT’s certificate authority.
Leave the policy creation for later, click on “create thing” and we will get the next screen.
Download your Device certificate
and key files
. The key files are unique to this certificate and can’t be downloaded after you leave this page. Download them now and save them in a secure place.
Download the Root CA certificates (Download the root CA certificate file that corresponds to the type of data endpoint and cipher suite you’re using. You can also download the root CA certificates later.)
Once you have downloaded all of your certificates and keys, click “Done”.
2.3 — Create and associate policies
What is a policy?
AWS IoT policies give things permission to access AWS IoT resources (like other things, MQTT topics, or thing shadows).
Create a policy:
We access the section secure->policies and click on “New policy”.
Chose a policy name my-policy
and set up the next statements:
Action: iot:*
Resource ARN: *
This policy will grant permission to the IoT things for all actions and for all our ARN.
Link certificate with policy
Next, attach the policy to the certificate created in step 2.
Access secure->certificates and then click the certificate.
Access the “Policies” section, click “Actions”, then click “Attach Policy”.
Select your policy and click “attach”.
Attach the certificate to the thing and activate
On the same screen, click “Things”, click the thing (my-raspberry-01), then click “Actions” to select “Activate”.
2.4 — Create Cognito
In this step, you will create a new identity pool, which will help you to connect your client application to the IoT Core service.
Access Amazon Cognito through https://console.aws.amazon.com/cognit and click on “Manage Identity Pools”. You will be redirected to the wizard to create a new identity pool.
Enter a name for the identity pool, set “Enable access to unauthenticated identities”, and click “Create Pool”.
Before you continue, you will need to copy the ARN name from the thing you created on AWS IoT. This can be found in the AWS IoT Core instance.
Add ARN to the pool*
Once you have copied the ARN, continue with the configuration of the pool on Cognito.
Access the “Role Summary” and click “View Policy Document”. update the Policy document. Click “Edit” and then add the ARN to the “Resource” array. Omit the name of the thing and click “Allow”.
3 — Create Node JS program
In this application, you will create an AWS IoT Core client using Node.js and will then send data to the AWS IoT server using the MQTT protocol.
The code will:
- use aws-iot-device-sdk to connect an MQTT client to the AWS IoT Core instance,
- mockup some dummy data for testing,
- send an MQTT message to the server every 3 seconds (this message will be tagged with a specific topic).
- and subscribe the MQTT client a handler fon and specific topic.
Initialize a new npm project running:
npm init
Install the package: aws-iot-device-sdk-js
npm install aws-iot-device-sdk
AWS Core Topics
MQTT topics identify AWS IoT messages. AWS IoT clients identify the messages they publish by giving them topic names. Clients identify the messages to which they want to subscribe (receive) by registering a topic filter with AWS IoT Core. The message broker uses topic names and topic filters to route messages from publishing clients to subscribing clients.
Application code:
Create your code file and start the application: node index.js
index.js:
Client configuration notes:
Access the host endpoint from the AWS IoT Core dashboard; click “Settings” and copy the custom endpoint.
Now, place the certificates you created earlier in the code folder on the Raspberry device.
4 — Test Communication (D2C and C2D)
AWS Core provides a monitor tool; access it by clicking MQTT test client.
Test D2D
First, test out the communication D2C (device-to-cloud). Then, click on “Subscribe to a topic”, input the “Topic filter” house/temperature/room1
, and click “Subscribe”.
With the script running, You will be sent to the cloud messages with a specific topic.
If everything is ok, you should be able to see the messaging sent from our Raspberry with the dummy telemetry data:
Test C2D
Now, test the communication C2D (cloud-to-device). Click ‘Publish to a topic’, input the ‘Topic name” core/broadcast
, and click “Publish”.
Now, access the console of your Node.js application.
If the code is running and the client is subscribed correctly to the topic, you should be able to log the data from the AWS IoT Core.
Wrapping up
At this point, you have set up an AWS IoT Core instance, created a Node.js program (running on a Raspberry Pi) that sends events to the cloud using an MQTT client, and tested out the messaging flow.
Thank you for your interest! Any feedback is welcome.
I work at Sytac.io; We are a consulting company in the Netherlands, we employ around ~100 developers across the country at A-grade companies like KLM, ING, ABN-AMRO, TMG, Ahold Delhaize, and KPMG. Together with the community, we run DevJam, check it out and subscribe if you want to read more stories like this one. Alternatively, look at our job offers if you are seeking a great job!