forked from martin-doyle/SensorTagMQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
301 lines (286 loc) · 11.2 KB
/
index.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* Author: Martin Doyle <[email protected]>
* Uses Sandeep Mistry's node-sensortag library (https://github.com/sandeepmistry/node-sensortag)
*/
'use strict';
var fs = require('fs');
var async = require('async');
var SensorTag = require('sensortag');
var mqtt = require('mqtt');
var configFile = 'config.json';
var config = JSON.parse(
fs.readFileSync(configFile));
var SensorTags = config.SensorTags;
console.log(SensorTags);
SensorTag.SCAN_DUPLICATES = true;
// Timeout Variables
// Discovering is limited to timeoutVar
var timeoutVar = config.Timeout;
var timeoutID;
var timeoutCleared = true;
var temperatureEnabled = config.TemperatureEnabled;
var humidityEnabled = config.HumidityEnabled;
var barometricEnabled = config.BarometricEnabled;
var luxometerEnabled = config.LuxometerEnabled;
var intervalUpdate = config.Interval;
var intervalIDs = [];
// Configure and start MQTT connection
var mqttHost = config.MQTTHost;
var mqttPort = config.MQTTPort;
var mqttOptions = {
host: mqttHost,
port: mqttPort,
};
var topicStart = 'sensors/' + mqttOptions.username;
var mqttClient = mqtt.connect('mqtts://' + mqttHost + ':' + mqttPort, mqttOptions);
// For each SensorTag
function onDiscover(sensorTag) {
var sensorTagID;
sensorTagID = SensorTags.indexOf(sensorTag.uuid);
if (sensorTagID > -1) {
console.log('SensorTag ' + sensorTag.uuid + ' found');
} else {
console.log('SensorTag ' + sensorTag.uuid + ' not in list');
return;
}
var deviceInfo = {};
var deviceData = {};
stopTimed();
console.log('Sensortag discovered: ' + sensorTag);
sensorTag.once('disconnect', function () {
console.log('Sensortag disconnected.');
if (timeoutCleared) {
scanTimed();
}
clearInterval(intervalIDs[sensorTagID]);
});
// Set up SensorTag and send device information
sensorTag.connectAndSetup(function () {
var topic,
tempString;
console.log('Connect and setup');
async.series([
function (callback) {
console.log('readDeviceName');
sensorTag.readDeviceName(function (error, deviceName) {
console.log('\tDevice Name = ' + deviceName);
console.log('\tType = ' + sensorTag.type);
console.log('\tUUID = ' + sensorTag.uuid);
deviceInfo.deviceName = deviceName;
deviceInfo.type = sensorTag.type;
deviceInfo.uuid = sensorTag.uuid;
callback();
});
},
function (callback) {
sensorTag.readFirmwareRevision(function (error, firmwareRevision) {
console.log('\tFirmware Revision = ' + firmwareRevision);
deviceInfo.firmwareRevision = firmwareRevision;
callback();
});
},
function (callback) {
sensorTag.readManufacturerName(function (error, manufacturerName) {
console.log('\tManufacturer = ' + manufacturerName);
deviceInfo.manufacturerName = manufacturerName;
callback();
});
}
],
function (error, results) {
if (error) {
console.log(error);
}
topic = topicStart + '/' + sensorTag.uuid + '/deviceInfo';
tempString = JSON.stringify(deviceInfo);
console.log(topic + ' ' + tempString);
mqttClient.publish(topic, tempString);
scanTimed();
}
);
function readSensors() {
async.series([
function (callback) {
// Read IR temperature sensor values
if (temperatureEnabled) {
async.series([
function (cb) {
console.log('Enable IR Temperature Sensor ' + sensorTag.uuid);
sensorTag.enableIrTemperature(cb);
},
function (cb) {
setTimeout(cb, config.EnableTimeout);
},
function (cb) {
console.log('Read IR Temperature Sensor ' + sensorTag.uuid);
sensorTag.readIrTemperature(function (error, objectTemperature, ambientTemperature) {
tempString = objectTemperature.toFixed(2);
console.log(sensorTag.uuid + '/objectTemperature' + ' ' + tempString);
deviceData.objectTemperature = tempString;
tempString = ambientTemperature.toFixed(2);
console.log(sensorTag.uuid + '/ambientTemperature' + ' ' + tempString);
deviceData.ambientTemperature = tempString;
cb();
});
},
function (cb) {
console.log('Disable IR Temperature Sensor ' + sensorTag.uuid);
sensorTag.disableIrTemperature(cb);
}
],
function (error, results) {
if (error) {
console.log(error);
}
console.log('IR Temperature read: ' + sensorTag.uuid);
callback(null, 'IR Temperature');
});
} else {
callback();
}
},
function (callback) {
// Read humidity sensor values
if (humidityEnabled) {
async.series([
function (cb) {
console.log('Enable Humidity Sensor ' + sensorTag.uuid);
sensorTag.enableHumidity(cb);
},
function (cb) {
setTimeout(cb, config.EnableTimeout);
},
function (cb) {
console.log('Read Humidity Sensor ' + sensorTag.uuid);
sensorTag.readHumidity(function (error, temperature, humidity) {
tempString = temperature.toFixed(2);
console.log(sensorTag.uuid + '/temperature' + ' ' + tempString);
deviceData.temperature = tempString;
tempString = humidity.toFixed(2);
console.log(sensorTag.uuid + '/humidity' + ' ' + tempString);
deviceData.humidity = tempString;
cb();
});
},
function (cb) {
console.log('Disable Humidity Sensor ' + sensorTag.uuid);
sensorTag.disableHumidity(cb);
}
],
function (error, results) {
if (error) {
console.log(error);
}
console.log('Humidity read: ' + sensorTag.uuid);
callback(null, 'Humidity');
});
} else {
callback();
}
},
function (callback) {
// Read pressure sensor values
if (barometricEnabled) {
async.series([
function (cb) {
console.log('Enable Barometric Pressure Sensor ' + sensorTag.uuid);
sensorTag.enableBarometricPressure(cb);
},
function (cb) {
setTimeout(cb, config.EnableTimeout);
},
function (cb) {
console.log('Read Barometric Pressure Sensor ' + sensorTag.uuid);
sensorTag.readBarometricPressure(function (error, pressure) {
tempString = pressure.toFixed(2);
console.log(sensorTag.uuid + '/pressure' + ' ' + tempString);
deviceData.pressure = tempString;
cb();
});
},
function (cb) {
console.log('Disable Barometric Pressure Sensor ' + sensorTag.uuid);
sensorTag.disableBarometricPressure(cb);
}
],
function (error, results) {
if (error) {
console.log(error);
}
console.log('Barometric Pressure read: ' + sensorTag.uuid);
callback(null, 'Barometric Pressure');
});
} else {
callback();
}
},
function (callback) {
// Read luxometer sensor values
if (luxometerEnabled) {
async.series([
function (cb) {
console.log('Enable Luxometer Sensor ' + sensorTag.uuid);
sensorTag.enableLuxometer(cb);
},
function (cb) {
setTimeout(cb, config.EnableTimeout);
},
function (cb) {
console.log('Read Luxometer Sensor ' + sensorTag.uuid);
sensorTag.readLuxometer(function (error, lux) {
tempString = lux.toFixed(2);
console.log(sensorTag.uuid + '/luxometer' + ' ' + tempString);
deviceData.lux = tempString;
cb(null, 'Luxometer');
});
},
function (cb) {
console.log('Disable Luxometer Sensor ' + sensorTag.uuid);
sensorTag.disableLuxometer(cb);
}
],
function (error, results) {
if (error) {
console.log(error);
}
console.log('Luxometer read: ' + sensorTag.uuid);
callback(null, 'Luxometer');
});
} else {
callback();
}
}
],
function (error, results) {
if (error) {
console.log(error);
}
topic = topicStart + '/' + sensorTag.uuid + '/deviceData';
tempString = JSON.stringify(deviceData);
console.log(topic + ' ' + tempString);
mqttClient.publish(topic, tempString);
console.log('Sensors read: ' + results + ' ' + sensorTag.uuid);
});
}
// Set an interval, get and send SensorTag data
intervalIDs[sensorTagID] = setInterval(readSensors, intervalUpdate);
});
}
// Start timed discovering
function scanTimed() {
console.log('Start discovering');
timeoutCleared = false;
SensorTag.discoverAll(onDiscover);
timeoutID = setTimeout(function () {
stopTimed();
}, timeoutVar);
}
//Stop timer and discovering
function stopTimed() {
SensorTag.stopDiscoverAll(onDiscover);
timeoutCleared = true;
console.log('Stop discovering');
clearTimeout(timeoutID);
}
//Discover all SensorTags
scanTimed();