Introduction: AWS IoT + DragonBoard

It's a simple way to connect the dragonBoard into AWS IoT and how to access the GPIOs and analog pins using nodeJS.

Step 1: Install AWS IoT SDK

Starting our AWS IoT sample project, we need to install the aws-iot-sdk inside your project folder:

  • npm install aws-iot-device-sdk

or it's possible to download the source code from github and install:

Step 2: Setup AWS IoT

We need to create the project inside AWS IoT, so you need to go to https://console.aws.amazon.com/iot

  • Click on "create a thing" and choose a name to your project.

Now, we need to prepare our "thing" to be able to publish and/or receive messages:

  • Click in our "thing" and then click on "Connect a Device"
  • After that, we can select the language that we're using to develop our application. I'm going to use NodeJS to this sample. Select NodeS and then click on "Generate Certificate and Policy"
  • We'll need to generate files and also it's necessary to download the root-CA.crt file.
  • Transfer this files into your project file. You can create a folder, for example, "certs" and add the certificate files into it.

Step 3: Prepare the Connection Code

You need to add in your code the variable of the AWS IoT sdk.

var awsIot = require('aws-iot-device-sdk');

And then it's necessary to prepare the "device" variable, that's responsible to connect your board into AWS. To build this variable we need to pass:

  • KeyPath (Private key);
  • certPah (Certificate crt);
  • caPath (root-CA certificate);
  • clientId ( can be any kind of cliend Id);
  • region (This one we need to adapt into your region).

For example:

<p>var device = awsIot.device({<br>   keyPath: './certs/private.pem.key',
   certPath: './certs/certificate.pem.crt',
   caPath: './certs/root-CA.crt',</p><p>   clientId: 'Tutorial',
   region: 'eu-west-1'
});</p>

Step 4: Adding Callbacks

Once we have prepared connection variable, we're able to add triggers (callbacks) to know what's happening with our application, these are some options to use:

  • connect;
  • reconnect;
  • close;
  • message;
  • subscribe;
  • etc...;

For example, we're adding some callbacks to our application

  • Connect:

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

  • Reconnection:

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

Step 5: Publish and Subscribe Into Topics

If you want, it's possible to subscribe into some topic, that makes possible to receive messages when some device/application publish some message into this topic, for example, here we're going to subscribe into topic_1:

device.subscribe('topic_1');

And now, if someone publish into this topic, we can receive their message using the "message" callback.

device.on('message', function(topic, payload){
console.log('\n message arrived from ' + topic); console.log('message is: ' + payload.toString()); });

With this, we can know what topic we're receiving messages and the message content. The message can be a simple string or we can receive some JSON with a detail message.

And to publish some message, it's very simple, we just need to pass in which topic we want to send the message and then the message.

device.publish('topic_1', 'message');

Step 6: Access GPIOs From the DragonBoard

Now that we're already publishing and receiving messages from AWS, we can access the GPIOs from the DragonBoard, to do that, we need to install the MRAA library (http://iotdk.intel.com/docs/master/mraa/), but requires another library (SWIG) to work, and we need to install it.

And then we're able to install MRAA, to do it, we have two options, we can install using the NodeJS:

  • npm install mraa -g

Or, we can use the source code project:

And now, when can use the MRAA to write and read from digital/analog pins, for example, after receiving a message from AWS, we can turn on some connected led on our board.

var m = require('mraa');

var myDigitalPin = new m.Gpio(27); //setup acess to pin 27 from dragonboard myDigitalPin.dir(m.DIR_OUT);

device.on('message', function(topic, payload){ myDigitalPin.write(1); // 1 to enable and 0 to disable });