2. Build Smart Lamp Simulator

This article introduces how to create a virtual Lamp and interact with AWS Iot Core.

Create a Thing on AWS IoT Console

Let’s get your account setup with a new Thing, certificates and security policies.

  1. 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.

  2. In the above screenshots, you’ll see a Onboard menu option. Select this to continue

  3. Under Configuring a device, click the Getting Started button

  4. 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

  5. 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

  6. 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.

Modify Default Policy

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.

  1. On the IoT Console click on Manage - it will default to Things

  2. Find the thing you just created, for example ratchet in this lab

  3. Click on your device to see it’s details

  4. Click on Security

  5. Click on the attached certificate - see below

  6. Click on Policies

  7. Click on your policy, in this lab it is named ratchet-Policy

  8. Click Edit Policy Document

  9. Enter the following for your document

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "iot:Publish",
            "iot:Subscribe",
            "iot:Connect",
            "iot:Receive"
          ],
          "Resource": [
            "*"
          ]
        }
      ]
    }
    
  10. 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:

  • Device certificates
  • A security policy and being modified for the permission we need
  • The certificate and security policy have been attached to the thing ratchet

Subscribe to IoT Core

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.

  1. 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
    
  2. Extract the certificates & SDK from the zip file you downloaded above. You will get the following things.

    • A private key named <thing-name>.private.key
    • A device certificate named <thing-name>.cert.pem
  3. Rename the private key to private.key and drag and upload to credentials folder.

  4. Rename the device certificate to cert.pem and drag and upload to credentials folder

  5. Change configurations in index.js and save

    • iotEndpoint. You could find it in Iot console - settings
    • thingName. The name of created thing
    • deviceBindingUrl. 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
  6. Install dependencies. Run npm install in Cloud9 terminal

  7. 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.

Test Shadow

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