This article introduces how to create a virtual Lamp and interact with AWS Iot Core.
Let’s get your account setup with a new Thing, certificates and security policies.
Log into your AWS console and make sure you can access the AWS IoT dashboard. It should like
the following. Let’s create our first “thing” and setup the policy and certificates for this to work.
In the above screenshots, you’ll see a Onboard menu option. Select this to continue
Under Configuring a device, click the Getting Started button
On How are you connecting to AWS IoT? page, select the platform; choose Node.js for IoT
Device SDK. This is used to generate a full package for us to quickly connect to AWS IoT.
Choose Next to continue
Enter ratchet
for the Name field, and click Next step
If you’re using a shared account, add your initials to this name, e.g. cwratchet
Everything has been generated for you! Click the button under Download connection kit for to download the package.
Once you have downloaded the zip file you’ll be able to click the Next step link.
To summary, the following contents have been created:
* A security policy allow device to send and receive messages
* A Linux/OSX zip file containing all certificates
Do not lose this zip file, it contains your private key file which cannot be retrieved again. Do not need to run the scripts on the last page of the wizard, just click Done.
The default security policy created by the above wizard will limit the topics your device can publish on. For the labs in this workshop we’re going to create a more open policy. So we need to find and edit the policy that has been created already.
On the IoT Console click on Manage - it will default to Things
Find the thing you just created, for example ratchet in this lab
Click on your device to see it’s details
Click on Security
Click on the attached certificate - see below
Click on Policies
Click on your policy, in this lab it is named ratchet-Policy
Click Edit Policy Document
Enter the following for your document
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Publish",
"iot:Subscribe",
"iot:Connect",
"iot:Receive"
],
"Resource": [
"*"
]
}
]
}
Click Save as new version
Your device can now publish and subscribe to any topics. So far, three required components to use AWS IoT have been created:
You will need to Cloud9 environment for this lab. Make sure you have finished Create a Cloud9 Environment. This link will open a new tab, please do remember to return to current page.
Please verify weather your network cannot access external 8883 port. Consider using a personal hotspot if not working.
Git clone this repo, type the following code in Cloud9 terminal
git clone https://github.com/aws-samples/aws-alexa-workshop-smarthome-lamp --depth=1
cd aws-alexa-workshop-smarthome-lamp
Extract the certificates & SDK from the zip file you downloaded above. You will get the following things.
<thing-name>.private.key
<thing-name>.cert.pem
Rename the private key to private.key and drag and upload to credentials folder.
Rename the device certificate to cert.pem and drag and upload to credentials folder
Change configurations in index.js
and save
iotEndpoint
. You could find it in Iot console - settings
thingName
. The name of created thingdeviceBindingUrl
. It is the url where you deployed in the Device Binding UI.
If you followed the guide, please go to AWS Amplify Console to find it, which named Production branch URL
Install dependencies. Run npm install
in Cloud9 terminal
Run node index.js
to start the application.
The program listens to shadow information and send reported status to IoT core. In real life, you make sure that the device’s status has been changed before you send reported status. Here, since we don’t have hardware in this session, we simply report back as soon as we receive the delta.
The virtual lamp output the QRCode content to its terminal.
In the lab, we demo the on & off status of the device by sending to the topic below.
$aws/things/<thing-name>/shadow/update
the shadow message should looks like this
{
"state": {
"desired": {
"power": "ON"
}
}
}
You could also see outputs from the the device simulator terminal, for example:
turn ON Smart Lamp
turn OFF Smart Lamp
For more information upon shadow, please check using shadows