This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
remote_monitoring.js
143 lines (125 loc) · 4.16 KB
/
remote_monitoring.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
// Azure IoT packages
var Protocol = require('azure-iot-device-http').Http;
var Client = require('azure-iot-device').Client;
var ConnectionString = require('azure-iot-device').ConnectionString;
var Message = require('azure-iot-device').Message;
// Edison packages
var five = require("johnny-five");
var Edison = require("edison-io");
var board = new five.Board({
io: new Edison()
});
var hostName = '<IOTHUB_HOST_NAME>';
var deviceId = '<DEVICE_ID>';
var sharedAccessKey = '<SHARED_ACCESS_KEY>';
// String containing Hostname, Device Id & Device Key in the following formats:
// "HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"
var connectionString = 'HostName=' + hostName + ';DeviceId=' + deviceId + ';SharedAccessKey=' + sharedAccessKey;
// Sensor data
var temperature = 0;
var humidity = 0;
var externalTemperature = 0;
// Create IoT Hub client
var client = Client.fromConnectionString(connectionString, Protocol);
// Helper function to print results for an operation
function printErrorFor(op) {
return function printError(err) {
if (err) console.log(op + ' error: ' + err.toString());
};
}
// Send device meta data
var deviceMetaData = {
'ObjectType': 'DeviceInfo',
'IsSimulatedDevice': 0,
'Version': '1.0',
'DeviceProperties': {
'DeviceID': deviceId,
'HubEnabledState': 1,
'CreatedTime': '2015-09-21T20:28:55.5448990Z',
'DeviceState': 'normal',
'UpdatedTime': null,
'Manufacturer': 'Intel',
'ModelNumber': 'Edison',
'SerialNumber': '12345678',
'FirmwareVersion': '159',
'Platform': 'node.js',
'Processor': 'Intel',
'InstalledRAM': '64 MB',
'Latitude': 47.617025,
'Longitude': -122.191285
},
'Commands': [{
'Name': 'SetTemperature',
'Parameters': [{
'Name': 'Temperature',
'Type': 'double'
}]
},
{
'Name': 'SetHumidity',
'Parameters': [{
'Name': 'Humidity',
'Type': 'double'
}]
}]
};
board.on("ready", function() {
var temp = new five.Temperature({
pin: "A0",
controller: "GROVE"
});
client.open(function (err, result) {
if (err) {
printErrorFor('open')(err);
} else {
console.log('Sending device metadata:\n' + JSON.stringify(deviceMetaData));
client.sendEvent(new Message(JSON.stringify(deviceMetaData)), printErrorFor('send metadata'));
client.on('message', function (msg) {
console.log('receive data: ' + msg.getData());
try {
var command = JSON.parse(msg.getData());
switch (command.Name) {
case 'SetTemperature':
temperature = command.Parameters.Temperature;
console.log('New temperature set to :' + temperature + 'F');
client.complete(msg, printErrorFor('complete'));
break;
case 'SetHumidity':
humidity = command.Parameters.Humidity;
console.log('New humidity set to :' + humidity + '%');
client.complete(msg, printErrorFor('complete'));
break;
default:
console.error('Unknown command: ' + command.Name);
client.reject(msg, printErrorFor('complete'));
break;
}
}
catch (err) {
printErrorFor('parse received message')(err);
client.reject(msg, printErrorFor('reject'));
}
});
// start event data send routing
var sendInterval = setInterval(function () {
temperature = temp.celsius;
var data = JSON.stringify({
'DeviceID': deviceId,
'Temperature': temperature,
'Humidity': humidity,
'ExternalTemperature': externalTemperature
});
console.log('Sending device event data:\n' + data);
client.sendEvent(new Message(data), printErrorFor('send event'));
}, 1000);
client.on('error', function (err) {
printErrorFor('client')(err);
if (sendInterval) clearInterval(sendInterval);
client.close();
});
}
});
});