Hey Guys, welcome back to electronicsInnovation.com
In this video, we will see how to get this
raspberry pi to interact with Amazon web Services
and we will also cover how to push data into
the DynamoDB.
So without wasting time lets get started.
This video is sponsored by PCBway.com,
PCBWAY is one of the best PCB manufacturers
in the current industry.
PCBway is offering services like PCB prototype,
SMD Stencil, PCB assembly, Flexible PCBs & Advanced
PCBs.
At PCBway.com we can get 10 pieces of 2 layered
PCBs at just $5 with 24 hours build time.
The Best part of PCBway is the Instant quote
feature, just enter the PCB size, choose the
quantity, layers, and thickness.
That's it, we will get the instant quote.
place an order by clicking on saving to cart.
Requirements are Working Raspberry Pi Setup,
Active account on Amazon web servises.
6 Steps are involved in this Tutorial
1. Creating thing, certificate and attaching
a policy to the certificate
2. Installing Required SDKs, and Libraries.
such as "AWS IoT Python SDK" & Paho-MQTT library
3. Programming Part - Creating scripts for
Publishing, subscribing to a particular thing.
4. Execution of Scripts & Demonstration of
Data logging
5. Creating DynamoDB Rule & DynamoDB Table.
6. Execution of Scripts & Demonstration of
pushing data into the DynamoDB
1. Creating thing, certificate and attaching
a policy to the certificate
Setting up the AWS environment for the IoT
core devices is pretty simple.
Log in to the AWS Management Console. search
for IoT core in the Amazon Services search
bar, Tap on the IoT core that will redirect
to the AWS IoT page.
On AWS IoT page, choose Manage and then things
from the navigation pane, click on Register
thing button.
Choose Create a single thing. Enter a name
for your thing in the Name field, Choose Next.
 Then Create a certificate. This generates
an X.509 certificate and key pair.
Download certificate, public and private keys,
and root certificate authority (CA) files
as shown here. Then activate the certificate.
and Then Click on Done.
On the AWS IoT Page, choose Secure, and then
choose Policies from the navigation pane.
Click on create policy.
Enter a Name for the policy, For Action and
Resource ARN, enter *. To allow your device
to perform all AWS IoT actions on all AWS
IoT resources.
Under Effect, choose Allow, and then Create.
That's it, we have successfully created Policy,
Thing, Certificate,
To attach a policy to this certificate, Click
on these meatballs menu, choose to attach
policy, Check freshly created Policy, and
then attach it. That it this policy also attached
to the certificate.
The AWS Credentials are saved in the Downloads
folder.
2. Installing Required SDKs and Libraries.
such as "AWS IoT Python SDK" & Paho-MQTT library
2.1 Installing "AWS IoT Python SDK"
Open Terminal, Enter the following command
to clone the "AWS IoT Python SDK" from GitHub.
This Should create the following directory
in Filesystem.
Now Enter the following the command to install
the SDK, You can find all the commands on
the project page which is mentioned in the
description below.
That's it, AWS IoT Python SDK is successfully
installed.
2.2. Installing Paho-MQTT library
Before going to install PahoMQTT Library,
Chech python, and OpenSSL Versions. Then Install
the Paho-MQTT using the following command.
After that update.
That it 2nd step is also completed.
3. Programming Part - Creating scripts for
Publishing, subscribing to a particular thing.
Download these two python scripts from the
project page which is mentioned in the description.
Open ei_aws_publish script. This is used to
publish, mac id, random-number, and a random
string for every 5 seconds.
To make this code work with our AWS thing,
we need to provide these details.
First-one AWS host, You can get this from
AWS account, click on the thing that you have
recently created, Click on interact here you
will find it. Copy and past it over here.
Enter client id, make sure it should be unique
among your AWS things. you can use the same
for thing name. there will be no issue.
Provide the path of the AWS credentials as
shown here.
We will use only the private key here. and
will not use the public key.
Here is the code for generating a payload
message in JSON format with mac id, Random
number, and Random string.
These lines are responsible for getting mac
id, random number, and a random string. Then
we are converting this to JSON format using
this line. and then publishing this JSON data
to MQTT topic, Electronics Innovation.
Now, Go back to AWS IoT Core, choose test,
and subscribe to this topic. Come back and
save this script. Now this script is ready
to work with our AWS thing.
4. Execution of Scripts & Demonstration of
Data logging
Open the Terminal and Execute script as shown
here.
Yeah, Now it's connected to AWS and started
publishing JSON data to the MQTT topic Electronics
Innovation. Now we can see the same data on
the AWS, where we have subscribed to the topic
of electronics Innovation.
Let's add another subscriber from our Raspberrypi,
Open the ei_aws-subscribe.py script. This
script is used to subscribe to a particular
topic and receive the published data on that
topic.
To make this code work with your AWS thing,
we need to provide these details. We have
already done this part to ei_aws-publish.py
script, So simply copy from that script and
past it over here. Then save it.
Now open terminal, and Execute this script
as shown here. Yes, Now it is subscribed to
the topic Electronics Innovation and started
receiving the JSON data that we are publishing
from the another script.
That's it, This is how we can make our Raspberry
pi, to interact with AWS IoT Core.
Close all the connections, after the testing
is done, otherwise, you will end up charged
by Amazon.
5. Creating DynamoDB Rule & DynamoDB Table.
On AWS IoT page, choose Act and then Rules
from the navigation pane, click on Create
Rule.
Enter name and description for your rule,
Description is optional.
For Rule query statement, enter the following.
 
"select *" specifies that you want to send
the entire MQTT message that triggered the
rule & “timestamp() AS ts" specifies that
 “timestamp()” will be copied to  “ts”
 
"from 'ElectronicsInnovation'" tells the rules
engine to trigger this rule when an MQTT topic
is ElectronicsInnovation.
Then Click on Add action, choose to split
the message into multiple columns of a DynamoDb
table and then click on Configure action,
On configure action page click on create new
resource, It will open a new tab, click on
create a table
Enter a name for the DynamoDB table. In Partition
key, enter mac_Id it should be a string. Select
Add sort key, and then enter ts in the Sort
key field, it should be number.
then uncheck Use Default settings. again uncheck
Read capacity and Write capacity; Edit Read
capacity units and Write capacity units from
5 to 1. Then Create a table.
that's it DynamoDB Table is successfully created.
Come back to the configure action page, refresh
it. The newly created resources will appear
here. Choose that one, Click on create a role,
Give a name for the role, Then create it.
If the policy successfully attached, click
on add action.
The added action will appear here. Then Scroll
down and click on create rule. That's it,
we have successfully created DynammoDB Rule
& DynamoDB Table.
6. Execution of Scripts & Demonstration of
pushing data into the DynamoDB
On AWS IoT page, choose test and subscribe
to this topic "Electronics Innovation"
Open the Terminal and Execute the ei_aws-publish
script as shown here, On Successful connection,
you will receive data on AWS Testing Page.
Now go to the DynamoDB table and open items
tab.
On DynamoDB,You can see how JSON data is split
into multiple columns of a DynamoDb table.
mac id, random number, random strings are
aligned with the timestamp.
Even, If we close subscription from the testing
page we will receive the data into the Dynamo
Table. There is no dependency between these
two.
If we add another subscription from Raspberry,
it will remain to receive data without affecting
DynamoDB Data. So That's it for this tutorial.
We suceeded to make our Raspberry pi interact
with AWS & saved received data into the Dynamodb.
Thank you.
