forked from mattbit/jsday-tessel-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (48 loc) · 1.96 KB
/
app.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
var mqtt = require('mqtt');
var tessel = require('tessel');
var led = tessel.led[0];
var climatelib = require('climate-si7020');
var ambientlib = require('ambient-attx4');
var client = mqtt.connect('mqtt://test.mosca.io?clientId=93247098723450');
client.on('connect', function () {
console.log("Connected!");
client.publish("jsday/presence_0", "Ok!");
var climate = climatelib.use(tessel.port['B']);
climate.on('ready', function () {
console.log('Climate module ready.');
setInterval(function () {
climate.readTemperature('c', function (err, temp) {
climate.readHumidity(function (err, humid) {
console.log('Temperature: ' + temp.toFixed(4));
console.log('Humidity: ' + humid.toFixed(4));
var time = new Date();
var data = {
time: time.getTime(),
temp: temp.toFixed(4),
humid: humid.toFixed(4),
};
client.publish("jsday/tessel_climate", JSON.stringify(data));
});
});
}, 1000);
});
var ambient = ambientlib.use(tessel.port['A']);
ambient.on('ready', function () {
console.log("Ambient module ready.");
setInterval(function () {
ambient.getLightLevel( function (err, light) {
ambient.getSoundLevel(function (err, sound) {
console.log('Sound: ' + sound.toFixed(4));
console.log('Light: ' + light.toFixed(4));
var time = new Date();
var data = {
time: time.getTime(),
light: light.toFixed(4),
sound: sound.toFixed(4),
};
client.publish("jsday/tessel_ambient", JSON.stringify(data));
});
});
}, 1000);
});
});