Getting started with AWS IoT and Tessel
AWS launched its IoT service at re:Invent 2015 and made it available for everyone in December 2015. I’ll show you how to get started with AWS IoT by using a thing, called Tessel, connected to the Internet during this article.
What is AWS IoT?
AWS IoT allows you to build an IoT infrastructure and integrate with many other AWS services. Combine the following components of AWS IoT as needed to cover your requirements.
- Device Gateway: enables things to communicate with AWS IoT
- Message Broker: provides a pub-sub system based on MQTT, WebSocket, or HTTP REST pub
- Rules Engine: routes messages to integrated services like S3, DynamoDB, and Lambda based on conditions
- Security and Identity: manages identity and access credentials for your things
- Thing Registry: organizes devices helping you to keep an overview of your fleet
- Thing Shadow: synchronizes device state between the device and the backend service
AWS IoT is a fully managed service, which means AWS is subject for operating the service with high availability and scalability.
What is Tessel?
A Tessel is an IoT device running JavaScript or more precise Node.js. The latest version, Tessel 2, is providing connectivity over Ethernet and Wifi and is offering two module ports and two USB ports.
There are a bunch of modules available that can be attached the module and USB ports:
- Accelerometer: detect orientation and movement
- Ambient: meters sound and light intensity
- Relay: switches power
- Climate: meters temperature and humidity
- Infrared: sends and detects infrared signals
- Servo: controls a servo
- RFID: reads RFID cards
- GPS: detects global position
- Bluetooth LE: communicates via Bluetooth Low Energie
- Cellular: communicates via 3G
- Camera: takes pictures and records video
- Audio: records audio
- Various community-created modules for even more flexibility and fun!
As you will see next, combining AWS IoT and a Tessel provides a flexible and easy to use IoT development environment. An excellent basis for a prototype or proof-of-concept.
Simple Example: Temperature Alert
The following example guides you through setting up a temperature alert with Tessel and AWS IoT. The climate module is used to meter the temperature. The device will report a measuring point every minute. The following figure illustrates the setup.
Configuring the IoT infrastructure by following these steps:
- Log in to the AWS Management Console, select region
N. Virginia
, and switch to AWS IoT. - Click on Create a Resource and select Create a Thing.
- Choose
temperature-sensor
as Name and click on Create. - Select the newly created thing temperature-sensor and click on Connect a device.
- Select
Node.js
from the list of SDKs and click on Generate certificate and policy. - Download the private key and the certificate and click on Confirm and start connecting.
- Click on Return to Thing Detail.
Deploying your temperature collector to Tessel by following these steps:
Connect to your Tessel by following Tessel Start.
Create a project directory.
Connect the climate module to
port A
of your Tessel.Download the root certificate (not available anymore at
https://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification-Authority-G5.pem
).Move the downloaded private key, certificate, and root certificate to the directory.
Rename the private key to
private.pem.key
.Rename the certificate to
certificate.pem.crt
.Rename the root certificate to
root-certificate.pem.crt
.Copy the content from below to a file named
package.json
.Copy the source code from below to a file named
index.js
.Copy the content from below to a file named
.tesselinclude
.Run
t2 push index.js
from your command line. Make sure setting your current working directory to the project directory.Content of
package.json
{
"name": "tessel-aws-iot",
"dependencies": {
"aws-iot-device-sdk": "1.0.12",
"climate-si7020": "0.1.1"
}
}
Content of index.js
var iot = require('aws-iot-device-sdk'); |
Content of .tesselinclude
certificate.pem.crt |
Your Tessel is now sending temperature measuring points to AWS IoT every minute. You deserve a short ☕️ break.
There is only one thing missing: setting up an alert if the temperature increases a threshold value.
First of all, you need to setup an SNS topic and subscribe to messages via email:
- Log in to the AWS Management Console, select region
N. Virginia
, and switch to AWS SNS. - Select Topics from the sub navigation.
- Click on Create new topic.
- Enter
temperature-alert
as Topic name and click on Create topic. - Open the details of the created topic by clicking on its ARN.
- Click on Create subscription.
- Select
Email
as Protocol and insert your email address into Endpoint. - Click on Create subscription.
- Wait for an incoming mail from SNS and confirm the subscription by clicking on the provided link.
Everything is ready to create a rule containing a condition to trigger an alert if the temperature exceeds the defined threshold. Follow these steps to setup an IoT rule:
- Log in to the AWS Management Console, select region
N. Virginia
, and switch to AWS IoT. - Click on Create a Resource and select Create a Rule.
- Choose
temperaturealert
as Name. - Fill in
temperature
as Attribute and Topic filter. - Insert
temperature > 10
as condition. - Select `Send message as a push notification (SNS)* at Choose an action.
- Select `temperature-alert* at SNS target.
- Fill in
RAW
as Message format. - Click on Create a new role to create a new IAM role allowing the IoT rule to send messages to SNS.
- Type in
temperature-alert
as name and click on Create. - Click on Add action.
- Click on Create to create the IoT rule.
You will receive an email whenever a temperature measurement point exceeds 10 degree Celsius. Adopt the condition in the IoT rule if you want to get alerted at another threshold value.
Time to clean up! Follow these instructions to delete all created resources:
- Log in to the AWS Management Console, select region
N. Virginia
, and switch to AWS SNS. - Select Topics from the sub navigation.
- Pick the element named
temperature-alert
and click on Actions and choose Delete topics. - Switch to AWS IoT.
- Delete the IoT rule
temperature
. - Deactivate the certificate and detach it from the policy and the thing.
- Delete the certificate.
- Delete the policy
temperature-sensor-Policy
and the thingtemperature-sensor
.
Congratulations! You have successfully deployed your first IoT application on AWS.
Benefits of using AWS IoT
I’d like to highlight the benefits of using AWS IoT as a conclusion. The main advantages of using AWS IoT from my experience are:
- Write less code: implementing IoT applications is possible almost without any code at all
- Serverless Infrastructure: focus on building your IoT applications instead of investing into setting up the infrastructure
- Integrate all the things: AWS IoT integrates into the AWS service portfolio including S3, DynamoDB, SNS, Lambda, and much more
- Simplify Deployment: use the CLI, SDKs or CloudFormation to deploy your IoT applications in an automated way
Watch out our blogs for more content about AWS IoT shortly!