Interaction between AWS IoT Button and IoT devices

We have learned how to communicate with AWS IoT Button. In this section, we will continue developing the IoT application to enable interaction between IoT devices and AWS IoT Button. Our scenario is to apply Raspberry Pi 3 with the attached LED. When AWS IoT Button is clicked, the LED will be lit. Otherwise, the LED will be turned off if AWS IoT Button is double clicked. You can see this scenario in the following image:

To enable accessing AWS IoT from Raspberry Pi 3, you should register it to obtain a certificate, private, and public keys files. We will use them to access the AWS IoT server. We use the same wiring from  Chapter 4, Building Local AWS Lambda with AWS Greengrass. We attach the LED on GPIO 18 (pin 12), as seen in the following image:

For program implementation, we use the Node.js platform. We should install AWS IoT SDK for Node.js. We also need the rpio library to access GPIO that we learned in Chapter 4Building Local AWS Lambda with AWS Greengrass.

The Node.js application will subscribe a topic, iotbutton/+, and listen to the incoming message. Once you have clicked on AWS IoT Button, the button type of AWS IoT Button shows SINGLE. The program will turn on the LED. Otherwise, if we click twice on AWS IoT Button, we obtain the DOUBLE clicked type. The program will turn off the LED.

We use the previous program for the publisher/subscriber. We only modify on the subscriber topic. Then, we filter the clicked type from the incoming message. The following is a code snippet for the Node.js application:

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

...

device
.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());

var ret = JSON.parse(payload.toString());
if(ret.clickType=='SINGLE'){
console.log('SINGLE clicked --> TURN ON LED');
rpio.open(12, rpio.OUTPUT, rpio.LOW);
rpio.write(12, rpio.HIGH);
}
if(ret.clickType=='DOUBLE'){
console.log('DOUBLE clicked --> TURN OFF LED');
rpio.open(12, rpio.OUTPUT, rpio.LOW);
rpio.write(12, rpio.LOW);
}
});

...

});

You should change keyPath for the private key file and certPath for the certificate file from Raspberry Pi 3. Save this program as the aws-iot-pi.js file.

You can run this program using the following command in Raspberry Pi 3:

$ node aws-iot-pi.js

Now you can test by clicking the button once and also by double clicking it.  You should obtain incoming messages and on/off lighting. A sample of the program output can be seen in the following screenshot:

While testing, my Raspberry Pi 3 shows the LED being lit after IoT is clicked once in the following image: 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.188.238