Arduino Using AWS IoT Serivce
by joeyzh in Circuits > Wireless
69202 Views, 39 Favorites, 0 Comments
Arduino Using AWS IoT Serivce
Have you ever wanted to control your IoT device through cloud? There are many ways to achieve such operations, we'll show you a simple way that uses Amazon Web Services (AWS) to control a single LED connected to Arduino device.
AWS IoT is a platform that enables you to connect devices to AWS Services and other devices, secure data and interactions, process and act upon device data, and enable applications to interact with devices even when they are offline. Please refer to the link for basic AWS information: https://aws.amazon.com/iot/how-it-works/. Onc can also refer to http://docs.aws.amazon.com/iot/latest/developergui... for all the detail operstions.
We'll only need one Arduino compatible board plus one LED for this project. The board we are using is Ameba Arduino board which can be found in eBay. The reason we use this board is Ameba Arduino already have 802.11 wireless built-in plus their SDK already have MQTT library that can be called to communicate with AWS.
AWS Registration
We first need to create an AWS account and sign in to it. Please go to https://aws.amazon.com/ to create an account then log in to the Amazon Management Console and click “AWS IoT” to get to AWS IoT Web page. Once you get to AWS IoT page, please select the region that close to you at the upper right corner so you can have best service quality. After you have chosen the nearby region for your location, click on "Get Started" to start.
Create a Thing
After you clicked "Get Started", you will enter a page default to “Create a thing”. Fill in the name “ameba” and click “Add attributes” on the lower left side. Attributes represent the status of ameba. The value of the attributes can be updated directly by ameba or by the control side. For the latter case, the control side request ameba to set the attribute to desired value. Here we add a attribute named “led” with value “0”, and click “Create”.
After create a thing and its attribute, we need to create a policy. Policy is used to restrict the functions that a “thing” can do. Click on “Create a Policy”. Here we do not place policy on ameba. Fill in “amebaPolicy” in the Name field, “iot:*” in Action field and “*” in Resources field. Then check “Allow”. Finally, click “Add statement”. You can add some statements if you so wish. Click “Create” to finish.
Certifications
After the above step, you should see two items, “thing” and “policy”, appear in AWS IoT page. Next, we have to set up the TLS certificate so we can used to authenticate the device. Click “Create a certificate”. You can choose to use user-defined certificate or generate a certificate by AWS IoT. In this example, we chose the easy way and click on “1-Click certificate create” to generate TLS certificates. Now you should see three links. Please click each of the three links to download “public key”, “private key” and “certificate”.
Attach to Certificate
At this time, you should see three square items, “certificate”, “policy” and “thing”, appear in left bottom of AWS IoT page. Next, we need to attach the policy and the thing to the certificate. Click “certificate” (marked "INACTIVE"), then click “Attach a policy” in the “Actions” drop down menu (which is located on the right side). Fill in the name of the policy we created in the previous step, i.e., “amebaPolicy”, and click “Attach”. Same operation for the "thing". Click “Attach a thing” in the “Actions” drop down menu. Fill in the name of the thing we created in the previous step, i.e., “ameba”, and click “Attach”. Refresh the page, and click “certificate” again. You can see two sqaure item “ameba” and “amebaPolicy” listed at the bottom of information area on the right hand side. Let's activate the certificate now. Click "certificate", and click “Activate” in the “Actions” drop down menu.
Finished Look
Now we have finished all the basic requirements for AWS IoT registration. Click “ameba” and look at the information appear at the right side:
- REST API endpoint: In the value “https://a2zweh2b7yb784.iot.ap-southeast-1.amazonaws.com/things/ameba/shadow”, the part “a2zweh2b7yb784.iot.ap-southeast-1.amazonaws.com” is the MQTT Broker server address.
- MQTT topic:The value “$aws/things/ameba/shadow/update” represents the MQTT topic we will use in the AWS IoT Shadow service (if we use MQTT only, without AWS IoT Shadow service, then we can specify other topic name). It is recommended to use “$aws/things/ameba/shadow/update” here.
Ameba Applications
You will need to install at least Ameba SDK version 1.1.1 to have this AWS sketch. Open “File” -> “Examples” -> “AmebaMQTTClient” -> “amazon_awsiot_basic”. There are five places you need to change according to your setup:
- SSID: Fill in the correct SSID of your AP
- Password: Fill in the correct password of your AP
- MQTT Server: Please fill in the MQTT Broker server address that we got in the previous step.
- Certificate: Please use the certificate file, certificate.pem.crt, that we download before to fill in this space.
- Private Key: Please use the private key file, private.pem.key, that we download before to fill in this space.
When applying the download key to the sketch, please edit its format as used in the sketch:
- Add the new line character “\n” at the end of each line.
- Add double-quote at the beginning and the end of each line.
- To concatenate each line as a string, add “\” at the end of each line.
- The last line ends with semicolon.
The root CA certificate is already done for you. You can however download it and verify: https://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification-Authority-G5.pem
Modify Status Through Cloud
After all the required modification is done, compile and run and upload the sample code to Ameba and press the reset button to start running the new code. If you open the serial monitor, you can see Ameba first will try to connect with AP, then authenticate with AWS server. You can see the LED is on by default (please attach the longer lead to digital pin 10 and shorter lead to ground). Let's go back to AWS IoT page. Now, check the thing information of “ameba” shown in AWS IoT console. You should see the published message in “Shadow status” which the led status is 1. Let's try to turn off the led. Click “Update Shadow”, you can see a “Shadow state” text field with default content. Modify the content: { "desired": { "led": 1 } }, change the value from 1 to 0, then click “Update shadow”. Voila, you can see the led in the Ameba board has been turned off, through cloud!