--- title: "2. Build Smart Lamp Simulator" date: 2019-09-05T23:06:51+08:00 draft: false chapter: false weight: 202 --- 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. ![](/images/smart-home/simulator/simulator-1.png) 1. In the above screenshots, you’ll see a **Onboard** menu option. Select this to continue 1. Under **Configuring a device**, click the **Getting Started** button 1. 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 ![](/images/smart-home/simulator/simulator-3.png) 1. Enter `ratchet` for the **Name** field, and click **Next step** ![](/images/smart-home/simulator/simulator-5.png) > If you’re using a shared account, add your initials to this name, e.g. cwratchet 1. 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. ![](/images/smart-home/simulator/simulator-6.png) 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 ![](/images/smart-home/simulator/simulator-8.png) {{% notice warning %}} 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. {{% /notice %}} ## 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** 1. Find the thing you just created, for example **ratchet** in this lab 1. Click on your device to see it’s details 1. Click on **Security** 1. Click on the attached certificate - see below ![](/images/smart-home/simulator/simulator-15.png) 1. Click on **Policies** ![](/images/smart-home/simulator/simulator-16.png) 1. Click on your policy, in this lab it is named **ratchet-Policy** 1. Click **Edit Policy Document** 1. Enter the following for your document ``` { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "iot:Publish", "iot:Subscribe", "iot:Connect", "iot:Receive" ], "Resource": [ "*" ] } ] } ``` 1. Click **Save as new version** ![](/images/smart-home/simulator/simulator-17.png) 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 {{% notice info %}} 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. {{% /notice %}} {{% notice note %}} Please verify weather your network cannot access external 8883 port. Consider using a personal hotspot if not working. {{% /notice %}} 1. Git clone [this repo](https://github.com/aws-samples/aws-alexa-workshop-smarthome-lamp), 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 ``` 1. Extract the certificates & SDK from the zip file you downloaded above. You will get the following things. - A **private key** named `.private.key` - A **device certificate** named `.cert.pem` 1. Rename the private key to **private.key** and drag and upload to **credentials** folder. 1. Rename the device certificate to **cert.pem** and drag and upload to **credentials** folder 1. Change configurations in `index.js` and save - `iotEndpoint`. You could find it in Iot console - settings ![](/images/smart-home/simulator/simulator-18.png?width=500) - `thingName`. The name of created thing - `deviceBindingUrl`. It is the url where you deployed in the [Device Binding UI]({{< ref "1.build-device-ui.md" >}}). If you followed the guide, please go to AWS Amplify Console to find it, which named **Production branch URL** ![](/images/smart-home/amplify-url.png?width=500) 1. Install dependencies. Run `npm install` in Cloud9 terminal 1. 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. ![](/images/smart-home/simulator/qrcode-terminal.png?width=400) ## Test Shadow In the lab, we demo the on & off status of the device by sending to the topic below. ``` $aws/things//shadow/update ``` the shadow message should looks like this ``` { "state": { "desired": { "power": "ON" } } } ``` ![](/images/smart-home/simulator/simulator-19.png) 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](https://docs.aws.amazon.com/iot/latest/developerguide/using-device-shadows.html)