-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
73 lines (62 loc) · 1.91 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
68
69
70
71
72
73
// app.js
var express = require('express');
var path = require('path')
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://iot.eclipse.org')
client.on('connect', function () {
client.subscribe('techo_climate')
client.subscribe('techo_rotation')
client.publish('techo_presence', 'Server Running')
})
app.use(express.static(__dirname + '/assets'));
app.use(express.static(__dirname + '/css'));
app.use(express.static(__dirname + '/img'));
app.use(express.static(__dirname + '/js'));
app.use(express.static(__dirname + '/node_modules'));
app.get('/', function(req, res,next) {
res.sendFile(__dirname + '/index.html');
});
app.get('/index', function(req, res,next) {
res.sendFile(__dirname + '/index.html');
});
app.get('/imu', function(req, res,next) {
res.sendFile(__dirname + '/imu.html');
});
//Whenever someone connects this gets executed
io.on('connection', function(socket) {
console.log('A user connected');
client.on('message', function (topic, message) {
console.log(topic);
if (topic == "techo_climate") {
var data1 = message.toString()
var temp1 = data1.replace(/\s/g, "")
var array1 = temp1.split(',');
console.log(array1);
socket.emit('techo_climate',array1);
}else {
var data = message.toString()
var temp = data.replace(/\s/g, "")
var array = temp.split(',');
console.log(array);
socket.emit('techo_vib',array);
}
// console.log(message.toString())
})
//Whenever someone disconnects this piece of code executed
socket.on('disconnect', function () {
console.log('A user disconnected');
});
//
// var sendToFront = function(data){
// var data = JSON.stringify(data);
// var data1 = JSON.parse(data);
// console.log(data1.temp);
//
// }
});
server.listen(3000, function() {
console.log('listening on *:3000');
});