Reader small image

You're reading from  Learning AWS IoT

Product typeBook
Published inJan 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788396110
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Agus Kurniawan
Agus Kurniawan
author image
Agus Kurniawan

Agus Kurniawan is an independent technology consultant, author, and lecturer. He has over 18 years' experience working on various software development projects, including delivering training courses and workshops, and delivering technical writing. He has done a few research activities related to wireless networking, software, and security in multiple universities. Currently, he is pursuing a Ph.D. program in Computer Science in Germany. He has previously written five books for Packt.
Read more about Agus Kurniawan

Right arrow

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());
});
Previous PageNext Page
You have been reading a chapter from
Learning AWS IoT
Published in: Jan 2018Publisher: PacktISBN-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.
undefined
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

Author (1)

author image
Agus Kurniawan

Agus Kurniawan is an independent technology consultant, author, and lecturer. He has over 18 years' experience working on various software development projects, including delivering training courses and workshops, and delivering technical writing. He has done a few research activities related to wireless networking, software, and security in multiple universities. Currently, he is pursuing a Ph.D. program in Computer Science in Germany. He has previously written five books for Packt.
Read more about Agus Kurniawan