forked from Azure/azure-iot-sdks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_c2d_message.js
39 lines (32 loc) · 1.18 KB
/
send_c2d_message.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
var Client = require('azure-iothub').Client;
var Message = require('azure-iot-common').Message;
var connectionString = '[IoT Hub Connection String]';
var targetDevice = '[Target device that will receive the message]';
var client = Client.fromConnectionString(connectionString);
client.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Client connected');
// Create a message and send it to the IoT Hub every second
setInterval(function () {
var data = JSON.stringify({ text : 'foo' });
var message = new Message(data);
console.log('Sending message: ' + message.getData());
client.send(targetDevice, message, printResultFor('send'));
}, 2000);
}
});
// Helper function to print results in the console
function printResultFor(op) {
return function printResult(err, res) {
if (err) {
console.log(op + ' error: ' + err.toString());
} else {
console.log(op + ' status: ' + res.constructor.name);
}
};
}