Getting started with AWS IoT and Tessel

Andreas Wittig – 13 Sep 2016

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.

Connecting AWS IoT and Tessel

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.

My Tessel 2

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.

Setup of IoT example

Configuring the IoT infrastructure by following these steps:

  1. Log in to the AWS Management Console, select region N. Virginia, and switch to AWS IoT.
  2. Click on Create a Resource and select Create a Thing.
  3. Choose temperature-sensor as Name and click on Create.
  4. Select the newly created thing temperature-sensor and click on Connect a device.
  5. Select Node.js from the list of SDKs and click on Generate certificate and policy.
  6. Download the private key and the certificate and click on Confirm and start connecting.
  7. Click on Return to Thing Detail.

Deploying your temperature collector to Tessel by following these steps:

  1. Connect to your Tessel by following Tessel Start.

  2. Create a project directory.

  3. Connect the climate module to port A of your Tessel.

  4. 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).

  5. Move the downloaded private key, certificate, and root certificate to the directory.

  6. Rename the private key to private.pem.key.

  7. Rename the certificate to certificate.pem.crt.

  8. Rename the root certificate to root-certificate.pem.crt.

  9. Copy the content from below to a file named package.json.

  10. Copy the source code from below to a file named index.js.

  11. Copy the content from below to a file named .tesselinclude.

  12. 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');
var tessel = require('tessel');
var climatelib = require('climate-si7020');

var climate = climatelib.use(tessel.port['A']);

var device = iot.device({
keyPath: __dirname + '/private.pem.key',
certPath: __dirname + '/certificate.pem.crt',
caPath: __dirname + '/root-certificate.pem.crt',
clientId: 'temperature-sensor',
region: 'us-east-1'
});

climate.on('ready', function () {
setInterval( function () {
climate.readTemperature('c', function (err, temperature) {
if (err) {
throw err;
} else {
console.log(`publish temperature: ${temperature}`);
device.publish('temperature', JSON.stringify({temperature: temperature}));
}
});
}, 60000);
});


device.on('connect', function() {
console.log('connect');
});

device.on('close', function() {
console.log('close');
});

device.on('reconnect', function() {
console.log('reconnect');
});

device.on('error', function(err) {
console.log(err);
})

device.on('offline', function() {
console.log('offline');
});

Content of .tesselinclude

certificate.pem.crt
private.pem.key
root-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:

  1. Log in to the AWS Management Console, select region N. Virginia, and switch to AWS SNS.
  2. Select Topics from the sub navigation.
  3. Click on Create new topic.
  4. Enter temperature-alert as Topic name and click on Create topic.
  5. Open the details of the created topic by clicking on its ARN.
  6. Click on Create subscription.
  7. Select Email as Protocol and insert your email address into Endpoint.
  8. Click on Create subscription.
  9. 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:

  1. Log in to the AWS Management Console, select region N. Virginia, and switch to AWS IoT.
  2. Click on Create a Resource and select Create a Rule.
  3. Choose temperaturealert as Name.
  4. Fill in temperature as Attribute and Topic filter.
  5. Insert temperature > 10 as condition.
  6. Select `Send message as a push notification (SNS)* at Choose an action.
  7. Select `temperature-alert* at SNS target.
  8. Fill in RAW as Message format.
  9. Click on Create a new role to create a new IAM role allowing the IoT rule to send messages to SNS.
  10. Type in temperature-alert as name and click on Create.
  11. Click on Add action.
  12. 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:

  1. Log in to the AWS Management Console, select region N. Virginia, and switch to AWS SNS.
  2. Select Topics from the sub navigation.
  3. Pick the element named temperature-alert and click on Actions and choose Delete topics.
  4. Switch to AWS IoT.
  5. Delete the IoT rule temperature.
  6. Deactivate the certificate and detach it from the policy and the thing.
  7. Delete the certificate.
  8. Delete the policy temperature-sensor-Policy and the thing temperature-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!

Andreas Wittig

Andreas Wittig

I’ve been building on AWS since 2012 together with my brother Michael. We are sharing our insights into all things AWS on cloudonaut and have written the book AWS in Action. Besides that, we’re currently working on bucketAV,HyperEnv for GitHub Actions, and marbot.

Here are the contact options for feedback and questions.