0
0

More than 3 years have passed since last update.

Azure IoT Hub に Node.js で publish

Last updated at Posted at 2021-01-17

Ubuntu 20.10 で確認しました。

ライブラリーのインストール

sudo npm install -g azure-iot-device-mqtt
sudo npm install -g azure-iot-device
azure_publish.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  azure_publish.js
//
//                  Jan/20/2021
//
// ---------------------------------------------------------------
'use strict'
var devicemqtt = require('azure-iot-device-mqtt')
var device = require('azure-iot-device')
const dotenv = require('dotenv')
//
// ---------------------------------------------------------------
function printResultFor(op) {
    return function printResult(err, res) {
    if (err) console.log(op + ' error: ' + err.toString())
    if (res) console.log(op + ' status: ' + res.constructor.name)
    }
}
// ---------------------------------------------------------------
function define_data_proc ()
{
    const device_id = "atami"
    const jikan= new Date()
    const hour = jikan.getHours()
    const minute = jikan.getMinutes()
    const second = jikan.getSeconds()
    const current_time = "" + hour + ":" + minute + ":" + second
        const tt = (20 + (Math.random() * 15)) * 10
        const temperature = Math.round (tt) / 10
        const hh = (60 + (Math.random() * 20)) * 10
        const humidity = Math.round (hh) / 10
        const ddx = { device_id: device_id, time: current_time, temperature: temperature, humidity: humidity }
        const data = JSON.stringify(ddx)

    return data
}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")
dotenv.config()
const connectionString = `${process.env.CONNECTSTRING}`
console.log(connectionString)

var Message = device.Message

var client = devicemqtt.clientFromConnectionString(connectionString)

var connectCallback = function (err) {
if (err)
    {
    console.error('Could not connect: ' + err);
    }
else
    {
    console.log('Client connected')

    const data =  define_data_proc ()
    var message = new Message(data)
    console.log("Sending message: " + message.getData())
    client.sendEvent(message, printResultFor('send'))
console.error ("*** ppp ***")
    }
console.error ("*** qqq ***")
}

client.open(connectCallback)
console.error ("*** rrr ***")

console.error ("*** 終了 ***")
// ---------------------------------------------------------------
.env
CONNECTSTRING="HostName=iot-aa.azure-devices.net;DeviceId=pansy;SharedAccessKey=SvsFWVsCy2bJkH0QuPJeIabcdefgh8mo6S6vNCjom92="

プライマリ接続文字列 をポータルで調べる必要があります。
primary_jan17.png

次のコマンドでも調べることが出来ます。

az iot hub device-identity connection-string show --hub-name iot-aa \
    --device-id pansy --output table

実行コマンド

export NODE_PATH=/usr/lib/node_modules
./azure_publish.js

関連情報
Azure IoT Hub に Node.js で subscribe

0
0
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
0
0