Node.js
IoT
awsIoT

Node.js で AWS IOT に Pub/Sub する

More than 1 year has passed since last update.

Publish
エンドポイントは変更して下さい。

#! /usr/bin/node
// ---------------------------------------------------------------
//  devicePub.js
//
//                  Dec/08/2017
//
// ---------------------------------------------------------------
var awsIot = require('aws-iot-device-sdk')

console.error ("*** 開始 ***")

const topic = 'r10001'

const KEY =  './r10001.private.key'
const CERT =  './r10001.cert.pem'
const TRUSTED_CA = './root-CA.crt'

var device = awsIot.device({
    keyPath: KEY,
    certPath: CERT,
    caPath: TRUSTED_CA,
    host: 'xxx00iq1d4m00.iot.ap-northeast-1.amazonaws.com'
})

device.on('connect', function() {
        console.log('connect')
        device.publish(topic, 'Hello mqtt PM 18:08\n\n', {'qos': 1})
        console.error ("*** 終了 ***")
    })

// ---------------------------------------------------------------

Subscribe

#! /usr/bin/node
// ---------------------------------------------------------------
//  deviceSub.js
//
//                  Dec/08/2017
//
// ---------------------------------------------------------------
var awsIot = require('aws-iot-device-sdk')

var KEY = './r10001.private.key'
var CERT = './r10001.cert.pem'
var TRUSTED_CA = './root-CA.crt'

var device = awsIot.device(
{
    keyPath: KEY,
    certPath: CERT,
    caPath: TRUSTED_CA,
    clientId: '+',
    host: 'xxx00iq1d4m00.iot.ap-northeast-1.amazonaws.com'
})

device.on('connect', function() {
    console.log('connect')
    device.subscribe('r10001', {'qos': 1})
    })

device.on('message', function(topic, payload) {
    console.log('message', topic, payload.toString())
    })
// ---------------------------------------------------------------