-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.js
More file actions
executable file
·71 lines (65 loc) · 2.01 KB
/
Copy pathServer.js
File metadata and controls
executable file
·71 lines (65 loc) · 2.01 KB
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
/* framework NODEJS */
var server_io = require('http').createServer(handler),
io = require('socket.io').listen(server_io),
fs = require('fs');
var firebase = require("firebase");
var PORT_IO = 3000;
/* Server IO */
server_io.listen(PORT_IO, function () {
console.log("Server IO listening at port %d", PORT_IO);
});
io.sockets.on('connection', function(socket) {
var address = socket.handshake.address;
console.log("Connect IO %s %d", address.address, address.port);
try {
socket_net.write("R\n");
}
catch(err) {
console.log("Arduino is disconnect");
}
socket.on('update range', function(max, min) {
console.log("update range");
socket_net.write("r," + max + "," + min + "\n");
});
socket.on('disconnect', function() {
console.log("Disconnect IO");
});
});
function handler(req, res) {
fs.readFile(__dirname+'/monitoring.html', function(err, data) {
if (err) {
console.log(err);
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
/* Fire Base */
/* Here config your firebase*/
var config = {
apiKey: "AIzaSyB7bq3kB_Hn0nZ5YBj1n44NSGpXNNHlhDw",
authDomain: "basesensorcombustible.firebaseapp.com",
databaseURL: "https://basesensorcombustible.firebaseio.com",
projectId: "basesensorcombustible",
storageBucket: "basesensorcombustible.appspot.com",
messagingSenderId: "671666726369"
};
var server_firebase = firebase.initializeApp(config);
var db = server_firebase.database();
var ref = db.ref("ServidorWeb_GPS");
/* This event update when exist any change in db firebase*/
/* firese return:
{ GPSLatitud: '-13.52101723',
GPSLongitud: '-71.96225735',
GPSVelocidad: '0',
Nivel: '0' }
*/
ref.on("value", function(snapshot) {
var data = snapshot.val();
console.log(data);
io.sockets.emit('data update', data);
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});