Node.js
AzureIoTHub

Azure IoT Hub に Node.js で publish

Arch Linux と Raspbian で動作を確認しました。

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

sudo npm install azure-iot-device
sudo npm install azure-iot-device-mqtt

5秒毎にメッセージを送るサンプルです。
connectionString は変更して下さい。

azure_publish.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  azure_publish.js
//
//                  Feb/06/2018
//
// ---------------------------------------------------------------
var devicemqtt = require('azure-iot-device-mqtt')
var device = require('azure-iot-device')
//
// ---------------------------------------------------------------
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 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 = { deviceId: 'dev001', time: current_time, temperature: temperature, humidity: humidity }
        const data = JSON.stringify(ddx)
    return data
}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")
//
var Message = device.Message;

const connectionString = "HostName=iot-aa.azure-devices.net;DeviceId=pansy;SharedAccessKey=2eQ2wF5O123456789G3CGJNXF123TfAnMJbZ8wtJPu4="

var client = devicemqtt.clientFromConnectionString(connectionString)

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

    setInterval(function()
        {
        const data =  define_data_proc ()
        var message = new Message(data)
        console.log("Sending message: " + message.getData())
        client.sendEvent(message, printResultFor('send'))
        }, 5000)
    }
}

client.open(connectCallback)

console.error ("*** 終了 ***")
// ---------------------------------------------------------------