Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning AWS IoT

You're reading from  Learning AWS IoT

Product type Book
Published in Jan 2018
Publisher Packt
ISBN-13 9781788396110
Pages 278 pages
Edition 1st Edition
Languages
Author (1):
Agus Kurniawan Agus Kurniawan
Profile icon Agus Kurniawan
Toc

Table of Contents (14) Chapters close

Title Page
Packt Upsell
Contributors
Preface
1. Getting Started with AWS IoT 2. Connecting IoT Devices to AWS IoT Platform 3. Optimizing IoT Computing Using AWS Greengrass 4. Building Local AWS Lambda with AWS Greengrass 5. Expanding IoT Capabilities with AWS IoT Button 6. Visualizing AWS IoT Data 7. Building Predictive Analytics for AWS IoT 8. Securing AWS IoT 1. Other Books You May Enjoy Index

Building an AWS IoT program


After we have configured our AWS IoT and added the IoT device, we can develop a program to access AWS IoT. In this scenario, our computer is used as an IoT thing. We also used Node.js to access AWS IoT, so we need to install AWS IoT SDK for JavaScript. For testing, we will build a Node.js application to access AWS IoT for such purposes as connecting, sending, and receiving.

Now, create a file called comp-demo.js. Then, write the following Node.js scripts:

var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
    keyPath: 'cert/macos-computer.private.key',
   certPath: 'cert/macos-computer.cert.pem',
     caPath: 'cert/root-CA.crt',
       host: 'xxxxxxx.iot.ap-southeast-1.amazonaws.com',
   clientId: 'user-testing',
     region: 'ap-southeast-'
 });
device
   .on('connect', function() {
     console.log('connected');
     device.subscribe('topic_1');
     device.publish('topic_1', JSON.stringify({ test_data: 1}));
   });
device
   .on('message', function(topic, payload) {
     console.log('message', topic, payload.toString());
});

Please change the path and certificate files from your AWS IoT on parameters such as keyPath, certPath, caPath, host, and region. Save this file.

How to work with the program?

Now we will review our program, comp-demo.js. The following is a list of steps for the program:

  1. Firstly, we apply the required library from AWS IoT SDK for JavaScript. Then, we declare our device based on our IoT thing from AWS IoT:
var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
    keyPath: 'cert/macos-computer.private.key',
   certPath: 'cert/macos-computer.cert.pem',
     caPath: 'cert/root-CA.crt',
       host: 'xxxxxxx.iot.ap-southeast-1.amazonaws.com',
   clientId: 'user-testing',
     region: 'ap-southeast-'
 });
  1. We try to connect to AWS IoT. After we are connected, we subscribe a specific topic, for instance, topic_1. Then, we send a message by calling the publish() function:
device
   .on('connect', function() {
     console.log('connected');
     device.subscribe('topic_1');
     device.publish('topic_1', JSON.stringify({ test_data: 1}));
   });
  1. To receive an incoming message from AWS IoT, we listen to the message event as follows:
device
   .on('message', function(topic, payload) {
     console.log('message', topic, payload.toString());
});
You have been reading a chapter from
Learning AWS IoT
Published in: Jan 2018 Publisher: Packt ISBN-13: 9781788396110
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime