Created
May 26, 2018 01:39
-
-
Save avirup171/5b5b23cbe39af2a9a3d1fc7c82c75749 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var clientFromConnectionString = require('azure-iot-device-mqtt').clientFromConnectionString; | |
var Message = require('azure-iot-device').Message; | |
function azcall() | |
{ | |
var connectionString = 'YOUR CONNECTION STRING'; | |
var client = clientFromConnectionString(connectionString); | |
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); | |
}; | |
} | |
var connectCallback = function (err) | |
{ | |
if (err) | |
{ | |
console.log('Could not connect: ' + err); | |
} | |
else | |
{ | |
console.log('Client connected'); | |
pubData(); | |
function pubData() | |
{ | |
var rand= Math.floor((Math.random() * 100) + 1); | |
var data = JSON.stringify({ "device_id": "Simulated Device", "Data":rand}); | |
var message = new Message(data); | |
console.log("Sending message: " + message.getData()); | |
client.sendEvent(message, printResultFor('send')); | |
} | |
} | |
}; | |
client.open(connectCallback); | |
} | |
setInterval(azcall, 1500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment