/
index.js
47 lines (37 loc) · 1.14 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
const path = require('path');
const five = require('johnny-five');
const Tessel = require('tessel-io');
const board = new five.Board({
io: new Tessel(),
});
const throttle = require('lodash/throttle');
board.on('ready', () => {
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 80;
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, '/public/index.html'));
});
const monitor = new five.Multi({
controller: 'BME280',
});
const handleChange = throttle(() => {
const temperature = monitor.thermometer.fahrenheit;
const pressure = monitor.barometer.pressure;
const relativeHumidity = monitor.hygrometer.relativeHumidity;
io.sockets.emit('weather updated', {
temperature,
pressure,
relativeHumidity,
});
}, 500);
monitor.on('change', handleChange);
http.listen(port, function() {
console.log(
'Your server is up and running on Port ' + port + '. Good job!',
);
});
});