Lab 5 : AWS IoT and RESTful API
EEC 172: Lab #5 - AWS IoT and RESTful API
Author : Kelly Su, Tony Xiao
Intro
The objective of this lab is to learn how to use the Amazon Web Services, specifically the IoT protocol that allows the user to create a ‘shadow’, or a virtual representation of their hardware device on the cloud. Our hardware device we will be creating a ‘shadow’ for is our CC3200 Launchpad and use the RESTful API HTTP GET and POST commands to retrieve status updates about the virtual device and send information to the device to trigger actions. We will be using Lab 3’s code that uses decodes and prints messages on the OLED screen via the remote control and the AWS’s SNS functionality to send messages composed on the OLED screen to our cellphones.
Background
AWS (Amazon Web Services) is a collection of different web-services for your cloud based-needs. The IoT (Internet of Things) service allows you to connect your hardware device that keeps the state of your device on the cloud via MQTT or RESTful API. A device can update its state from the cloud from its ‘shadow’ when it is connected to a network. You can define rules for the device’s ‘shadow’ that can trigger actions. In this lab, that action will be to text messages to a cellphone when the CC3200 device receives a ‘send’ signal from the remote control.
OpenSSL “is a robust, commercial-grade, and full-featured toolkit for the Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols. It is also a general-purpose cryptography library.” We will be using this development tool to convert .pem formatted certificate and keys from AWS to .der format that is used by the CC3200 device.
“The RESTful API, based on the Representational state transfer (REST) architecture, is a web-based communication protocol that is widely used for many services. The RESTful API is typically implemented using HTTP. Although not as optimized for low power and low cost applications as another IoT protocol, MQTT, RESTful API is widely used due to its simplicity and genericity. The RESTful API defines the format of the messages that are sent to a service to interact with that service. The function of the RESTful APIs will vary depending on the service with which you are interacting.” In this lab, we will be using two common commands provided by RESTful API, GET to retrieve information from the shadow and POST to update information onto the cloud.
Goals
Part I.A: Setting up an Amazon AWS account
Create an Amazon account and watch a tutorial on how IoT works.
Part I.B: Setting Up Your First Device Thing/Shadow with the AWS IoT Console
Follow the directions on how to add a thing to your AWS IoT , as well as downloading the associated public keys, private keys and certificates for access to the ‘shadow’
Part I.C: Making a Policy to Allow Update/Get Status from Thing Shadows
Create a policy that will gives the device privilege to access the AWS information through HTTP GET and POST requests from the ‘shadow’
Part I.D: Converting the Keys/Certificates for Use with the CC3200
Use OpenSSL to convert the downloaded keys and certificates from .pem files to .der format
Part I.E: Using UniFlash to flash key and certificates to CC3200
Flash keys and certificates onto the CC3200 device so it can be granted access to the AWS ‘shadow’ through a secured connection
Part I.F. Accessing AWS using the RESTful API
Configure the correct keys, date/time, and format the POST and GET method response in JSON in the application Configure the endpoint address of the server that the device will access Configure the network AP in the application
Part II.A: Creating an SNS Topic
Create a new topic in the SNS module and create a subscription for the SMS. Configure a phone number as the endpoint
Part II.B: Creating an IoT Rule
Create a rule that will trigger when you push updates to your shadow device Trigger the rule (a text message to send) using the CC3200
Part II.C: Integrate your IR Remote multi-tap texting to send a message to your phone
Now add Lab 3’s functionality that will allow the remote control to trigger a text message to send
Methods
Part I.A: Setting up an Amazon AWS account
We learned about how the IoT protocol allows updates to the device from the ‘shadow’ when it is connected to a network and the high level way that rules are used by AWS to trigger actions
Part I.B: Setting Up Your First Device Thing/Shadow with the AWS IoT Console
We learned that how the ‘shadow’ is accessed by the device, which is through encrypted keys and certificates
Part I.C: Making a Policy to Allow Update/Get Status from Thing Shadows
We learned that to get status updates and post information to your ‘shadow,’ special privileges are needed for each command, thus creating a need for policies
Part I.D: Converting the Keys/Certificates for Use with the CC3200
We learned how to use OpenSSL to convert .pem to .der We learned .pem use ASCII characters and .der uses binary
Part I.E: Using UniFlash to flash key and certificates to CC3200
We learned how to flash keys and certificates onto our device for it to have permission to access the AWS shadow through a secured network
Part I.F. Accessing AWS using the RESTful API
We learned how to configure the access point for the network and how to send post request and get requests with JSON
Part II.A: Creating an SNS Topic:
We learned how to use the SNS module to add a SMS subscription for our phone
Part II.B: Creating an IoT Rule:
We learned how to create rule that will do a certain action when the ‘shadow’ is updated with certain information using MQTT
Part II.C: Integrate your IR Remote multi-tap texting to send a message to your phone
We used the given application as our starting code with all the modifications done in the previous part and copy and pasted our code from lab3 into main. We added lines 1120-1123 and 1125-1125 into our code that will format the message to be sent to SMS format and called http_post to send trigger the AWS SNS service rule to send the text message.
High-level explanation of code:
The SimpleLinkWLANEventHandler()
configures the network connection for the device through the given access point. The SimpleLinkNetAppEventHandler()
sets up the gateway IP address for the network. The SimpleLinkSockEventHandler()
configures the socket for transmission over the network. connectToAccessPoint()
sets the device to a default state by calling ConfigureSimpleLinkToDefaultState()
and calls WlanConnect()
to configure the security key and connect to the network. http_post()
sends the message specified in DATA1 buffer to the server using sl_send()
and waits for a response using sl_Recv()
. It also prints out the display headers using UART and changes the GPIO pin colors to indicate error if the post failed. In our main, we just had to call http_post()
when the ‘send’ button is pressed, grab the buffer message displayed on the OLED screen and use this information to send to the http_post that triggers the action defined in that command on AWS.
Discussion
The most difficult part about this lab was figuring out how to put everything together. The lab walked us through things easily step by step but figuring out how they all contribute and actually understanding the implementation of the application was a little difficult. It was also challenging to figure out the JSON get/post response format. The network, access point, and socket code was also difficult to figure out as well.
Conclusion
In this lab, we learned about how the use the AWS web services to create a ‘shadow’ of our hardware device in the cloud, how to have the device and application securely access the ‘shadow’, how to flash they keys onto the drive, how to create rules to trigger SNS actions, how to grant privileges to enable http post and get requests to be done using policies and how to integrate the application by setting the endpoint of the server, date/time, and configuring the private/public keys and certificates.