For this article you need an AWS IoT account, BeagleBone (any edition I used Green) and a PC. BeagleBone is connected via USB with PC and with Ethernet cable to internet. On your PC make folders hacksterdemo and hacksterdemo/certs. Second folder we will use for certificates from AWS. For writing code we are going to use Cloud9 IDE on BeagleBone. To check availability for Cloud9 IDE, open in browser address 192.168.7.2:3000. Notice port 3000. If you omit it, you will open web server from BeagleBone.
AWS IoT - creating ThingFirst step is creating AWS IoT thing. From AWS IoT tutorial I copied one paragraph:
A thing represents a device whose status or data is stored in the AWS IoT cloud. The Thing Shadow service maintains a thing shadow for each device connected to AWS IoT. Thing shadows allow you to access and modify thing state data.
In this, and I hope first article in series, we do not enter in Thing Shadow area. Our goal is to establish communication between BeagleBone and AWS IoT. For that purpose, AWS provided last week new feature : MQTT client from dashboard. This feature is a big improvement for developing IoT application. Now, you can test sending and receiving messages from device without back-end software!
Let's start! Open your AWS IoT dashboard from main AWS control panel. Your dashboard is empty, and you have to complete next steps:
- create a thing
- create policy
- create certificates
AWS provide something like a wizard for this process. You will not use any command line interface or script editor. Only one information you will enter : the name of your thing.
Slideshow below contain a few screenshots and follow them as step by step instructions. With red color are highlighted important info and buttons.
After step #4 you will get a very important form. There will be links for certificates. Read carefully note that private and public keys will not be retrievable. You have to download all certificates before you click button. And once again, I have to comment term connecting. After this step your device will not go to connect with AWS IoT. As you can see on next image in slideshow below, new form appear with links to SDK and important part of code. That part you have to copy into local text document on your PC drive. We will use it later for NodeJS script on BeagleBone.
Please review next slideshow with two screenshots before you continue after step #4.
Finally, on your dashboard you will see three new items : thing, policy and certificates. AWS IoT wizard did everything for you, even attaching policy and certificates with thing.
In your browser, open address 192.168.7.2:3000 to start Cloud9 IDE. You will get developer environment in your browser! On the left side you have workspace structure with folders and files. At bottom of page is Linux console. Rest of space is file editor.
Under folder examples make hacksterdemo and hacksterdemo/certs folder.
With Linux console navigate to folder hacksterdemo and install AWS IoT device SDK. During install process you will receive some error messages. Just ignore :).
root@beaglebone:/var/lib/cloud9/examples/hacksterdemo# npm install aws-iot-device-sdk
App.js
var device = awsIot.device({
clientId: "HacksterDemo",
caPath: "certs/root-CA.crt",
certPath: "certs/fe29d41a54-certificate.pem.crt",
keyPath: "certs/fe29d41a54-private.pem.key",
region: 'eu-west-1'
});
//
// 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('topic_2', JSON.stringify({ test_data: 1}));
});
device
.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});
Final version of project
With Linux console check files and subfolder structure
root@beaglebone:/var/lib/cloud9/examples/hacksterdemo# ls
app.js certs node_modules
root@beaglebone:/var/lib/cloud9/examples/hacksterdemo#
MQTT clientOn your dashboard you have simple MQTT client :
Next step is subscribing and publishing to topics. From code you see that BeagleBone is going to send(publish) message to topic_2 and receive(subscribe) message from topic_1. According that, Subscribe MQTT client to topic_1 and Publish to topic_2
Back to the Cloud9 IDE and run app with:
root@beaglebone:/var/lib/cloud9/examples/hacksterdemo# node app.js
After that first you will see on AWS IoT MQTT client message from BeagleBone
Write some JSON data into payload for publishing to topic_1 and press button
Finally, if you receive message in Linux Console, you finish this simple tutorial
Comments