This is a step-by-step tutorial for publishing Hexiwear sensor data onto the Amazon cloud. You'll need (1) an Amazon AWS account, a PC, Mac or Linux computer, (2) a smartphone with Bluetooth turned on and (3) a Hexiwear device to pass the finish line. Traditionally, it sure takes a while to set up the server-side of this project, while in this case we've prepared an AWS script for doing most of the heavy lifting for us.
We'll be using three AWS services:
- A Lambda service which is an event listener for aggregating data,
- The Dynamo database for storage where you can make queries later on
- IAM to set up a role on AWS with very limited capabilities, and safer to use than your root account; you won't need to flash the identity or credentials around for your personal or your company's main Amazon account.
If you don’t have access to a physical Hexiwear device yet, or have just ordered one, you can still stay with the action; you can use the demo app for reading and writing simulated sensor data, and add a physical device to the setup later on.
The mobile device will be acting as a gateway for the Hexiwear, passing on the sensor data to the cloud. In coming tutorials, we'll also be showing how a smartphone or tablet can be used as a remote to dynamically control which sensor data is sent, and how to easily set different modes on the Hexiwear, using it as a peripheral device. We'll also investigate how data can be visualized on the mobile device using Javascript libraries like the Smoothie.js
chart library, Google Charts and Three.js
.
Start by clicking on this Amazon short link, its not a trap. This cloud-side wizard, in your computer’s web browser. It will do much of the heavy lifting, helping you to set up your AWS services (i.e. IAM to create a designated user role, Lambda to run code on incoming events and DynamoDB to store and retrieve data).
You'll find that most fields and checkboxes are pre-configured. In practice that means that you need to press "next
" and "ok
" on the screens to follow, as the fields are filled in already with what you need, thanks to the scripted wizard in the link provided.
Many AWS services, like Lambda and DynamoDB are available for limited use without cost in the Free tier. Follow this link for the details on creating your free account, or use the account you have: aws.amazon.com/free/. To be precise, the free AWS tier has in our case the following limits (Sept 2016):
- DynamoDB: 25 GB of Storage, 25 Units of Read Capacity and 25 Units of Write Capacity, up to 200M requests per month.
- Lambda service: 1,000,000 free requests, and 3.2 million seconds of compute time per month.
- IAM: A framework for setting up and assigning user roles and accessibility to services on AWS.
The wizard helps you define a software stack (called an “AWS CloudFormation
” in Amazon lingo), which is a collection of resources you’ll need. The default stack name is “IOT2015
”, which was for last year when our friends at Amazon set up this script for us originally. There is also a pre-configured AWS S3 template you'll find called "ioe2015.s3.amazonaws.com/iot.template
” which automates most of the setup. In this screen, you can download the template's script if you want to see what it does in details. On this screen you don’t need to alter anything really.
Just press Next.
This is where the Lambda function's parameters are defined. Basically, the AWS Lambda is a simple yet efficient event listener and queuing mechanism which is used for gathering sensor data. Use the default bucket name “ioe2015
” and the Lambda package S3 key “iotapi.zip
”.
Now press Next.
This is the Options screen. You can leave the fields blank.
Press Next again.
This last screen summarizes what will be going on, once you execute your setup:
- Creation of the Lambda function for triggering code on incoming events
- A DynamoDB database table for storing values
- A new user in the IAM service (Identity and Access Management), with access only to (1) and (2), and not anything else you’re running on AWS.
Finally, check the checkbox to allow creation of the user and permissions.
Press CreateNow the service setup on AWS is complete. You don't have to create additional Stacks or Services, even if the landing screen arguably tempts you to do so.
When the CloudFormation
stack creation is complete, just wait for it a number of seconds - select the created a IOE2015 stack and click the Outputs tab. There you can see the configuration values that you should enter in the configuration of the Evothings application. Have these values ready to enter in the steps below. If you do mess up, don't fret, you can always go to CloudFormation
from the main menu, and click Output from there.
You’ve now all done with Amazon AWS set up and are good to go for the next step, hang on, we're going mobile!
Evothings StudioDownload and install the Evothings Studio software and the corresponding Evothings Viewer app to your iPhone or Android - search for “evothings viewer
” via the public app stores. While you're on the Evothings Download page, you can also generate a Cloud Token which you'll be asked to paste in Evothings Studio when you connect the first time. It's an anonymous serial number, which allows you to see which computer is connected to which phone(s) when you run the Evothings Studio software. It will looks something like this, like a UUID on any networked device.
371eee72-bh09-2g15-1d6e-cd2af323a592
Lastly, connect your phone to your computer by generating a connect code on the computer by pressing the GET KEY button in the Connect tab in the Studio software. A short string of four letters and four numbers appears, for you to type into the Evothings Viewer. Go to Examples and run .e.g. the Hello World or any of the other examples to see an app load on your mobile device.
The Hexiwear AWS exampleDownload the example project and unzip it in a directory with write permissions. You can find it here: evothings.com/hexiwear/hexiwear-sensors-aws.zip
Open the file called aws-config.js file and enter your AWS credentials for the IOT2015 user, that you found under the Output tab in CloudFormation previously.
evothings.aws.config =
{
//generate access keys under IAM
accessKeyId: 'ASDFASDFASDFADSF',
secretAccessKey: 'asdASDFOIasdfopadsfpoiadfSOIadsfAPODSFi23423',
// find FunctionName and region under Lambda
region: 'eu-west-1',
params: { FunctionName: 'IOE2015-IoTAPI-XXFLIHSDLFHLDF'}
}
Not these ones mind you, they are just examples, state your own. Press Save.
Drag and drop the index.html file from your newly downloaded xip file into the Evothings Studio workbench window and press RUN. The app now loads at once on your smartphone. Test operations by pressing the top button READ/WRITE DEMO in the app. Write some random data by pressing WRITE TEMPERATURE VALUE. Now read the data just written by pressing READ TEMPERATURE VALUE. This shows that your AWS Lambda service is working correctly.
Navigate back to the app main menu and select the third menu option on-screen, the SENSORTAG DEMO example. This is the point in time where the Hexiwear actually comes into play, at last some hardware in action:
Connecting the HexiwearProbably you've already been playing around with the Hexiwear and your smartphone. Then you've already paired the Hexiwear device with your phone, otherwise you'll get an on-screen six-digit number shown on the Hexiwear's little 96x96 pixel display, to enter on your mobile device when you RUN your Evothings app for the first time. The Hexiwear device has several modes, so set the Hexiwear either as a Beacon (main menu) or as a Pedometer as the sensors will become active in these modes. Press Connect in the mobile app on your smartphone or tablet, and see the status message changing from scanning to uploading data. Just like clockwork! You're done and have now completed this tutorial with flying colors.
The code source and open source licensingThe source code for this app is released under an open source Apache2 licence, so you can play around, share, modify, publish or sell the code as you desire. Perhaps you'd like to see some steampunk analog gauges from the Google Charts arsenal like the ones we published previously in another tutorial. You'll need to do more of your own tinkering then, if we get lots of followers on this one, we might just do it for you.
That's it. Happy coding! Did it take more than 15 minutes, then I perhaps owe you a beer.
Links, further readingMore on AWS and how to get started. AWS programme for startups. If you have a solid case, you should apply! AWS Free Tier.
More tutorials and useful code for your mobile IoT needs here.
Comments