The internet of things, or IoT for short, has seen tremendous activity and interest this past year. From enterprises, to healthcare providers, and even automobile companies, the idea of a connected world is beginning to appeal more and more to everyday consumers and businesses alike. This tectonic shift in the way the things around us operate has opened the door to a plethora of new and exciting products, and placed cloud service platforms, such as Amazon Web Services (AWS), at the forefront of scalable support for these new technologies.
Sometime last year I picked up a Particle Photon; a tiny, low powered wifi chip that's intended for rapid prototyping around IoT projects. The small board, about the side of an eraser, utilized a cloud based development toolset to update the "firmware" of the devices with your custom C++ code. The SDK for the device gives a developer access to a number of pins that allows for extensive capabilities and enhancements.
In this post, I outline the steps necessary to create a simple data capturing device, in this case a sleep sensor, that sends the information collected to AWS for processing and storage. This technique can be used for a number of entry level IoT projects, and all of the tools included in this write up are "off the shelf" and available to everyday consumers.
Particle Photon + AWS
One of the more impressive features of the Particle platform is its built in webhook integration. The wifi connected circuit boards have the ability to send data to, and receive data from, webhooks setup through the Particle service. This capability opens the door to a number of interesting solutions, including out-of-the-box integrations with IFTTT.
This webhook integration also allows for interacting with custom API endpoints, developed specifically for processing the data that is generated by the Particle. I set out to create a sleep sensor, leveraging an accelerometer in conjunction with the Particle and AWS to process the "quality" of my sleep throughout the night, sending minute-by-minute data about my movements to the cloud for processing. The following section will explain how this worked, and what services must be leveraged to make this possible.
The AWS Configuration
For this project, I used three AWS services; a DynamoDB table to store the information captured, a Lambda function to process the data from the Particle and write it to the table and a API Gateway to publicly expose the Lambda function and perform some authentication before allowing the data to be written. This section will explain how to configure these services.
DynamoDB is a NoSQL storage service that is great for IoT projects. It's extremely inexpensive, and performance is fantastic. For this project I created a single table named SleepData with a Primary Partition Key of deviceID and a Primary Sort Key of published_at set to a number. This will allow us to scan or query our DynamoDB table for specific Photon devices at specific time periods when it comes time to read the data.
After creating the DynamoDB table, I began configuring the Lambda function. The function is intended to read a JSON payload delivered via the API Gateway, sanitize and normalize the data, and store it in the DynamoDB table. I also included some logic in the function to capture when it should be recording this information and when it shouldn't, so I'm only storing information while I'm actually asleep.
Finally, I configured the API Endpoint to send data to the Lambda function. I wanted to limit who was capable of sending the information, so I also required an API Key for all requests. You can link your Lambda function to an API Gateway by navigating to the API endpoints section of your Lambda function in the AWS console. You can then create a new API gateway and set it up accordingly. There is an excellent tutorial on API Gateway and Lambda in the AWS documentation.
For this API Gateway, we'll want to make sure we have the Method set to PUT and Security set to Open with access key. This will ensure that the data we send the API Gateway is secured, and that no one other than those with an access key can send data to our endpoint.
That about sums up what's going on on the AWS side. In a future project I'll explore processing the data stored in the DynamoDB table. For now, we're only concerned with capturing it for future use.
The Particle Photon Configuration
For this project, I leveraged the "Internet Button" shield, which comes packed with an accelerometer, four buttons and a number of LED's. This gave me the capability to interact with the API, turning data recording on and off and giving me some visual feedback when I triggered an action, such as setting it to sleep, on the device.
In the world of Particle, a webhook is a published endpoint, attached to your account, that contains the information needed to send data to an external API. Setting this up can be a bit of a challenge, as you'll need to perform the operation through the CLI, and there's not a whole lot in the form of feedback when something is setup wrong. The following steps can be used to configure a Particle webhook with an AWS API Gateway.
The first thing you'll need to do is install the Particle CLI and log in. Luckily, Particle has put together a great resource for setting this up. After getting the CLI configured, you'll need to create a JSON definition file for the webhook. This file contains the API keys sent in the header, a template for the data being sent, and the URL of the endpoint. I have a sample of this file above. Finally, from the CLI, you'll need to add the webhook API:
particle webhook create SleepAPI.json
This will associate the API, as it is named in the JSON file, with your account. To send data to the API, you'll use the "Particle.publish()" function from your code snipped with the API name as the first attribute. More information on Particles implementation of Webhooks can be found in their documentation.
The small block of code used for the Particle loops through every 100 milliseconds and capture any movement that took place. I'm using a special function, specific for the Internet Button, that returns the "lowest LED" during each loop. If the "lowest LED" has changed since the previous loop, I record that as a single movement. After one full minute of looping, the total is sent to my AWS API Gateway via the Particle webhook functionality.
The end result of tying all of these services together is a pretty robust, low cost IoT platform. I was able to create a prototype in a few hours, using off the shelf products and a bit of connectivity code sprinkled throughout. While my example is a movement "sleep" tracker, it's easy to see how this type of IoT design can be used in a number of applications.
The chart above is a sample of data extracted from the DynamoDB table after a night of sleep. The data captured is complete, and an exciting first step in the creation of my roll-your-own sleep tracker. There's still plenty of work to do when it comes to processing the information, but the initial results are inspiring. The serverless architecture is quickly becoming a viable reality.