NodeJs Connect AWS-IOT Device with JS SDK

Published May 11, 2018Last updated May 15, 2018
NodeJs Connect AWS-IOT Device with JS SDK

Goto AWS dashboard

Create Registry Things/Button by click on Create button (blue button right side corner) and enter the name of button/Things. After setting the name click on next after that click on create certificate button to get the credential of button/things, it will give you
a. private.pem.key
b. public.pem.key
c. certificate.pem.crt
d. and path of rootCA.pem
Download all the four Credentials .

After that click on Activate button then done button.

NodeJS Setup

Create a Project directory like this:
aws.png

Now install npm modules to make connection
npm module aws-iot-device-sdk
Create simple one js file like server.js and Put the following lines of code

var awsIot = require('aws-iot-device-sdk');
 
//
// Replace the values of '<YourUniqueClientIdentifier>' and '<YourCustomEndpoint>'
// with a unique client identifier and custom host endpoint provided in AWS IoT.
// NOTE: client identifiers must be unique within your AWS account; if a client attempts 
// to connect with a client identifier which is already in use, the existing 
// connection will be terminated.
//
var device = awsIot.device({
   keyPath: 'xxxxxxxxx-private.pem.key',
  certPath: 'xxxxxxxxx-certificate.pem.crt',
    caPath: 'rootCA.pem',
  clientId: 'MyConnect',
   host: 'xxxxxxx.iot.ap-southeast-1.amazonaws.com'
});
 
//
// Device is an instance returned by mqtt.Client(), see mqtt.js for full
// documentation.
//
device
  .on('connect', function() {
    console.log('connect');
    //device.subscribe('topic_1');
    device.publish('MyConnectPolicy', JSON.stringify({ test_data: 'NodeJS server connected...'}));
  });
 
device
  .on('message', function(topic, payload) {
    console.log('message', topic, payload.toString());
  });

The ClientID is basically the name of the Policy (goto dashboard and click on secure option and click on Policies and create the Policy attach the same certificate of button/Things to Policy also) and and for host, Find your custom endpoint in the AWS IoT console. (From the dashboard, in the left navigation pane, choose Manage, and then choose Things. Select the box representing your button to show its details page. On the details page, in the left navigation pane, choose Interact and look for the HTTPS section, near the top.) Your endpoint will look something like the following:

ABCDEFG1234567.iot.us-east-2.amazonaws.com
where ABCDEFG1234567 is the subdomain and us-east-2 is the region..

Now Goto Test in dashboard and enter Subscription topic name(MyConnectPolicy as you have written in code) create Subscribe to a topic

Run your js file from Nodejs command Prompt : node server.js.

You will see the message on the subscription dashboard 'NodeJS server connected...'

Same way Button event (Single click, Double click, Long Press) also can be capture
Coming Soon in next Post

Discover and read more posts from Mohammed AbdurRaheem
get started