nodejsで、AWSIoTのDevice SDKを用いてMacBookをデバイスに登録する
必要な環境
パッケージ | Version |
---|---|
Node.js | V4.4.3 |
v4.4.3をインストールしておく
$ node -v
v4.4.3
aws-iot-device-sdkのインストール
npm install aws-iot-device-sdk
サンプルのソースコード
device.js
awsIot = require('aws-iot-device-sdk');
//
// Replace the values of '<YourUniqueClientIdentifier>' and '<YourAWSRegion>'
// with a unique client identifier and the AWS region you created your
// certificate in (e.g. 'us-east-1'). NOTE: client identifiers must be
// unique within your AWS account; if a client attempts to connect with a
// client identifier which is already in use, the existing connection will
// be terminated.
//
var device = awsIot.device({
keyPath: <YourPrivateKeyPath>,
certPath: <YourCertificatePath>,
caPath: <YourRootCACertificatePath>,
clientId: <YourUniqueClientIdentifier>,
region: <YourAWSRegion>
});
//
// 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());
});
必要なファイル
npm install aws-iot-device-sdk
awsIot = require('aws-iot-device-sdk');
//
// Replace the values of '<YourUniqueClientIdentifier>' and '<YourAWSRegion>'
// with a unique client identifier and the AWS region you created your
// certificate in (e.g. 'us-east-1'). NOTE: client identifiers must be
// unique within your AWS account; if a client attempts to connect with a
// client identifier which is already in use, the existing connection will
// be terminated.
//
var device = awsIot.device({
keyPath: <YourPrivateKeyPath>,
certPath: <YourCertificatePath>,
caPath: <YourRootCACertificatePath>,
clientId: <YourUniqueClientIdentifier>,
region: <YourAWSRegion>
});
//
// 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());
});
必要なファイル
項目 | 概要 | 解説場所 |
---|---|---|
keyPath | SSL証明書のprivate-key | Thingsの作成で説明 |
certPath | SSL証明書のcertificate | Thingsの作成で説明 |
caPath | Root証明書 | ルート証明書の取得で説明 |
clientId | ClientID | Things名(この例の場合MacBook) |
region | Tokyoならap-northeast-1 |
Thingsの作成
ここまでの手順で
- SSL証明書のprivate-key
- SSL証明書のcertificate
が取得できる。
$ mkdir cert
keyというフォルダを作成し、そこに保存しておく。
また、AWSIoTのDashboardには、MacBook, MacBookPolicy,certificate が生成される。
ルート証明書の取得
$ cd cert
$ wget http://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification-Authority-G5.pem
これでルート証明書が取得できた。
$ ls
57555c6996-certificate.pem.crt
57555c6996-private.pem.key
VeriSign-Class 3-Public-Primary-Certification-Authority-G5.pem
サンプルのソースコードの修正
device.js
awsIot = require('aws-iot-device-sdk');
//
// Replace the values of '<YourUniqueClientIdentifier>' and '<YourAWSRegion>'
// with a unique client identifier and the AWS region you created your
// certificate in (e.g. 'us-east-1'). NOTE: client identifiers must be
// unique within your AWS account; if a client attempts to connect with a
// client identifier which is already in use, the existing connection will
// be terminated.
//
var device = awsIot.device({
keyPath: './cert/57555c6996-private.pem.key',
certPath: './cert/57555c6996-certificate.pem.crt',
caPath: './cert/VeriSign-Class 3-Public-Primary-Certification-Authority-G5.pem',
clientId: 'MacBook',
region: 'ap-northeast-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());
});
TestClientの作成
awsIot = require('aws-iot-device-sdk');
//
// Replace the values of '<YourUniqueClientIdentifier>' and '<YourAWSRegion>'
// with a unique client identifier and the AWS region you created your
// certificate in (e.g. 'us-east-1'). NOTE: client identifiers must be
// unique within your AWS account; if a client attempts to connect with a
// client identifier which is already in use, the existing connection will
// be terminated.
//
var device = awsIot.device({
keyPath: './cert/57555c6996-private.pem.key',
certPath: './cert/57555c6996-certificate.pem.crt',
caPath: './cert/VeriSign-Class 3-Public-Primary-Certification-Authority-G5.pem',
clientId: 'MacBook',
region: 'ap-northeast-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());
});
実行
$ ls
device.js cert node_modules
$node device.js
connect
$ ls
device.js cert node_modules
$node device.js
connect
Publishした{ test_data:1}がMQTT Clientのtopic_2に反映される。
プログラムでは、この部分でtopic_2にPublishしている。
device.publish('topic_2', JSON.stringify({ test_data: 1}));
今度は、{key: "value"}という値をMQTT ClientからMacBookに、topic_1でPublishする。
device
.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});
$ node device.js
connected
message topic_1 {key: "value"}
Author And Source
この問題について(nodejsで、AWSIoTのDevice SDKを用いてMacBookをデバイスに登録する), 我々は、より多くの情報をここで見つけました https://qiita.com/akira-sasaki/items/3e7277d4440600492b21著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .