LoginSignup
1
1

More than 1 year has passed since last update.

Raspberry Pi で Node.js の aws-iot-device-sdk を使う

Last updated at Posted at 2017-12-19

aws-iot-device-sdk は、ディフォールト の Node.js v0.10.29 では動きません。
私は、 v9.2.0 で動くことを確認しました。

環境は、次の通りです。

$ which node
/usr/local/bin/node
$ node --version
v9.2.0

2秒毎に、数字をカウントアップして topic_1 に publish するプログラムです。
host 名は変更して下さい。
認証ファイルは、certs というフォルダーに入れました。

iot_publish.js
#! /usr/local/bin/node
// -----------------------------------------------------------------
//  iot_publish.js
//
//                      Dec/19/2017
// -----------------------------------------------------------------
// AWS IoT DeviceSDKの利用
var awsIot = require('aws-iot-device-sdk')
const deviceModule = awsIot.device


const device = deviceModule({
    region: 'ap-northeast-1',
    keyPath: 'certs/gw10005.private.key',
    certPath: 'certs/gw10005.cert.pem',
    caPath: 'certs/root-CA.crt',
    host: 'xxxxxxxxxx.iot.ap-northeast-1.amazonaws.com'
})

console.log("*** start ***")

var count = 0

// 通信確立した際に呼び出されるイベント
device.on('connect', function() {
    console.log('connect')
    setInterval( function()
        {
        count++
        const str_count = count.toString()
        console.log(str_count)
        device.publish('topic_1', str_count)
        }, 2000)
    })

// -----------------------------------------------------------------
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1