nodejs에서 AWSIoT의 Device SDK를 사용하여 MacBook을 장치에 등록
필요한 환경
패키지
버전
Node.js
V4.4.3
v4.4.3을 설치합니다.
$node -v
v4.4.3
aws-iot-device-sdk 설치
npm install aws-iot-device-sdk
샘플 소스 코드
device.jsawsIot = 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
루트 인증서
루트 인증서 얻기에서 설명
clientId
ClientID
Things 이름 (이 예에서는 MacBook)
region
도쿄라면 ap-northeast-1
Things 만들기







여기까지의 순서로
npm install aws-iot-device-sdk
샘플 소스 코드
device.jsawsIot = 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
루트 인증서
루트 인증서 얻기에서 설명
clientId
ClientID
Things 이름 (이 예에서는 MacBook)
region
도쿄라면 ap-northeast-1
Things 만들기







여기까지의 순서로
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());
});







여기까지의 순서로
얻을 수 있습니다.
$mkdir cert
key라는 폴더를 만들고 거기에 저장합니다.
또한 AWSIoT의 Dashboard에는 MacBook, MacBookPolicy, certificate가 생성된다.

루트 인증서 얻기

$cd cert
$ wget h tp // w w. sy 만나 c. 코 m / 콘텐 t / 엔 / u s / 엔테 rp 리세 / ゔ ぇ し し ん / 로오 ts / ょ ぇ し ん - C ぁ s % 203-pu b ぃ c P 리 마 ry-r 치후 카치 온 - 어조 ty-G5.ぺm
이제 루트 인증서를 얻을 수있었습니다.
$ ls
57555c6996-certificate.pem.crt
57555c6996-private.pem.key
VeriSign-Class 3-Public-Primary-Certification-Authority-G5.pem
샘플 소스 코드 수정
device.jsawsIot = 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 만들기




실행
$ ls
device.js cert node_modules
$node device.js
연결
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"}
Reference
이 문제에 관하여(nodejs에서 AWSIoT의 Device SDK를 사용하여 MacBook을 장치에 등록), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/akira-sasaki/items/3e7277d4440600492b21
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
$ 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 만들기




실행
$ ls
device.js cert node_modules
$node device.js
연결
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"}
Reference
이 문제에 관하여(nodejs에서 AWSIoT의 Device SDK를 사용하여 MacBook을 장치에 등록), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/akira-sasaki/items/3e7277d4440600492b21
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
$ ls
device.js cert node_modules
$node device.js
연결
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"}
Reference
이 문제에 관하여(nodejs에서 AWSIoT의 Device SDK를 사용하여 MacBook을 장치에 등록), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/akira-sasaki/items/3e7277d4440600492b21텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)