Preliminary requirements
To get started with AWS IoT, you need an AWS account. If you don’t have an AWS account, you can create one here.
Once you have created your account, you can log in and navigate to the AWS IoT Console.
Setting up the IoT Thing
1- Once you have clicked on the Get Started button, from the side menu, navigate to Manage > Things and you should see a screen as shown here:
2- Next, click on Register a thing and, on the next screen click on the Create a single thing button and you should see a screen as shown here:
3- Start filling in the form by naming the device. You can give your thing any name but remember to properly update the code (I have called my device VSENSOR
).
4- Next, click on Create a type, fill the form with name and description and then click on the Create thing type button.
5- At the bottom of the page, click on Next.
6- Now you must create a certificate for your thing in order to create a secure connection between the device and the AWS IoT Core. To do that, click on Create certificate (the recommended action) and you will see a screen like this:
7- Once the certificates are created, use the four Download links to save them. Remember to update the code with your keys. (My keys start with fc278d0e63
).
8- Click on Activate and once the activation is successful, click on the Attach a policy button and you will see the following screen:
9- Since you don’t create a policy yet, you will add it in the following steps. Now click on Register Thing.
10- To create the policy, from the side menu, navigate to Secure > Policies, click on Create a policy and fill in the form as demonstrated in the following image:
11- At the bottom of the page, click on Create.
12- Now, to attach this policy to the certificate, navigate to Secure > Certificates and select the Attach policy by the options menu available at the top-right of the certificate.
13- Next select the policy you have just created and click on Attach to complete the set-up.
Developing the Node.js Thing appPreliminary requirements
You must have Node.js installed. If Node.js is not installed, you can download it here.
App set-up
1- Once you have created your application folder, open the Terminal from that folder and run the following command to set up a new package:
> npm init -y
2- Next, to connect your devices to AWS IoT you must install AWS IoT Device SDK. To do so, execute the following command:
> npm install aws-iot-device-sdk --save
3- Create a new file index.js
and a new directory certs
in the project root directory. Next, move the four certificates you have downloaded into certs
.
4- Copy this inside index.js
and replace the values inside awsIot.device
with your own.
5- To run the app, open the Terminal from the application directory and run this command:
> node index.js
Connection with DynamoDBCreate a DynamoDB table
1- Navigate to AWS DynamoDB Console, click on Create table and next fill in the form with your table’s information as shown in the following screenshot:
2- Click on Create.
Create a DynamoDB rule
Now must create a DynamoDB rule that allows you to take information from an incoming MQTT message and write it to a DynamoDB table.
1- Open the AWS IoT Console and from the side menu, navigate to Act > Rules and you should see a screen as shown here:
2- Click on the Create a rule button and fill in the form with name and description.
3- Next, write the query as shown in the following screenshot:
You can give your topic any name but remember to properly update the code (I have called my topic sensor/values
).
4- Now, on the section Set one or more action click on Add action, then select Insert a message into a DynamoDB table, and then click on Configure action.
5- Choose your table from the drop-down menu and then fill in the form as showed in the following image (always remember to properly update the code with your values):
6- Click on Create Role, insert a name and click on the Create role button.
7- Next, click on Add action.
8- Finally, click on the Create rule button.
Developing the Node.js backend appPreliminary requirements
You must have Node.js installed. At this point of the tutorial you should have Node.js installed in your system, otherwise you can download it here.
App set-up
1- Once you have created your application directory, open the Terminal from that directory and run the following command to set up a new package:
> npm init -y
2- Next, to execute the queries on DynamoDB you must install AWS SDK executing the following command:
> npm install aws-sdk
3- Now, install the framework Express for web application features running this command:
> npm install express --save
4- Create a new file index.js
.
5- Copy this inside index.js
and replace the table's and columns' names inside latestValues
and lastHour
functions with your own.
6- To interact with DynamoDB on AWS you need to keep your AWS credentials data in a shared file. When the SDK for JavaScript loads, it automatically searches the shared credentials file, which is named "credentials".
- The shared credentials file on Linux, Unix, and macOS:
~/.aws/credentials
- The shared credentials file on Windows:
C:\Users\USER_NAME\.aws\credentials
The credentials file has this form:
[default]
aws_access_key_id=<YOUR_ACCESS_KEY_ID>
aws_secret_access_key=<YOUR_SECRET_ACCESS_KEY>
aws_session_token=<YOUR_SESSION_TOKEN>*
* The session token is only required when using temporary security credentials. Since I have used an AWS Educate Account, my credentials are temporary and have an expiration of 3 hours after which the REST calls between the backend and DynamoDB get refused and I need to manually modify the credentials file.
7- To run the application, open the Terminal from the application directory and run this command:
> node index.js
Developing the Angular frontend appPreliminary requirements
1- You must have Node.js installed. At this point of the tutorial you should have Node.js installed in your system, otherwise you can download it here.
2- You must have Angular CLI installed in your system. If Angular CLI is not installed, you can install it executing the following command from the Terminal:
> npm install -g @angular/cli
App set-up
1- To create a new project monitor-fe
open the Terminal and run this command:
> ng new monitor-fe
2- When asked if you would like to add Angular routing write y and press enter.
3- Next, using the arrows select CSS as stylesheet format and press enter.
4- Get inside monitor-fe
and run this command to install Bootstrap:
> npm install bootstrap
5- Open the project with a code editor and add the following line in styles.css:
@import "~bootstrap/dist/css/bootstrap.css"
6- Create the monitor component and service with this Angular CLI commands:
> ng g c monitor
> ng g s monitor
7- In the app directory create a new model
directory. Next, inside model
add the classes station.response.ts
station.model.ts
and stationSensor.model.ts.
8- Update all the classes like these.
9- To start the application, execute the following command:
> ng serve
10- To see the web app, open your browser on localhost:4200
.
Comments