-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (58 loc) · 1.87 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
const mqtt = require('./mqtt');
const onewire = require('ds18b20');
const { config } = require('./config');
const { log } = require('./logger');
let sensorIds = [];
async function init(){
if (!config.loadError) {
sensorIds = await findSensors();
if (sensorIds.length > 0) {
if (config.username && config.password) {
log(`Connect MQTT server with credentials`);
mqtt.connect(config.server, config.username, config.password);
} else {
log(`Connect MQTT server`);
mqtt.connect(config.server);
}
setInterval(getTemperatures, config.publishFrequency * 1000);
}
}
}
function publishTemperature(sensorId, value) {
if (sensorId && !isNaN(value)){
const topic = `${config.topic}/${sensorId}/SENSOR`;
const data = {"DS18B20": { "Address": sensorId, "Temperature": value } };
mqtt.publish(topic, JSON.stringify(data));
}
}
async function findSensors() {
return new Promise((resolve, _) => {
log('Searching for OneWire sensors...');
onewire.sensors(function(err, ids) {
if(err) {
console.error(err);
resolve([]);
} else if (ids) {
log('Sensors found. Publishing to:')
ids.forEach((id)=> {
log(`${config.topic}/${id}/SENSOR`);
});
resolve(ids);
} else {
log('No sensors found');
resolve([]);
}
});
});
}
function getTemperatures() {
sensorIds.map((id) => {
log(`Getting temperature for ${id}`, true);
onewire.temperature(id, {}, (err, data) => {
if (!err && data) {
publishTemperature(id, data);
}
});
});
}
init();