Book Image

Learning AWS IoT

By : Agus Kurniawan
Book Image

Learning AWS IoT

By: Agus Kurniawan

Overview of this book

The Internet of Things market increased a lot in the past few years and IoT development and its adoption have showed an upward trend. Analysis and predictions say that Enterprise IoT platforms are the future of IoT. AWS IoT is currently leading the market with its wide range of device support SDKs and versatile management console. This book initially introduces you to the IoT platforms, and how it makes our IoT development easy. It then covers the complete AWS IoT Suite and how it can be used to develop secure communication between internet-connected things such as sensors, actuators, embedded devices, smart applications, and so on. The book also covers the various modules of AWS: AWS Greengrass, AWS device SDKs, AWS IoT Platform, AWS Button, AWS Management consoles, AWS-related CLI, and API references, all with practical use cases. Near the end, the book supplies security-related best practices to make bi-directional communication more secure. When you've finished this book, you'll be up-and-running with the AWS IoT Suite, and building IoT projects.
Table of Contents (14 chapters)
Title Page
Packt Upsell
Contributors
Preface
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());
});